Skip to content

Commit 5e7b0e9

Browse files
committed
OpenGL: make get_proc_address take &CStr instead of &str (#289)
1 parent bcf8afb commit 5e7b0e9

5 files changed

Lines changed: 27 additions & 16 deletions

File tree

examples/render_femtovg/src/main.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use baseview::{
66
use femtovg::renderer::OpenGl;
77
use femtovg::{Canvas, Color};
88
use std::cell::{Cell, RefCell};
9+
use std::ffi::CString;
910

1011
struct FemtovgExample {
1112
window_context: WindowContext,
@@ -20,8 +21,10 @@ impl FemtovgExample {
2021
let gl_context = window_context.gl_context().unwrap();
2122
unsafe { gl_context.make_current() };
2223

23-
let renderer =
24-
unsafe { OpenGl::new_from_function(|s| gl_context.get_proc_address(s)) }.unwrap();
24+
let renderer = unsafe {
25+
OpenGl::new_from_function(|s| gl_context.get_proc_address(&CString::new(s).unwrap()))
26+
}
27+
.unwrap();
2528

2629
let mut canvas = Canvas::new(renderer).unwrap();
2730
let size = window_context.size();

src/gl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::platform::gl::*;
2-
use std::ffi::c_void;
2+
use std::ffi::{c_void, CStr};
33
use std::marker::PhantomData;
44

55
#[derive(Clone, Debug, PartialEq)]
@@ -70,7 +70,7 @@ impl GlContext {
7070
self.inner.make_not_current();
7171
}
7272

73-
pub fn get_proc_address(&self, symbol: &str) -> *const c_void {
73+
pub fn get_proc_address(&self, symbol: &CStr) -> *const c_void {
7474
self.inner.get_proc_address(symbol)
7575
}
7676

src/platform/macos/gl.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ use objc2_app_kit::{
1111
NSOpenGLPixelFormat, NSOpenGLProfileVersion3_2Core, NSOpenGLProfileVersion4_1Core,
1212
NSOpenGLProfileVersionLegacy, NSOpenGLView, NSView,
1313
};
14-
use objc2_core_foundation::{CFBundle, CFString};
14+
use objc2_core_foundation::{CFBundle, CFString, CFStringBuiltInEncodings};
1515
use objc2_foundation::NSSize;
16-
use std::ffi::c_void;
16+
use std::ffi::{c_void, CStr};
1717
use std::ptr::NonNull;
1818

1919
pub type CreationFailedError = ();
@@ -103,8 +103,19 @@ impl GlContext {
103103
NSOpenGLContext::clearCurrentContext();
104104
}
105105

106-
pub fn get_proc_address(&self, symbol: &str) -> *const c_void {
107-
let symbol_name = CFString::from_str(symbol);
106+
pub fn get_proc_address(&self, symbol: &CStr) -> *const c_void {
107+
// SAFETY: The string pointer is valid
108+
let symbol_name = unsafe {
109+
CFString::with_bytes(
110+
None,
111+
symbol.as_ptr().cast(),
112+
symbol.count_bytes().try_into().unwrap(),
113+
CFStringBuiltInEncodings::EncodingUTF8.0,
114+
false,
115+
)
116+
}
117+
.unwrap();
118+
108119
let framework_name = CFString::from_static_str("com.apple.opengl");
109120
let framework = CFBundle::bundle_with_identifier(Some(&framework_name)).unwrap();
110121

src/platform/win/gl.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ffi::{c_void, CString};
1+
use std::ffi::{c_void, CStr};
22
use std::num::NonZeroI32;
33
use std::rc::Rc;
44
use windows_core::{s, PCSTR};
@@ -79,8 +79,7 @@ impl GlContextInner {
7979
let _ = self.wgl_ctx.make_not_current();
8080
}
8181

82-
pub fn get_proc_address(&self, symbol: &str) -> *const c_void {
83-
let symbol = CString::new(symbol).unwrap();
82+
pub fn get_proc_address(&self, symbol: &CStr) -> *const c_void {
8483
let symbol_ptr = symbol.as_ptr().cast();
8584

8685
if let Some(addr) = unsafe { wglGetProcAddress(symbol_ptr) } {

src/platform/x11/gl.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::wrappers::glx::*;
44
use crate::wrappers::xlib::{XErrorHandler, XLibError};
55

66
use crate::platform::x11::xcb_window::XcbWindow;
7-
use std::ffi::{c_ulong, c_void, CString};
7+
use std::ffi::{c_ulong, c_void, CStr};
88
use std::rc::Rc;
99
use x11_dl::error::OpenError;
1010
use x11_dl::glx::GLXContext;
@@ -165,10 +165,8 @@ impl GlContextInner {
165165
self.window.get().into()
166166
}
167167

168-
pub fn get_proc_address(&self, symbol: &str) -> *const c_void {
169-
let symbol = CString::new(symbol).unwrap();
170-
171-
match self.glx.get_proc_address(&symbol) {
168+
pub fn get_proc_address(&self, symbol: &CStr) -> *const c_void {
169+
match self.glx.get_proc_address(symbol) {
172170
Some(ptr) => ptr.as_ptr(),
173171
None => std::ptr::null(),
174172
}

0 commit comments

Comments
 (0)