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

Support no_std #64

Closed
wants to merge 2 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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ readme = "README.md"
travis-ci = { repository = "dtolnay/thiserror" }

[dependencies]
thiserror-impl = { version = "=1.0.11", path = "impl" }
thiserror-impl = { version = "=1.0.11", path = "impl", default-features = false }

[dev-dependencies]
anyhow = "1.0"
ref-cast = "1.0"
rustversion = "1.0"
trybuild = { version = "1.0.19", features = ["diff"] }

[features]
default = ["std"]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, this is technically a breaking change if anyone was already using thiserror with default-features = false, as they will now have reduced functionality.

I got burned by this when I added no_std to num crates -- rust-num/num#297. I've seen a lot of crates brush that off though, whether deliberate or not.

std = ["thiserror-impl/std"]

[workspace]
members = ["impl"]
4 changes: 4 additions & 0 deletions impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ travis-ci = { repository = "dtolnay/thiserror" }
proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0.11"

[features]
default = ["std"]
std = []
42 changes: 35 additions & 7 deletions impl/src/expand.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::ast::{Enum, Field, Input, Struct};
use proc_macro2::TokenStream;
use quote::{format_ident, quote, quote_spanned, ToTokens};
#[cfg(feature = "std")]
use quote::quote_spanned;
use quote::{format_ident, quote, ToTokens};
#[cfg(feature = "std")]
use syn::spanned::Spanned;
use syn::{DeriveInput, Member, PathArguments, Result, Type};

