Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
24 changes: 19 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
}
Expand Down Expand Up @@ -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: {
Expand Down