Skip to content

Commit 4687902

Browse files
committed
Add no_std support
Introduce no_std feature which, when enabled, uses core::error::Error trait rather than std::error::Error and thus makes it possible to use the crate in no_std builds. Importantly though, using core::error module requires a unstable error_in_core feature thus enabling no_std requires building with nightly compiler. Furthermore, enabling `no_std` disables handling of Backtrace fields and support for displaying paths. Since those types aren’t available in no_std environments this shouldn’t be an issue.
1 parent e3d16e5 commit 4687902

File tree

5 files changed

+41
-29
lines changed

5 files changed

+41
-29
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ members = ["impl"]
2626
[package.metadata.docs.rs]
2727
targets = ["x86_64-unknown-linux-gnu"]
2828
rustdoc-args = ["--generate-link-to-definition"]
29+
30+
[features]
31+
no_std = []

src/aserror.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::__private::Error;
2-
use std::panic::UnwindSafe;
2+
use core::panic::UnwindSafe;
33

44
pub trait AsDynError<'a>: Sealed {
55
fn as_dyn_error(&self) -> &(dyn Error + 'a);

src/display.rs

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use std::fmt::Display;
2-
use std::path::{Path, PathBuf};
1+
use core::fmt::Display;
32

43
pub trait AsDisplay {
54
type Target: Display + ?Sized;
@@ -15,39 +14,44 @@ impl<T: Display> AsDisplay for &T {
1514
}
1615
}
1716

18-
impl AsDisplay for Path {
19-
type Target = PathDisplay;
17+
#[cfg(not(feature = "no_std"))]
18+
mod path {
19+
use std::path::{Path, PathBuf};
2020

21-
#[inline(always)]
22-
fn as_display(&self) -> &Self::Target {
23-
PathDisplay::new(self)
21+
impl super::AsDisplay for Path {
22+
type Target = PathDisplay;
23+
24+
#[inline(always)]
25+
fn as_display(&self) -> &Self::Target {
26+
PathDisplay::new(self)
27+
}
2428
}
25-
}
2629

27-
impl AsDisplay for PathBuf {
28-
type Target = PathDisplay;
30+
impl super::AsDisplay for PathBuf {
31+
type Target = PathDisplay;
2932

30-
#[inline(always)]
31-
fn as_display(&self) -> &Self::Target {
32-
PathDisplay::new(self.as_path())
33+
#[inline(always)]
34+
fn as_display(&self) -> &Self::Target {
35+
PathDisplay::new(self.as_path())
36+
}
3337
}
34-
}
3538

36-
#[repr(transparent)]
37-
pub struct PathDisplay(Path);
39+
#[repr(transparent)]
40+
pub struct PathDisplay(Path);
3841

39-
impl PathDisplay {
40-
#[inline(always)]
41-
fn new(path: &Path) -> &Self {
42-
// SAFETY: PathDisplay is repr(transparent) so casting pointers between
43-
// it and its payload is safe.
44-
unsafe { &*(path as *const Path as *const Self) }
42+
impl PathDisplay {
43+
#[inline(always)]
44+
fn new(path: &Path) -> &Self {
45+
// SAFETY: PathDisplay is repr(transparent) so casting pointers
46+
// between it and its payload is safe.
47+
unsafe { &*(path as *const Path as *const Self) }
48+
}
4549
}
46-
}
4750

48-
impl Display for PathDisplay {
49-
#[inline(always)]
50-
fn fmt(&self, fmtr: &mut std::fmt::Formatter) -> std::fmt::Result {
51-
self.0.display().fmt(fmtr)
51+
impl core::fmt::Display for PathDisplay {
52+
#[inline(always)]
53+
fn fmt(&self, fmtr: &mut core::fmt::Formatter) -> core::fmt::Result {
54+
self.0.display().fmt(fmtr)
55+
}
5256
}
5357
}

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@
237237
clippy::wildcard_imports,
238238
)]
239239
#![cfg_attr(provide_any, feature(provide_any))]
240+
#![cfg_attr(feature = "no_std", feature(error_in_core))]
241+
#![cfg_attr(feature = "no_std", no_std)]
240242

241243
mod aserror;
242244
mod display;
@@ -252,5 +254,8 @@ pub mod __private {
252254
pub use crate::display::AsDisplay;
253255
#[cfg(provide_any)]
254256
pub use crate::provide::ThiserrorProvide;
257+
#[cfg(feature = "no_std")]
258+
pub use core::error::Error;
259+
#[cfg(not(feature = "no_std"))]
255260
pub use std::error::Error;
256261
}

src/provide.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::any::{Demand, Provider};
1+
use core::any::{Demand, Provider};
22

33
pub trait ThiserrorProvide: Sealed {
44
fn thiserror_provide<'a>(&'a self, demand: &mut Demand<'a>);

0 commit comments

Comments
 (0)