Skip to content

Commit

Permalink
Split off into load_checked
Browse files Browse the repository at this point in the history
  • Loading branch information
Rua committed Mar 11, 2021
1 parent 1959612 commit 215f53b
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions ash/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,8 @@ impl<L> EntryCustom<L> {
where
Load: FnMut(&mut L, &::std::ffi::CStr) -> *const c_void,
{
// TODO: Make this a &'static CStr once CStr::from_bytes_with_nul_unchecked is const
static ENTRY_POINT: &'static [u8] = b"vkGetInstanceProcAddr\0";

// Bypass the normal StaticFn::load so we can return an error
let static_fn = vk::StaticFn {
get_instance_proc_addr: unsafe {
let val = load(&mut lib, CStr::from_bytes_with_nul_unchecked(ENTRY_POINT));
if val.is_null() {
return Err(MissingEntryPoint(&ENTRY_POINT[..ENTRY_POINT.len() - 1]));
} else {
::std::mem::transmute(val)
}
},
};
let static_fn = vk::StaticFn::load_checked(|name| load(&mut lib, name))?;

let entry_fn_1_0 = vk::EntryFnV1_0::load(|name| unsafe {
mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr()))
Expand Down Expand Up @@ -244,6 +232,28 @@ impl<L> EntryCustom<L> {
}
}

impl vk::StaticFn {
pub fn load_checked<F>(mut _f: F) -> Result<Self, MissingEntryPoint>
where
F: FnMut(&::std::ffi::CStr) -> *const c_void,
{
// TODO: Make this a &'static CStr once CStr::from_bytes_with_nul_unchecked is const
static ENTRY_POINT: &[u8] = b"vkGetInstanceProcAddr\0";

Ok(vk::StaticFn {
get_instance_proc_addr: unsafe {
let cname = CStr::from_bytes_with_nul_unchecked(ENTRY_POINT);
let val = _f(cname);
if val.is_null() {
return Err(MissingEntryPoint(&ENTRY_POINT[..ENTRY_POINT.len() - 1]));
} else {
::std::mem::transmute(val)
}
},
})
}
}

#[derive(Clone, Debug)]
pub struct MissingEntryPoint(&'static [u8]); // Slice without the NUL terminator
impl std::fmt::Display for MissingEntryPoint {
Expand Down

0 comments on commit 215f53b

Please sign in to comment.