1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use std::error;
#[cfg(windows)]
pub const LIBRARY: &str = "vulkan-1.dll";
#[cfg(target_os = "android")]
pub const LIBRARY: &str = "libvulkan.so";
#[cfg(any(target_os = "ios", target_os = "macos"))]
pub const LIBRARY: &str = "libvulkan.dylib";
#[cfg(all(
unix,
not(target_os = "android"),
not(target_os = "ios"),
not(target_os = "macos")
))]
pub const LIBRARY: &str = "libvulkan.so.1";
pub trait Loader {
#[allow(clippy::missing_safety_doc)]
unsafe fn load(
&self,
name: &[u8],
) -> Result<extern "system" fn(), Box<dyn error::Error + 'static>>;
}
#[cfg(feature = "libloading_")]
mod libloading_loader {
use std::error;
use std::ffi::OsStr;
use std::mem;
use libloading::{Error, Library, Symbol};
use super::*;
#[derive(Debug)]
pub struct LibloadingLoader(Library);
impl LibloadingLoader {
#[inline]
pub fn new(filename: impl AsRef<OsStr>) -> Result<Self, Error> {
Library::new(filename).map(Self)
}
#[inline]
pub fn library(&self) -> &Library {
&self.0
}
}
impl Loader for LibloadingLoader {
#[inline]
unsafe fn load(
&self,
name: &[u8],
) -> Result<extern "system" fn(), Box<dyn error::Error + 'static>> {
let symbol: Symbol<Option<extern "C" fn()>> = self.0.get(name)?;
let symbol = symbol.lift_option().ok_or("missing function")?;
Ok(mem::transmute(symbol))
}
}
}
#[cfg(feature = "libloading_")]
pub use self::libloading_loader::*;