Skip to content

Commit

Permalink
Merge grovesNL#225
Browse files Browse the repository at this point in the history
225: Add a function to the native context that loads the functions using `CStr` instead of `str` r=grovesNL a=notgull

I'm not particularly attached to the name

Co-authored-by: jtnunley <[email protected]>
  • Loading branch information
bors[bot] and notgull authored Jul 25, 2022
2 parents d88346c + 17d5712 commit 11853d2
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@ pub struct Context {
}

impl Context {
pub unsafe fn from_loader_function<F>(mut loader_function: F) -> Self
pub unsafe fn from_loader_function_cstr<F>(mut loader_function: F) -> Self
where
F: FnMut(&str) -> *const std::os::raw::c_void,
F: FnMut(&CStr) -> *const std::os::raw::c_void,
{
// Note(Lokathor): This is wildly inefficient, because the loader_function
// is doubtlessly just going to allocate the `&str` we pass into a new `CString`
// so that it can pass that `*const c_char` off to the OS's actual loader.
// However, this is the best we can do without changing the outer function
// signature into something that's less alloc crazy.
let raw: native_gl::GlFns =
native_gl::GlFns::load_with(|p: *const std::os::raw::c_char| {
let c_str = std::ffi::CStr::from_ptr(p);
loader_function(c_str.to_str().unwrap()) as *mut std::os::raw::c_void
loader_function(c_str) as *mut std::os::raw::c_void
});

// Retrieve and parse `GL_VERSION`
Expand Down Expand Up @@ -82,6 +77,15 @@ impl Context {
context
}

pub unsafe fn from_loader_function<F>(mut loader_function: F) -> Self
where
F: FnMut(&str) -> *const std::os::raw::c_void,
{
Self::from_loader_function_cstr(move |name| {
loader_function(name.to_str().unwrap())
})
}

/// Creates a texture from an external GL name.
///
/// This can be useful when a texture is created outside of glow (e.g. OpenXR surface) but glow
Expand Down

0 comments on commit 11853d2

Please sign in to comment.