diff --git a/Cargo.toml b/Cargo.toml index 6505ef1..4437332 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 = [] diff --git a/impl/src/attr.rs b/impl/src/attr.rs index aa71665..4beb8c9 100644 --- a/impl/src/attr.rs +++ b/impl/src/attr.rs @@ -197,7 +197,7 @@ 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) }); } } @@ -205,6 +205,6 @@ impl ToTokens for Display<'_> { 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)); } } diff --git a/impl/src/expand.rs b/impl/src/expand.rs index ef8eaf3..ea9feff 100644 --- a/impl/src/expand.rs +++ b/impl/src/expand.rs @@ -26,17 +26,17 @@ fn impl_struct(input: Struct) -> TokenStream { let source_body = if input.attrs.transparent.is_some() { let only_field = &input.fields[0]; if only_field.contains_generic { - error_inferred_bounds.insert(only_field.ty, quote!(std::error::Error)); + error_inferred_bounds.insert(only_field.ty, quote!(thiserror::__private::Error)); } let member = &only_field.member; Some(quote! { - std::error::Error::source(self.#member.as_dyn_error()) + thiserror::__private::Error::source(self.#member.as_dyn_error()) }) } else if let Some(source_field) = input.source_field() { let source = &source_field.member; if source_field.contains_generic { let ty = unoptional_type(source_field.ty); - error_inferred_bounds.insert(ty, quote!(std::error::Error + 'static)); + error_inferred_bounds.insert(ty, quote!(thiserror::__private::Error + 'static)); } let asref = if type_is_option(source_field.ty) { Some(quote_spanned!(source.span()=> .as_ref()?)) @@ -45,14 +45,14 @@ fn impl_struct(input: Struct) -> TokenStream { }; let dyn_error = quote_spanned!(source.span()=> self.#source #asref.as_dyn_error()); Some(quote! { - std::option::Option::Some(#dyn_error) + ::core::option::Option::Some(#dyn_error) }) } else { None }; let source_method = source_body.map(|body| { quote! { - fn source(&self) -> std::option::Option<&(dyn std::error::Error + 'static)> { + fn source(&self) -> ::core::option::Option<&(dyn thiserror::__private::Error + 'static)> { use thiserror::__private::AsDynError; #body } @@ -66,7 +66,7 @@ fn impl_struct(input: Struct) -> TokenStream { let source = &source_field.member; let source_provide = if type_is_option(source_field.ty) { quote_spanned! {source.span()=> - if let std::option::Option::Some(source) = &self.#source { + if let ::core::option::Option::Some(source) = &self.#source { source.thiserror_provide(#demand); } } @@ -79,13 +79,13 @@ fn impl_struct(input: Struct) -> TokenStream { None } else if type_is_option(backtrace_field.ty) { Some(quote! { - if let std::option::Option::Some(backtrace) = &self.#backtrace { - #demand.provide_ref::(backtrace); + if let ::core::option::Option::Some(backtrace) = &self.#backtrace { + #demand.provide_ref::<::std::backtrace::Backtrace>(backtrace); } }) } else { Some(quote! { - #demand.provide_ref::(&self.#backtrace); + #demand.provide_ref::<::std::backtrace::Backtrace>(&self.#backtrace); }) }; quote! { @@ -95,17 +95,17 @@ fn impl_struct(input: Struct) -> TokenStream { } } else if type_is_option(backtrace_field.ty) { quote! { - if let std::option::Option::Some(backtrace) = &self.#backtrace { - #demand.provide_ref::(backtrace); + if let ::core::option::Option::Some(backtrace) = &self.#backtrace { + #demand.provide_ref::<::std::backtrace::Backtrace>(backtrace); } } } else { quote! { - #demand.provide_ref::(&self.#backtrace); + #demand.provide_ref::<::std::backtrace::Backtrace>(&self.#backtrace); } }; quote! { - fn provide<'_demand>(&'_demand self, #demand: &mut std::any::Demand<'_demand>) { + fn provide<'_demand>(&'_demand self, #demand: &mut ::core::any::Demand<'_demand>) { #body } } @@ -116,18 +116,11 @@ fn impl_struct(input: Struct) -> TokenStream { let only_field = &input.fields[0].member; display_implied_bounds.insert((0, Trait::Display)); Some(quote! { - std::fmt::Display::fmt(&self.#only_field, __formatter) + ::core::fmt::Display::fmt(&self.#only_field, __formatter) }) } else if let Some(display) = &input.attrs.display { display_implied_bounds = display.implied_bounds.clone(); - let use_as_display = if display.has_bonus_display { - Some(quote! { - #[allow(unused_imports)] - use thiserror::__private::{DisplayAsDisplay, PathAsDisplay}; - }) - } else { - None - }; + let use_as_display = impl_use_as_display(display.has_bonus_display); let pat = fields_pat(&input.fields); Some(quote! { #use_as_display @@ -149,9 +142,9 @@ fn impl_struct(input: Struct) -> TokenStream { let display_where_clause = display_inferred_bounds.augment_where_clause(input.generics); quote! { #[allow(unused_qualifications)] - impl #impl_generics std::fmt::Display for #ty #ty_generics #display_where_clause { + impl #impl_generics ::core::fmt::Display for #ty #ty_generics #display_where_clause { #[allow(clippy::used_underscore_binding)] - fn fmt(&self, __formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { #body } } @@ -164,7 +157,7 @@ fn impl_struct(input: Struct) -> TokenStream { let body = from_initializer(from_field, backtrace_field); quote! { #[allow(unused_qualifications)] - 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 { #[allow(deprecated)] fn from(source: #from) -> Self { #ty #body @@ -203,10 +196,10 @@ fn impl_enum(input: Enum) -> TokenStream { if variant.attrs.transparent.is_some() { let only_field = &variant.fields[0]; if only_field.contains_generic { - error_inferred_bounds.insert(only_field.ty, quote!(std::error::Error)); + error_inferred_bounds.insert(only_field.ty, quote!(thiserror::__private::Error)); } let member = &only_field.member; - let source = quote!(std::error::Error::source(transparent.as_dyn_error())); + let source = quote!(thiserror::__private::Error::source(transparent.as_dyn_error())); quote! { #ty::#ident {#member: transparent} => #source, } @@ -214,7 +207,7 @@ fn impl_enum(input: Enum) -> TokenStream { let source = &source_field.member; if source_field.contains_generic { let ty = unoptional_type(source_field.ty); - error_inferred_bounds.insert(ty, quote!(std::error::Error + 'static)); + error_inferred_bounds.insert(ty, quote!(thiserror::__private::Error + 'static)); } let asref = if type_is_option(source_field.ty) { Some(quote_spanned!(source.span()=> .as_ref()?)) @@ -224,16 +217,16 @@ fn impl_enum(input: Enum) -> TokenStream { let varsource = quote!(source); let dyn_error = quote_spanned!(source.span()=> #varsource #asref.as_dyn_error()); quote! { - #ty::#ident {#source: #varsource, ..} => std::option::Option::Some(#dyn_error), + #ty::#ident {#source: #varsource, ..} => ::core::option::Option::Some(#dyn_error), } } else { quote! { - #ty::#ident {..} => std::option::Option::None, + #ty::#ident {..} => ::core::option::Option::None, } } }); Some(quote! { - fn source(&self) -> std::option::Option<&(dyn std::error::Error + 'static)> { + fn source(&self) -> ::core::option::Option<&(dyn thiserror::__private::Error + 'static)> { use thiserror::__private::AsDynError; #[allow(deprecated)] match self { @@ -258,7 +251,7 @@ fn impl_enum(input: Enum) -> TokenStream { let varsource = quote!(source); let source_provide = if type_is_option(source_field.ty) { quote_spanned! {source.span()=> - if let std::option::Option::Some(source) = #varsource { + if let ::core::option::Option::Some(source) = #varsource { source.thiserror_provide(#demand); } } @@ -269,13 +262,13 @@ fn impl_enum(input: Enum) -> TokenStream { }; let self_provide = if type_is_option(backtrace_field.ty) { quote! { - if let std::option::Option::Some(backtrace) = backtrace { - #demand.provide_ref::(backtrace); + if let ::core::option::Option::Some(backtrace) = backtrace { + #demand.provide_ref::<::std::backtrace::Backtrace>(backtrace); } } } else { quote! { - #demand.provide_ref::(backtrace); + #demand.provide_ref::<::std::backtrace::Backtrace>(backtrace); } }; quote! { @@ -297,7 +290,7 @@ fn impl_enum(input: Enum) -> TokenStream { let varsource = quote!(source); let source_provide = if type_is_option(source_field.ty) { quote_spanned! {backtrace.span()=> - if let std::option::Option::Some(source) = #varsource { + if let ::core::option::Option::Some(source) = #varsource { source.thiserror_provide(#demand); } } @@ -317,13 +310,13 @@ fn impl_enum(input: Enum) -> TokenStream { let backtrace = &backtrace_field.member; let body = if type_is_option(backtrace_field.ty) { quote! { - if let std::option::Option::Some(backtrace) = backtrace { - #demand.provide_ref::(backtrace); + if let ::core::option::Option::Some(backtrace) = backtrace { + #demand.provide_ref::<::std::backtrace::Backtrace>(backtrace); } } } else { quote! { - #demand.provide_ref::(backtrace); + #demand.provide_ref::<::std::backtrace::Backtrace>(backtrace); } }; quote! { @@ -338,7 +331,7 @@ fn impl_enum(input: Enum) -> TokenStream { } }); Some(quote! { - fn provide<'_demand>(&'_demand self, #demand: &mut std::any::Demand<'_demand>) { + fn provide<'_demand>(&'_demand self, #demand: &mut ::core::any::Demand<'_demand>) { #[allow(deprecated)] match self { #(#arms)* @@ -351,19 +344,13 @@ fn impl_enum(input: Enum) -> TokenStream { let display_impl = if input.has_display() { let mut display_inferred_bounds = InferredBounds::new(); - let use_as_display = if input.variants.iter().any(|v| { + let use_as_display = input.variants.iter().any(|v| { v.attrs .display .as_ref() .map_or(false, |display| display.has_bonus_display) - }) { - Some(quote! { - #[allow(unused_imports)] - use thiserror::__private::{DisplayAsDisplay, PathAsDisplay}; - }) - } else { - None - }; + }); + let use_as_display = impl_use_as_display(use_as_display); let void_deref = if input.variants.is_empty() { Some(quote!(*)) } else { @@ -382,7 +369,7 @@ fn impl_enum(input: Enum) -> TokenStream { Member::Unnamed(index) => format_ident!("_{}", index), }; display_implied_bounds.insert((0, Trait::Display)); - quote!(std::fmt::Display::fmt(#only_field, __formatter)) + quote!(::core::fmt::Display::fmt(#only_field, __formatter)) } }; for (field, bound) in display_implied_bounds { @@ -401,8 +388,8 @@ fn impl_enum(input: Enum) -> TokenStream { let display_where_clause = display_inferred_bounds.augment_where_clause(input.generics); Some(quote! { #[allow(unused_qualifications)] - impl #impl_generics std::fmt::Display for #ty #ty_generics #display_where_clause { - fn fmt(&self, __formatter: &mut std::fmt::Formatter) -> std::fmt::Result { + impl #impl_generics ::core::fmt::Display for #ty #ty_generics #display_where_clause { + fn fmt(&self, __formatter: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { #use_as_display #[allow(unused_variables, deprecated, clippy::used_underscore_binding)] match #void_deref self { @@ -423,7 +410,7 @@ fn impl_enum(input: Enum) -> TokenStream { let body = from_initializer(from_field, backtrace_field); Some(quote! { #[allow(unused_qualifications)] - 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 { #[allow(deprecated)] fn from(source: #from) -> Self { #ty::#variant #body @@ -466,10 +453,21 @@ fn fields_pat(fields: &[Field]) -> TokenStream { } } +fn impl_use_as_display(needs_as_display: bool) -> Option { + if needs_as_display { + Some(quote! { + #[allow(unused_imports)] + use thiserror::__private::AsDisplay as _; + }) + } else { + None + } +} + fn from_initializer(from_field: &Field, backtrace_field: Option<&Field>) -> TokenStream { let from_member = &from_field.member; let some_source = if type_is_option(from_field.ty) { - quote!(std::option::Option::Some(source)) + quote!(::core::option::Option::Some(source)) } else { quote!(source) }; @@ -477,11 +475,11 @@ fn from_initializer(from_field: &Field, backtrace_field: Option<&Field>) -> Toke let backtrace_member = &backtrace_field.member; if type_is_option(backtrace_field.ty) { quote! { - #backtrace_member: std::option::Option::Some(std::backtrace::Backtrace::capture()), + #backtrace_member: ::core::option::Option::Some(::std::backtrace::Backtrace::capture()), } } else { quote! { - #backtrace_member: std::convert::From::from(std::backtrace::Backtrace::capture()), + #backtrace_member: ::core::convert::From::from(::std::backtrace::Backtrace::capture()), } } }); @@ -539,7 +537,7 @@ fn spanned_error_trait(input: &DeriveInput) -> TokenStream { }; let first_span = vis_span.unwrap_or(data_span); let last_span = input.ident.span(); - let path = quote_spanned!(first_span=> std::error::); + let path = quote_spanned!(first_span=> thiserror::__private::); let error = quote_spanned!(last_span=> Error); quote!(#path #error) } diff --git a/src/aserror.rs b/src/aserror.rs index 5fea84e..e12d4c3 100644 --- a/src/aserror.rs +++ b/src/aserror.rs @@ -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); diff --git a/src/display.rs b/src/display.rs index 0eb0dd9..40f1b54 100644 --- a/src/display.rs +++ b/src/display.rs @@ -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 DisplayAsDisplay for &T { - fn as_display(&self) -> Self { +impl 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) + } } } diff --git a/src/lib.rs b/src/lib.rs index 13e1f91..4c1c9fb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; @@ -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; } diff --git a/src/provide.rs b/src/provide.rs index 524e743..3f772bd 100644 --- a/src/provide.rs +++ b/src/provide.rs @@ -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>); diff --git a/tests/ui/no-display.stderr b/tests/ui/no-display.stderr index 76818e1..26dc842 100644 --- a/tests/ui/no-display.stderr +++ b/tests/ui/no-display.stderr @@ -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 |