Expand All @@ -17,6 +20,7 @@ fn impl_struct(input: Struct) -> TokenStream {
let ty = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

#[cfg(feature = "std")]
let source_body = if input.attrs.transparent.is_some() {
let only_field = &input.fields[0].member;
Some(quote! {
Expand All @@ -36,6 +40,7 @@ fn impl_struct(input: Struct) -> TokenStream {
} else {
None
};
#[cfg(feature = "std")]
let source_method = source_body.map(|body| {
quote! {
fn source(&self) -> std::option::Option<&(dyn std::error::Error + 'static)> {
Expand All @@ -45,6 +50,7 @@ fn impl_struct(input: Struct) -> TokenStream {
}
});

#[cfg(feature = "std")]
let backtrace_method = input.backtrace_field().map(|backtrace_field| {
let backtrace = &backtrace_field.member;
let body = if let Some(source_field) = input.source_field() {
Expand Down Expand Up @@ -96,7 +102,10 @@ fn impl_struct(input: Struct) -> TokenStream {
let use_as_display = if display.has_bonus_display {
Some(quote! {
#[allow(unused_imports)]
use thiserror::private::{DisplayAsDisplay, PathAsDisplay};
use thiserror::private::DisplayAsDisplay;
#[cfg(feature = "std")]
#[allow(unused_imports)]
use thiserror::private::PathAsDisplay;
})
} else {
None
Expand Down Expand Up @@ -126,19 +135,26 @@ fn impl_struct(input: Struct) -> TokenStream {
let from = from_field.ty;
let body = from_initializer(from_field, backtrace_field);
quote! {
impl #impl_generics std::convert::From<#from> for #ty #ty_generics #where_clause {
impl #impl_generics core::convert::From<#from> for #ty #ty_generics #where_clause {
fn from(source: #from) -> Self {
#ty #body
}
}
}
});

quote! {
#[cfg(feature = "std")]
let error_impl = quote! {
impl #impl_generics std::error::Error for #ty #ty_generics #where_clause {
#source_method
#backtrace_method
}
};
#[cfg(not(feature = "std"))]
let error_impl = quote! {};

quote! {
#error_impl
#display_impl
#from_impl
}
Expand All @@ -148,6 +164,7 @@ fn impl_enum(input: Enum) -> TokenStream {
let ty = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();

#[cfg(feature = "std")]
let source_method = if input.has_source() {
let arms = input.variants.iter().map(|variant| {
let ident = &variant.ident;
Expand Down Expand Up @@ -186,6 +203,7 @@ fn impl_enum(input: Enum) -> TokenStream {
None
};

#[cfg(feature = "std")]
let backtrace_method = if input.has_backtrace() {
let arms = input.variants.iter().map(|variant| {
let ident = &variant.ident;
Expand Down Expand Up @@ -260,7 +278,10 @@ fn impl_enum(input: Enum) -> TokenStream {
}) {
Some(quote! {
#[allow(unused_imports)]
use thiserror::private::{DisplayAsDisplay, PathAsDisplay};
use thiserror::private::DisplayAsDisplay;
#[allow(unused_imports)]
#[cfg(feature = "std")]
use thiserror::private::PathAsDisplay;
})
} else {
None
Expand Down Expand Up @@ -309,19 +330,26 @@ fn impl_enum(input: Enum) -> TokenStream {
let from = from_field.ty;
let body = from_initializer(from_field, backtrace_field);
Some(quote! {
impl #impl_generics std::convert::From<#from> for #ty #ty_generics #where_clause {
impl #impl_generics core::convert::From<#from> for #ty #ty_generics #where_clause {
fn from(source: #from) -> Self {
#ty::#variant #body
}
}
})
});

quote! {
#[cfg(feature = "std")]
let error_impl = quote! {
impl #impl_generics std::error::Error for #ty #ty_generics #where_clause {
#source_method
#backtrace_method
}
};
#[cfg(not(feature = "std"))]
let error_impl = quote! {};

quote! {
#error_impl
#display_impl
#(#from_impls)*
}
Expand Down
9 changes: 8 additions & 1 deletion impl/src/prop.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use crate::ast::{Enum, Field, Struct, Variant};
use syn::{Member, Type};
#[cfg(feature = "std")]
use syn::Member;
use syn::Type;

impl Struct<'_> {
pub(crate) fn from_field(&self) -> Option<&Field> {
from_field(&self.fields)
}

#[cfg(feature = "std")]
pub(crate) fn source_field(&self) -> Option<&Field> {
source_field(&self.fields)
}
Expand All @@ -16,12 +19,14 @@ impl Struct<'_> {
}

impl Enum<'_> {
#[cfg(feature = "std")]
pub(crate) fn has_source(&self) -> bool {
self.variants
.iter()
.any(|variant| variant.source_field().is_some() || variant.attrs.transparent.is_some())
}

#[cfg(feature = "std")]
pub(crate) fn has_backtrace(&self) -> bool {
self.variants
.iter()
Expand All @@ -47,6 +52,7 @@ impl Variant<'_> {
from_field(&self.fields)
}

#[cfg(feature = "std")]
pub(crate) fn source_field(&self) -> Option<&Field> {
source_field(&self.fields)
}
Expand All @@ -71,6 +77,7 @@ fn from_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
None
}

#[cfg(feature = "std")]
fn source_field<'a, 'b>(fields: &'a [Field<'b>]) -> Option<&'a Field<'b>> {
for field in fields {
if field.attrs.from.is_some() || field.attrs.source.is_some() {
Expand Down
4 changes: 4 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt::Display;
#[cfg(feature = "std")]
use std::path::{self, Path, PathBuf};

pub trait DisplayAsDisplay {
Expand All @@ -11,16 +12,19 @@ impl<T: Display> DisplayAsDisplay for &T {
}
}

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

#[cfg(feature = "std")]
impl PathAsDisplay for Path {
fn as_display(&self) -> path::Display<'_> {
self.display()
}
}

#[cfg(feature = "std")]
impl PathAsDisplay for PathBuf {
fn as_display(&self) -> path::Display<'_> {
self.display()
Expand Down
19 changes: 18 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,20 @@
//!
//! [`anyhow`]: https://github.com/dtolnay/anyhow

// Always enabled to force devs to add use statements for things like Vec and String, even when
// working with std.
#![no_std]

#[cfg(not(feature = "std"))]
#[allow(unused_imports)]
#[macro_use]
extern crate alloc as std;
#[cfg(feature = "std")]
#[allow(unused_imports)]
#[macro_use]
extern crate std;

#[cfg(feature = "std")]
mod aserror;
mod display;

Expand All @@ -184,6 +198,9 @@ pub use thiserror_impl::*;
// Not public API.
#[doc(hidden)]
pub mod private {
#[cfg(feature = "std")]
pub use crate::aserror::AsDynError;
pub use crate::display::{DisplayAsDisplay, PathAsDisplay};
pub use crate::display::DisplayAsDisplay;
#[cfg(feature = "std")]
pub use crate::display::PathAsDisplay;
}