diff --git a/Cargo.toml b/Cargo.toml index 8aba6c5..aa3305c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,8 @@ keywords = ["dylib", "dlopen"] categories = ["api-bindings"] [dependencies] -libloading = ">=0.7, <0.9" +libloading = { version = ">=0.7, <0.9", optional = true } + +[features] +default = [ "std" ] +std = [ "dep:libloading" ] diff --git a/src/lib.rs b/src/lib.rs index 4208b02..864f01d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -127,12 +127,23 @@ //! #[cfg(not(feature = "dlopen-foo"))] //! use ffi::*; //! ``` +//! +//! ## A note on `#![no_std]` support +//! +//! Deactivating the `std` dependency through `default-features = false` will stop `dlib` from +//! importing `libloading`. This means in `no_std` environments, users of this library *must* +//! link their binaries, and not use `dlopen`. +#![no_std] #![warn(missing_docs)] +#[cfg(feature = "std")] extern crate libloading; +#[cfg(feature = "std")] pub use libloading::Error as LibLoadingError; + #[doc(hidden)] +#[cfg(feature = "std")] pub use libloading::{Library, Symbol}; /// Macro for generically invoking a FFI function @@ -235,23 +246,26 @@ pub enum DlError { /// /// Includes the error reported by `libloading` when trying to /// open the library. + #[cfg(feature = "std")] CantOpen(LibLoadingError), /// Some required symbol was missing in the library MissingSymbol(&'static str), } -impl std::error::Error for DlError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { +impl core::error::Error for DlError { + fn source(&self) -> Option<&(dyn core::error::Error + 'static)> { match *self { + #[cfg(feature = "std")] DlError::CantOpen(ref e) => Some(e), DlError::MissingSymbol(_) => None, } } } -impl std::fmt::Display for DlError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl core::fmt::Display for DlError { + fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { match *self { + #[cfg(feature = "std")] DlError::CantOpen(ref e) => write!(f, "Could not open the requested library: {}", e), DlError::MissingSymbol(s) => write!(f, "The requested symbol was missing: {}", s), } @@ -287,7 +301,7 @@ macro_rules! dlopen_external_library( impl $structname { pub unsafe fn open(name: &str) -> Result<$structname, $crate::DlError> { // we use it to ensure the 'static lifetime - use std::mem::transmute; + use core::mem::transmute; let lib = $crate::Library::new(name).map_err($crate::DlError::CantOpen)?; let s = $structname { $($($sname: {