Skip to content

Commit

Permalink
Merge pull request #271 from dtolnay/provider
Browse files Browse the repository at this point in the history
impl std::any::Provider for anyhow::Error
  • Loading branch information
dtolnay authored Sep 13, 2022
2 parents 916e22b + ee49ec0 commit 07acdac
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
8 changes: 7 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ compile_error! {
const PROBE: &str = r#"
#![feature(error_generic_member_access, provide_any)]
use std::any::Demand;
use std::any::{Demand, Provider};
use std::backtrace::{Backtrace, BacktraceStatus};
use std::error::Error;
use std::fmt::{self, Display};
Expand All @@ -39,6 +39,12 @@ const PROBE: &str = r#"
}
}
struct P;
impl Provider for P {
fn provide<'a>(&'a self, _demand: &mut Demand<'a>) {}
}
const _: fn() = || {
let backtrace: Backtrace = Backtrace::capture();
let status: BacktraceStatus = backtrace.status();
Expand Down
7 changes: 4 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,15 +522,16 @@ impl Error {
Some(addr.cast::<E>().deref_mut())
}
}
}

#[cfg(backtrace)]
impl std::any::Provider for Error {
// Called by thiserror when you have `#[source] anyhow::Error`. This provide
// implementation includes the anyhow::Error's Backtrace if any, unlike
// deref'ing to dyn Error where the provide implementation would include
// only the original error's Backtrace from before it got wrapped into an
// anyhow::Error.
#[cfg(backtrace)]
#[doc(hidden)]
pub fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
unsafe { ErrorImpl::provide(self.inner.by_ref(), demand) }
}
}
Expand Down

0 comments on commit 07acdac

Please sign in to comment.