Skip to content

Commit

Permalink
Optionally support no_std (#31)
Browse files Browse the repository at this point in the history
Add a "std" feature which is enabled by default, and enable `no_std`
when it is not enabled. Disable all `Display` and `Debug` features
in `no_std` mode.

My specific use case for this is [rsix](https://crates.io/crates/rsix),
which has a no_std branch, and which uses the `errno` crate in its
`libc`-using backend.

Fixes #18.
  • Loading branch information
sunfishcode authored Oct 25, 2021
1 parent 6d8f379 commit ffbba25
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ libc = "0.2"
[target.'cfg(target_os="hermit")'.dependencies]
libc = "0.2"

[features]
default = ["std"]
std = []

[badges]
travis-ci = { repository = "lambda-fairy/rust-errno" }
16 changes: 16 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//! ```

#![cfg_attr(target_os = "wasi", feature(thread_local))]
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(unix)] extern crate libc;
#[cfg(windows)] extern crate winapi;
Expand All @@ -31,8 +32,11 @@
#[cfg_attr(target_os = "hermit", path = "hermit.rs")]
mod sys;

#[cfg(feature = "std")]
use std::fmt;
#[cfg(feature = "std")]
use std::io;
#[cfg(feature = "std")]
use std::error::Error;

/// Wraps a platform-specific error code.
Expand All @@ -46,6 +50,7 @@ use std::error::Error;
#[derive(Copy, Clone, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub struct Errno(pub i32);

#[cfg(feature = "std")]
impl fmt::Debug for Errno {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
sys::with_description(*self, |desc| {
Expand All @@ -57,6 +62,7 @@ impl fmt::Debug for Errno {
}
}

#[cfg(feature = "std")]
impl fmt::Display for Errno {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
sys::with_description(*self, |desc| match desc {
Expand All @@ -74,6 +80,7 @@ impl Into<i32> for Errno {
}
}

#[cfg(feature = "std")]
impl Error for Errno {
// TODO: Remove when MSRV >= 1.27
#[allow(deprecated)]
Expand All @@ -82,6 +89,7 @@ impl Error for Errno {
}
}

#[cfg(feature = "std")]
impl From<Errno> for io::Error {
fn from(errno: Errno) -> Self {
io::Error::from_raw_os_error(errno.0)
Expand All @@ -102,9 +110,16 @@ pub fn set_errno(err: Errno) {
fn it_works() {
let x = errno();
set_errno(x);
}

#[cfg(feature = "std")]
#[test]
fn it_works_with_to_string() {
let x = errno();
let _ = x.to_string();
}

#[cfg(feature = "std")]
#[test]
fn check_description() {
let expect = if cfg!(windows) {
Expand All @@ -125,6 +140,7 @@ fn check_description() {
format!("Errno {{ code: 1, description: Some({:?}) }}", expect));
}

#[cfg(feature = "std")]
#[test]
fn check_error_into_errno() {
const ERROR_CODE: i32 = 1;
Expand Down
8 changes: 7 additions & 1 deletion src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[cfg(feature = "std")]
use std::ffi::CStr;
use libc::{self, c_char, c_int};
use libc::c_int;
#[cfg(feature = "std")]
use libc::{self, c_char};
#[cfg(target_os = "dragonfly")]
use errno_dragonfly::errno_location;

use Errno;

#[cfg(feature = "std")]
pub fn with_description<F, T>(err: Errno, callback: F) -> T where
F: FnOnce(Result<&str, Errno>) -> T
{
Expand All @@ -35,6 +39,7 @@ pub fn with_description<F, T>(err: Errno, callback: F) -> T where
callback(Ok(&String::from_utf8_lossy(c_str.to_bytes())))
}

#[cfg(feature = "std")]
pub const STRERROR_NAME: &'static str = "strerror_r";

pub fn errno() -> Errno {
Expand Down Expand Up @@ -67,6 +72,7 @@ extern {
link_name = "__errno_location")]
fn errno_location() -> *mut c_int;

#[cfg(feature = "std")]
#[cfg_attr(target_os = "linux", link_name = "__xpg_strerror_r")]
fn strerror_r(errnum: c_int, buf: *mut c_char,
buflen: libc::size_t) -> c_int;
Expand Down
8 changes: 7 additions & 1 deletion src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[cfg(feature = "std")]
use std::ffi::CStr;
use libc::{self, c_char, c_int};
use libc::c_int;
#[cfg(feature = "std")]
use libc::{self, c_char};

use Errno;

#[cfg(feature = "std")]
pub fn with_description<F, T>(err: Errno, callback: F) -> T where
F: FnOnce(Result<&str, Errno>) -> T
{
Expand All @@ -33,6 +37,7 @@ pub fn with_description<F, T>(err: Errno, callback: F) -> T where
callback(Ok(&String::from_utf8_lossy(c_str.to_bytes())))
}

#[cfg(feature = "std")]
pub const STRERROR_NAME: &'static str = "strerror_r";

pub fn errno() -> Errno {
Expand All @@ -54,6 +59,7 @@ extern {
#[link_name = "errno"]
static mut libc_errno: c_int;

#[cfg(feature = "std")]
fn strerror_r(errnum: c_int, buf: *mut c_char,
buflen: libc::size_t) -> c_int;
}

0 comments on commit ffbba25

Please sign in to comment.