Move lib creation out of EntryCustom::new_custom (#292)

* Update libloading to `0.6.1`

* Update Error type

* Make `LoadingError` a newtype

* Move lib creation out of `new_custom`
This commit is contained in:
Friz64 2020-05-07 18:14:24 +02:00 committed by GitHub
parent 93238e093b
commit 37a701ad3f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 27 deletions

View file

@ -11,7 +11,7 @@ documentation = "https://docs.rs/ash"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
libloading = "0.5.2" libloading = "0.6.1"
[features] [features]
default = [] default = []

View file

@ -5,7 +5,6 @@ use crate::RawPtr;
use libloading::Library; use libloading::Library;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::io;
use std::mem; use std::mem;
use std::os::raw::c_char; use std::os::raw::c_char;
use std::os::raw::c_void; use std::os::raw::c_void;
@ -41,19 +40,19 @@ pub struct EntryCustom<L> {
} }
#[derive(Debug)] #[derive(Debug)]
pub enum LoadingError { pub struct LoadingError(libloading::Error);
LibraryLoadError(io::Error),
}
impl fmt::Display for LoadingError { impl fmt::Display for LoadingError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { fmt::Display::fmt(&self.0, f)
LoadingError::LibraryLoadError(e) => write!(f, "{}", e),
}
} }
} }
impl Error for LoadingError {} impl Error for LoadingError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
Error::source(&self.0)
}
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum InstanceError { pub enum InstanceError {
@ -225,29 +224,24 @@ impl EntryCustom<Arc<Library>> {
/// # Ok(()) } /// # Ok(()) }
/// ``` /// ```
pub fn new() -> Result<Entry, LoadingError> { pub fn new() -> Result<Entry, LoadingError> {
Self::new_custom( let lib = Library::new(&LIB_PATH)
|| { .map_err(LoadingError)
Library::new(&LIB_PATH) .map(Arc::new)?;
.map_err(LoadingError::LibraryLoadError)
.map(Arc::new) Ok(Self::new_custom(lib, |vk_lib, name| unsafe {
}, vk_lib
|vk_lib, name| unsafe { .get(name.to_bytes_with_nul())
vk_lib .map(|symbol| *symbol)
.get(name.to_bytes_with_nul()) .unwrap_or(ptr::null_mut())
.map(|symbol| *symbol) }))
.unwrap_or(ptr::null_mut())
},
)
} }
} }
impl<L> EntryCustom<L> { impl<L> EntryCustom<L> {
pub fn new_custom<Open, Load>(open: Open, mut load: Load) -> Result<Self, LoadingError> pub fn new_custom<Load>(mut lib: L, mut load: Load) -> Self
where where
Open: FnOnce() -> Result<L, LoadingError>,
Load: FnMut(&mut L, &::std::ffi::CStr) -> *const c_void, Load: FnMut(&mut L, &::std::ffi::CStr) -> *const c_void,
{ {
let mut lib = open()?;
let static_fn = vk::StaticFn::load(|name| load(&mut lib, name)); let static_fn = vk::StaticFn::load(|name| load(&mut lib, name));
let entry_fn_1_0 = vk::EntryFnV1_0::load(|name| unsafe { let entry_fn_1_0 = vk::EntryFnV1_0::load(|name| unsafe {
@ -262,13 +256,13 @@ impl<L> EntryCustom<L> {
mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr())) mem::transmute(static_fn.get_instance_proc_addr(vk::Instance::null(), name.as_ptr()))
}); });
Ok(EntryCustom { EntryCustom {
static_fn, static_fn,
entry_fn_1_0, entry_fn_1_0,
entry_fn_1_1, entry_fn_1_1,
entry_fn_1_2, entry_fn_1_2,
lib, lib,
}) }
} }
#[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkEnumerateInstanceVersion.html>"] #[doc = "<https://www.khronos.org/registry/vulkan/specs/1.2-extensions/man/html/vkEnumerateInstanceVersion.html>"]