Skip to content

Commit 1e50471

Browse files
add no-std support
1 parent f4ec9e7 commit 1e50471

File tree

12 files changed

+852
-73
lines changed

12 files changed

+852
-73
lines changed

.github/workflows/libloading.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ jobs:
3333
# pwsh.exe drops quotes kekw. https://stackoverflow.com/a/59036879
3434
shell: bash
3535

36+
no-std-build-test:
37+
runs-on: ubuntu-latest
38+
# This will rebuild core and alloc, which will re-mangle symbols in alloc,
39+
# which then will break the pre-compiled std. This will cause the program to fail to link.
40+
# This test does not test anything in an actual no-std environment only that the library compiles on it.
41+
# We test all no-std only functions together with the ordinary functions.
42+
steps:
43+
- uses: actions/checkout@v4
44+
- name: Add nightly toolchain
45+
run: rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu
46+
- name: Compile without the standard library
47+
run: cargo clean && cargo +nightly build -Zbuild-std=core,alloc --no-default-features
48+
3649
windows-test:
3750
runs-on: windows-latest
3851
strategy:

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ categories = ["api-bindings"]
1515
rust-version = "1.71.0"
1616
edition = "2015"
1717

18+
[features]
19+
default = ["std"]
20+
std = []
21+
1822
[target.'cfg(windows)'.dependencies.windows-link]
1923
version = "0.2"
2024

src/error.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use std::ffi::{CStr, CString};
1+
use alloc::ffi::CString;
2+
use core::ffi::CStr;
23

34
/// A `dlerror` error.
45
pub struct DlDescription(pub(crate) CString);
56

6-
impl std::fmt::Debug for DlDescription {
7-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
8-
std::fmt::Debug::fmt(&self.0, f)
7+
impl core::fmt::Debug for DlDescription {
8+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
9+
core::fmt::Debug::fmt(&self.0, f)
910
}
1011
}
1112

@@ -16,11 +17,15 @@ impl From<&CStr> for DlDescription {
1617
}
1718

1819
/// A Windows API error.
20+
#[cfg(not(feature = "std"))]
21+
pub struct WindowsError(pub(crate) i32);
22+
23+
#[cfg(feature = "std")]
1924
pub struct WindowsError(pub(crate) std::io::Error);
2025

21-
impl std::fmt::Debug for WindowsError {
22-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23-
std::fmt::Debug::fmt(&self.0, f)
26+
impl core::fmt::Debug for WindowsError {
27+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
28+
core::fmt::Debug::fmt(&self.0, f)
2429
}
2530
}
2631

@@ -82,17 +87,28 @@ pub enum Error {
8287
/// Could not create a new CString.
8388
CreateCString {
8489
/// The source error.
85-
source: std::ffi::NulError,
90+
source: alloc::ffi::NulError,
8691
},
8792
/// Could not create a new CString from bytes with trailing null.
8893
CreateCStringWithTrailing {
8994
/// The source error.
90-
source: std::ffi::FromBytesWithNulError,
95+
source: core::ffi::FromBytesWithNulError,
9196
},
9297
}
9398

94-
impl std::error::Error for Error {
95-
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
99+
impl core::error::Error for Error {
100+
#[cfg(not(feature = "std"))]
101+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
102+
use Error::*;
103+
match *self {
104+
CreateCString { ref source } => Some(source),
105+
CreateCStringWithTrailing { ref source } => Some(source),
106+
_ => None,
107+
}
108+
}
109+
110+
#[cfg(feature = "std")]
111+
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
96112
use Error::*;
97113
match *self {
98114
CreateCString { ref source } => Some(source),
@@ -106,8 +122,8 @@ impl std::error::Error for Error {
106122
}
107123
}
108124

109-
impl std::fmt::Display for Error {
110-
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
125+
impl core::fmt::Display for Error {
126+
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
111127
use Error::*;
112128
match *self {
113129
DlOpen { ref desc } => write!(f, "{}", desc.0.to_string_lossy()),

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
deny(missing_docs, clippy::all, unreachable_pub, unused)
4141
)]
4242
#![cfg_attr(libloading_docs, feature(doc_cfg))]
43+
#![no_std]
44+
45+
extern crate alloc;
46+
#[cfg(feature = "std")]
47+
extern crate std;
4348

4449
pub mod changelog;
4550
mod error;
@@ -51,7 +56,10 @@ mod util;
5156
pub use self::error::Error;
5257
#[cfg(any(unix, windows, libloading_docs))]
5358
pub use self::safe::{Library, Symbol};
59+
60+
#[cfg(feature = "std")]
5461
use std::env::consts::{DLL_PREFIX, DLL_SUFFIX};
62+
#[cfg(feature = "std")]
5563
use std::ffi::{OsStr, OsString};
5664

5765
/// Converts a library name to a filename generally appropriate for use on the system.
@@ -71,6 +79,7 @@ use std::ffi::{OsStr, OsString};
7179
/// Library::new(library_filename("LLVM"))
7280
/// };
7381
/// ```
82+
#[cfg(feature = "std")]
7483
pub fn library_filename<S: AsRef<OsStr>>(name: S) -> OsString {
7584
let name = name.as_ref();
7685
let mut string = OsString::with_capacity(name.len() + DLL_PREFIX.len() + DLL_SUFFIX.len());

src/os/unix/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::os::raw::c_int;
1+
use core::ffi::c_int;
22

33
/// Perform lazy binding.
44
///

0 commit comments

Comments
 (0)