Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no_std support #244

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ members = ["impl"]
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]
rustdoc-args = ["--generate-link-to-definition"]

[features]
no_std = []
4 changes: 2 additions & 2 deletions impl/src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ impl ToTokens for Display<'_> {
let fmt = &self.fmt;
let args = &self.args;
tokens.extend(quote! {
std::write!(__formatter, #fmt #args)
::core::write!(__formatter, #fmt #args)
});
}
}

impl ToTokens for Trait {
fn to_tokens(&self, tokens: &mut TokenStream) {
let trait_name = format_ident!("{}", format!("{:?}", self));
tokens.extend(quote!(std::fmt::#trait_name));
tokens.extend(quote!(::core::fmt::#trait_name));
}
}
112 changes: 55 additions & 57 deletions impl/src/expand.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/aserror.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::error::Error;
use std::panic::UnwindSafe;
use crate::__private::Error;
use core::panic::UnwindSafe;

pub trait AsDynError<'a>: Sealed {
fn as_dyn_error(&self) -> &(dyn Error + 'a);
Expand Down
61 changes: 45 additions & 16 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,57 @@
use std::fmt::Display;
use std::path::{self, Path, PathBuf};
use core::fmt::Display;

pub trait DisplayAsDisplay {
fn as_display(&self) -> Self;
pub trait AsDisplay {
type Target: Display + ?Sized;

fn as_display(&self) -> &Self::Target;
}

impl<T: Display> DisplayAsDisplay for &T {
fn as_display(&self) -> Self {
impl<T: Display> AsDisplay for &T {
type Target = T;

fn as_display(&self) -> &Self::Target {
self
}
}

pub trait PathAsDisplay {
fn as_display(&self) -> path::Display<'_>;
}
#[cfg(not(feature = "no_std"))]
mod path {
use std::path::{Path, PathBuf};

impl PathAsDisplay for Path {
fn as_display(&self) -> path::Display<'_> {
self.display()
impl super::AsDisplay for Path {
type Target = PathDisplay;

#[inline(always)]
fn as_display(&self) -> &Self::Target {
PathDisplay::new(self)
}
}

impl super::AsDisplay for PathBuf {
type Target = PathDisplay;

#[inline(always)]
fn as_display(&self) -> &Self::Target {
PathDisplay::new(self.as_path())
}
}

#[repr(transparent)]
pub struct PathDisplay(Path);

impl PathDisplay {
#[inline(always)]
fn new(path: &Path) -> &Self {
// SAFETY: PathDisplay is repr(transparent) so casting pointers
// between it and its payload is safe.
unsafe { &*(path as *const Path as *const Self) }
}
}
}

impl PathAsDisplay for PathBuf {
fn as_display(&self) -> path::Display<'_> {
self.display()
impl core::fmt::Display for PathDisplay {
#[inline(always)]
fn fmt(&self, fmtr: &mut core::fmt::Formatter) -> core::fmt::Result {
self.0.display().fmt(fmtr)
}
}
}
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@
clippy::wildcard_imports,
)]
#![cfg_attr(provide_any, feature(provide_any))]
#![cfg_attr(feature = "no_std", feature(error_in_core))]
#![cfg_attr(feature = "no_std", no_std)]

mod aserror;
mod display;
Expand All @@ -249,7 +251,11 @@ pub use thiserror_impl::*;
#[doc(hidden)]
pub mod __private {
pub use crate::aserror::AsDynError;
pub use crate::display::{DisplayAsDisplay, PathAsDisplay};
pub use crate::display::AsDisplay;
#[cfg(provide_any)]
pub use crate::provide::ThiserrorProvide;
#[cfg(feature = "no_std")]
pub use core::error::Error;
#[cfg(not(feature = "no_std"))]
pub use std::error::Error;
}
2 changes: 1 addition & 1 deletion src/provide.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::any::{Demand, Provider};
use core::any::{Demand, Provider};

pub trait ThiserrorProvide: Sealed {
fn thiserror_provide<'a>(&'a self, demand: &mut Demand<'a>);
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/no-display.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ error[E0599]: the method `as_display` exists for reference `&NoDisplay`, but its
|
= note: the following trait bounds were not satisfied:
`NoDisplay: std::fmt::Display`
which is required by `&NoDisplay: DisplayAsDisplay`
which is required by `&NoDisplay: AsDisplay`
note: the trait `std::fmt::Display` must be implemented
--> $RUST/core/src/fmt/mod.rs
|
Expand Down