Add a `frame` method to Explain

This commit is contained in:
Michael Pfaff 2023-07-02 15:19:30 -04:00
parent a888990a2a
commit 60b4547267
Signed by: michael
GPG Key ID: CF402C4A012AA9D4
2 changed files with 21 additions and 13 deletions

View File

@ -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)
}
}

View File

@ -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