diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/chain.rs | 1 | ||||
-rw-r--r-- | src/error.rs | 19 | ||||
-rw-r--r-- | src/kind.rs | 3 | ||||
-rw-r--r-- | src/lib.rs | 12 |
4 files changed, 26 insertions, 9 deletions
diff --git a/src/chain.rs b/src/chain.rs index 207e4f0..f7baff2 100644 --- a/src/chain.rs +++ b/src/chain.rs @@ -24,6 +24,7 @@ pub(crate) enum ChainState<'a> { } impl<'a> Chain<'a> { + #[cold] pub fn new(head: &'a (dyn StdError + 'static)) -> Self { Chain { state: ChainState::Linked { next: Some(head) }, diff --git a/src/error.rs b/src/error.rs index f4f5bc2..3fa0835 100644 --- a/src/error.rs +++ b/src/error.rs @@ -25,6 +25,7 @@ impl Error { /// created here to ensure that a backtrace exists. #[cfg(feature = "std")] #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] + #[cold] pub fn new<E>(error: E) -> Self where E: StdError + Send + Sync + 'static, @@ -70,6 +71,7 @@ impl Error { /// .await /// } /// ``` + #[cold] pub fn msg<M>(message: M) -> Self where M: Display + Debug + Send + Sync + 'static, @@ -78,6 +80,7 @@ impl Error { } #[cfg(feature = "std")] + #[cold] pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self where E: StdError + Send + Sync + 'static, @@ -100,6 +103,7 @@ impl Error { unsafe { Error::construct(error, vtable, backtrace) } } + #[cold] pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self where M: Display + Debug + Send + Sync + 'static, @@ -125,6 +129,7 @@ impl Error { unsafe { Error::construct(error, vtable, backtrace) } } + #[cold] pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self where M: Display + Send + Sync + 'static, @@ -151,6 +156,7 @@ impl Error { } #[cfg(feature = "std")] + #[cold] pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self where C: Display + Send + Sync + 'static, @@ -177,6 +183,7 @@ impl Error { } #[cfg(feature = "std")] + #[cold] pub(crate) fn from_boxed( error: Box<dyn StdError + Send + Sync>, backtrace: Option<Backtrace>, @@ -207,6 +214,7 @@ impl Error { // // Unsafe because the given vtable must have sensible behavior on the error // value of type E. + #[cold] unsafe fn construct<E>( error: E, vtable: &'static ErrorVTable, @@ -284,6 +292,7 @@ impl Error { /// }) /// } /// ``` + #[cold] pub fn context<C>(self, context: C) -> Self where C: Display + Send + Sync + 'static, @@ -373,6 +382,7 @@ impl Error { /// ``` #[cfg(feature = "std")] #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))] + #[cold] pub fn chain(&self) -> Chain { unsafe { ErrorImpl::chain(self.inner.by_ref()) } } @@ -515,6 +525,7 @@ impl<E> From<E> for Error where E: StdError + Send + Sync + 'static, { + #[cold] fn from(error: E) -> Self { let backtrace = backtrace_if_absent!(error); Error::from_std(error, backtrace) @@ -741,12 +752,11 @@ unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option where C: 'static, { + let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref(); if TypeId::of::<C>() == target { - let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref(); Some(Ref::new(&unerased._object.context).cast::<()>()) } else { // Recurse down the context chain per the inner error's vtable. - let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref(); let source = &unerased._object.error; (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target) } @@ -758,12 +768,11 @@ unsafe fn context_chain_downcast_mut<C>(e: Mut<ErrorImpl>, target: TypeId) -> Op where C: 'static, { + let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut(); if TypeId::of::<C>() == target { - let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut(); Some(Mut::new(&mut unerased._object.context).cast::<()>()) } else { // Recurse down the context chain per the inner error's vtable. - let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut(); let source = &mut unerased._object.error; (vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target) } @@ -881,6 +890,7 @@ impl ErrorImpl { .expect("backtrace capture failed") } + #[cold] pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain { Chain::new(Self::error(this)) } @@ -919,6 +929,7 @@ where } impl From<Error> for Box<dyn StdError + Send + Sync + 'static> { + #[cold] fn from(error: Error) -> Self { let outer = ManuallyDrop::new(error); unsafe { diff --git a/src/kind.rs b/src/kind.rs index eb8d604..5985705 100644 --- a/src/kind.rs +++ b/src/kind.rs @@ -62,6 +62,7 @@ pub trait AdhocKind: Sized { impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {} impl Adhoc { + #[cold] pub fn new<M>(self, message: M) -> Error where M: Display + Debug + Send + Sync + 'static, @@ -82,6 +83,7 @@ pub trait TraitKind: Sized { impl<E> TraitKind for E where E: Into<Error> {} impl Trait { + #[cold] pub fn new<E>(self, error: E) -> Error where E: Into<Error>, @@ -106,6 +108,7 @@ impl BoxedKind for Box<dyn StdError + Send + Sync> {} #[cfg(feature = "std")] impl Boxed { + #[cold] pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Error { let backtrace = backtrace_if_absent!(error); Error::from_boxed(error, backtrace) @@ -128,10 +128,11 @@ //! # ; //! ``` //! -//! - If using the nightly channel, a backtrace is captured and printed with the -//! error if the underlying error type does not already provide its own. In -//! order to see backtraces, they must be enabled through the environment -//! variables described in [`std::backtrace`]: +//! - If using the nightly channel, or stable with `features = ["backtrace"]`, a +//! backtrace is captured and printed with the error if the underlying error +//! type does not already provide its own. In order to see backtraces, they +//! must be enabled through the environment variables described in +//! [`std::backtrace`]: //! //! - If you want panics and errors to both have backtraces, set //! `RUST_BACKTRACE=1`; @@ -209,7 +210,7 @@ //! will require an explicit `.map_err(Error::msg)` when working with a //! non-Anyhow error type inside a function that returns Anyhow's error type. -#![doc(html_root_url = "https://docs.rs/anyhow/1.0.40")] +#![doc(html_root_url = "https://docs.rs/anyhow/1.0.44")] #![cfg_attr(backtrace, feature(backtrace))] #![cfg_attr(doc_cfg, feature(doc_cfg))] #![cfg_attr(not(feature = "std"), no_std)] @@ -625,6 +626,7 @@ pub mod private { pub use crate::kind::BoxedKind; } + #[cold] pub fn new_adhoc<M>(message: M) -> Error where M: Display + Debug + Send + Sync + 'static, |