diff --git a/src/explain.rs b/src/explain.rs index 894a090..83d6d7e 100644 --- a/src/explain.rs +++ b/src/explain.rs @@ -3,20 +3,17 @@ use std::sync::Arc; use crate::{How, IntoContext, Detail}; -pub trait Explain { +pub trait Explain: Sized { type Output; #[track_caller] #[must_use] fn context(self, context: impl IntoContext) -> Self::Output; -} -impl Explain for How { - type Output = Self; - - #[track_caller] #[inline] - fn context(mut self, context: impl IntoContext) -> Self { + #[track_caller] + #[must_use] + fn frame(self, context: impl IntoContext) -> Self::Output { let mut context = context.into_context(); context.extra.reserve(if cfg!(feature = "extra-backtrace") { @@ -27,7 +24,18 @@ impl Explain for How { context.extra.push(Detail::Location(*Location::caller())); #[cfg(feature = "extra-backtrace")] context.extra.push(Detail::backtrace()); - self.push_context(context); + + self.context(context) + } +} + +impl Explain for How { + type Output = Self; + + #[inline(always)] + #[track_caller] + fn context(mut self, context: impl IntoContext) -> Self { + self.push_context(context.into_context()); self } } @@ -70,20 +78,20 @@ where { type Output = How; + #[inline] #[track_caller] fn context(self, context: impl IntoContext) -> Self::Output { - How::new(self.into_context()) - .context(context) + How::new(self).context(context) } } impl Explain for Arc { type Output = How; + #[inline] #[track_caller] fn context(self, context: impl IntoContext) -> Self::Output { - How::new(self.into_context()) - .context(context) + How::new(self).context(context) } } diff --git a/src/how.rs b/src/how.rs index af1b75d..f6dbaa0 100644 --- a/src/how.rs +++ b/src/how.rs @@ -30,7 +30,7 @@ impl How { location, context: Vec::with_capacity(4), })) - .context(context); + .frame(context); #[cfg(all(feature = "backtrace", not(feature = "extra-backtrace")))] how.top_mut().extra.push(Detail::backtrace()); how