Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl std::any::Provider for anyhow::Error #271

Merged
merged 2 commits into from Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion build.rs
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
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