Add a `frame` method to Explain
This commit is contained in:
parent
a888990a2a
commit
60b4547267
|
@ -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<dyn std::error::Error + Send + Sync> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue