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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
//! Library api. //! //! # Example //! //! ```no_run //! # use emf_core_base_rs_ffi::CBaseBinding; //! # let base_interface: &mut dyn CBaseBinding = unsafe { std::mem::MaybeUninit::uninit().assume_init() }; //! use emf_core_base_rs_ffi::sys::api::SysBinding; //! use emf_core_base_rs_ffi::library::api::LibraryBinding; //! use emf_core_base_rs_ffi::collections::NonNullConst; //! use emf_core_base_rs_ffi::library::{OSPathChar, DEFAULT_HANDLE, LibraryHandle, Error}; //! //! unsafe { //! // `base_interface` has the type `&mut dyn CBaseBinding`. //! SysBinding::lock(base_interface); //! //! // Path of the library. Platform dependent initialisation. //! let lib_path: &OSPathChar = unsafe { std::mem::MaybeUninit::uninit().assume_init() }; //! //! let handle = match LibraryBinding::load( //! base_interface, //! DEFAULT_HANDLE, //! NonNullConst::from(lib_path) //! ).to_result() { //! Ok(handle) => handle, //! Err(_) => { //! SysBinding::panic( //! base_interface, //! Some(NonNullConst::from(b"Unable to load the library.\0")) //! ); //! } //! }; //! //! let symbol: unsafe extern "C" fn(i32, i32) -> i32 = //! match LibraryBinding::get_function_symbol( //! base_interface, //! handle, //! NonNullConst::from(b"add_fn\0") //! ).to_result() { //! Ok(sym) => { //! std::mem::transmute(sym.symbol) //! }, //! Err(_) => { //! SysBinding::panic(base_interface, //! Some(NonNullConst::from(b"Unable to find the symbol.\0")) //! ); //! } //! }; //! //! assert_eq!(symbol(3, 5), 8); //! //! match LibraryBinding::unload(base_interface, handle).to_result() { //! Ok(_) => {} //! Err(_) => { //! SysBinding::panic(base_interface, //! Some(NonNullConst::from(b"Unable to unload the library.\0")) //! ); //! } //! } //! //! SysBinding::unlock(base_interface); //! } //! ``` use crate::collections::StaticVec; pub mod api; pub mod library_loader; /// Max length of a library type. pub const LOADER_TYPE_MAX_LENGTH: usize = 64; /// Name of the native library type. pub const NATIVE_LIBRARY_TYPE_NAME: &str = "emf::core_base::native"; /// Handle of the native library loader. pub const DEFAULT_HANDLE: LoaderHandle = LoaderHandle { id: PredefinedHandles::Native as i32, }; /// Predefined loader handles. #[repr(i32)] #[non_exhaustive] #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] pub enum PredefinedHandles { Native = 0, } /// Library api errors. #[repr(i32)] #[non_exhaustive] #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] pub enum Error { PathNotFound = 0, LibraryHandleInvalid = 1, LoaderHandleInvalid = 2, InternalHandleInvalid = 3, LibraryTypeInvalid = 4, LibraryTypeNotFound = 5, DuplicatedLibraryType = 6, SymbolNotFound = 7, BufferOverflow = 8, } /// Character type of a windows path. pub type OSPathCharWindows = u16; /// Character type of a unix path. pub type OSPathCharUnix = u8; /// Character type of a path. #[cfg(unix)] pub type OSPathChar = OSPathCharUnix; /// Character type of a path. #[cfg(windows)] pub type OSPathChar = OSPathCharWindows; /// Handle to a library. #[repr(C)] #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] pub struct LibraryHandle { pub id: i32, } /// Handle to a loader. #[repr(C)] #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] pub struct LoaderHandle { pub id: i32, } /// Internal handle to a library. #[repr(C)] #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] pub struct InternalHandle { pub id: isize, } /// A symbol from a library. #[repr(C)] #[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)] pub struct Symbol<T> { pub symbol: T, } /// Library type. pub type LibraryType = StaticVec<u8, LOADER_TYPE_MAX_LENGTH>;