Skip to content
Myr

Update to nightly's new Error::provide API

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Tolnay · committed by David Tolnay · parent 496b958 · Browse files · 7 files+46 added−53 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+46 added−53 removed

  • other:+46 added−53 removed

7 files+46 added−53 removed

modified:src/backtrace.rs+1 added-1 removed
36 unmodified lines
3738#[cfg(backtrace)]39macro_rules! backtrace_if_absent {40 ($err:expr) => {41 match ($err as &dyn std::error::Error).request_ref::<std::backtrace::Backtrace>() {41 match std::error::request_ref::<std::backtrace::Backtrace>($err as &dyn std::error::Error) {42 Some(_) => None,43 None => backtrace!(),44 }45 };
356 unmodified lines
modified:src/context.rs+5 added-5 removed
2 unmodified lines
3use core::convert::Infallible;4use core::fmt::{self, Debug, Display, Write};56#[cfg(backtrace)]7use std::any::{Demand, Provider};7use std::error::Request;89mod ext {10 use super::*;11
131 unmodified lines
143 Some(&self.error)144 }145146 #[cfg(backtrace)]147 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {148 StdError::provide(&self.error, demand);147 fn provide<'a>(&'a self, request: &mut Request<'a>) {148 StdError::provide(&self.error, request);149 }150}151152impl<C> StdError for ContextError<C, Error>
4 unmodified lines
157 Some(unsafe { crate::ErrorImpl::error(self.error.inner.by_ref()) })158 }159160 #[cfg(backtrace)]161 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {162 Provider::provide(&self.error, demand);161 fn provide<'a>(&'a self, request: &mut Request<'a>) {162 Error::provide(&self.error, request);163 }164}165166struct Quoted<C>(C);
27 unmodified lines
modified:src/error.rs+17 added-13 removed
3 unmodified lines
4use crate::ptr::Mut;5use crate::ptr::{Own, Ref};6use crate::{Error, StdError};7use alloc::boxed::Box;8#[cfg(backtrace)]9use core::any::Demand;8use core::any::TypeId;9use core::fmt::{self, Debug, Display};10use core::mem::ManuallyDrop;11#[cfg(not(anyhow_no_ptr_addr_of))]12use core::ptr;13use core::ptr::NonNull;14#[cfg(backtrace)]15use std::error::{self, Request};1617#[cfg(feature = "std")]18use core::ops::{Deref, DerefMut};19
501 unmodified lines
521522 Some(addr.cast::<E>().deref_mut())523 }524 }525}525527#[cfg(backtrace)]528impl std::any::Provider for Error {526 #[cfg(backtrace)]527 pub(crate) fn provide<'a>(&'a self, request: &mut Request<'a>) {528 unsafe { ErrorImpl::provide(self.inner.by_ref(), request) }529 }530531 // Called by thiserror when you have `#[source] anyhow::Error`. This provide532 // implementation includes the anyhow::Error's Backtrace if any, unlike533 // deref'ing to dyn Error where the provide implementation would include534 // only the original error's Backtrace from before it got wrapped into an535 // anyhow::Error.534 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {535 unsafe { ErrorImpl::provide(self.inner.by_ref(), demand) }536 #[cfg(backtrace)]537 #[doc(hidden)]538 pub fn thiserror_provide<'a>(&'a self, request: &mut Request<'a>) {539 Self::provide(self, request);540 }541}542543#[cfg(feature = "std")]
359 unmodified lines
903 .backtrace904 .as_ref()905 .or_else(|| {906 #[cfg(backtrace)]903 return Self::error(this).request_ref::<Backtrace>();907 return error::request_ref::<Backtrace>(Self::error(this));908 #[cfg(not(backtrace))]909 return (vtable(this.ptr).object_backtrace)(this);910 })911 .expect("backtrace capture failed")912 }913914 #[cfg(backtrace)]911 unsafe fn provide<'a>(this: Ref<'a, Self>, demand: &mut Demand<'a>) {915 unsafe fn provide<'a>(this: Ref<'a, Self>, request: &mut Request<'a>) {916 if let Some(backtrace) = &this.deref().backtrace {913 demand.provide_ref(backtrace);917 request.provide_ref(backtrace);918 }915 Self::error(this).provide(demand);919 Self::error(this).provide(request);920 }921922 #[cold]923 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {
9 unmodified lines
933 unsafe { ErrorImpl::error(self.erase()).source() }934 }935936 #[cfg(backtrace)]933 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {934 unsafe { ErrorImpl::provide(self.erase(), demand) }937 fn provide<'a>(&'a self, request: &mut Request<'a>) {938 unsafe { ErrorImpl::provide(self.erase(), request) }939 }940}941942impl<E> Debug for ErrorImpl<E>
54 unmodified lines
modified:src/lib.rs+1 added-1 removed
209 unmodified lines
210//! will require an explicit `.map_err(Error::msg)` when working with a211//! non-Anyhow error type inside a function that returns Anyhow's error type.212213#![doc(html_root_url = "https://docs.rs/anyhow/1.0.72")]214#![cfg_attr(backtrace, feature(error_generic_member_access, provide_any))]214#![cfg_attr(backtrace, feature(error_generic_member_access))]215#![cfg_attr(doc_cfg, feature(doc_cfg))]216#![cfg_attr(not(feature = "std"), no_std)]217#![deny(dead_code, unused_imports, unused_mut)]218#![allow(
465 unmodified lines
modified:src/wrapper.rs+3 added-3 removed
1use crate::StdError;2use core::fmt::{self, Debug, Display};34#[cfg(backtrace)]5use std::any::Demand;5use std::error::Request;67#[repr(transparent)]8pub struct MessageError<M>(pub M);9
64 unmodified lines
74 self.0.source()75 }7677 #[cfg(backtrace)]78 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {79 self.0.provide(demand);78 fn provide<'a>(&'a self, request: &mut Request<'a>) {79 self.0.provide(request);80 }81}
modified:build.rs+18 added-29 removed
9 unmodified lines
10compile_error! {11 "`backtrace` feature without `std` feature is not supported"12}1314// This code exercises the surface area that we expect of the std Backtrace15// type. If the current toolchain is able to compile it, we go ahead and use16// backtrace in anyhow.14// This code exercises the surface area that we expect of the Error generic15// member access API. If the current toolchain is able to compile it, then16// anyhow is able to provide backtrace support.17const PROBE: &str = r#"18 #![feature(error_generic_member_access, provide_any)]18 #![feature(error_generic_member_access)]1920 use std::any::{Demand, Provider};21 use std::backtrace::{Backtrace, BacktraceStatus};22 use std::error::Error;23 use std::fmt::{self, Display};20 use std::backtrace::Backtrace;21 use std::error::{self, Error, Request};22 use std::fmt::{self, Debug, Display};2325 #[derive(Debug)]26 struct E {27 backtrace: Backtrace,28 }24 struct MyError(Thing);25 struct Thing;2630 impl Display for E {27 impl Debug for MyError {28 fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {29 unimplemented!()30 }31 }3236 impl Error for E {37 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {38 demand.provide_ref(&self.backtrace);33 impl Display for MyError {34 fn fmt(&self, _formatter: &mut fmt::Formatter) -> fmt::Result {35 unimplemented!()36 }37 }3842 struct P;4344 impl Provider for P {45 fn provide<'a>(&'a self, _demand: &mut Demand<'a>) {}39 impl Error for MyError {40 fn provide<'a>(&'a self, request: &mut Request<'a>) {41 request.provide_ref(&self.0);42 }43 }4448 const _: fn() = || {49 let backtrace: Backtrace = Backtrace::capture();50 let status: BacktraceStatus = backtrace.status();51 match status {52 BacktraceStatus::Captured | BacktraceStatus::Disabled | _ => {}53 }54 };5556 const _: fn(&dyn Error) -> Option<&Backtrace> = |err| err.request_ref::<Backtrace>();45 const _: fn(&dyn Error) -> Option<&Backtrace> = |err| error::request_ref::<Backtrace>(err);46"#;4748fn main() {49 if cfg!(feature = "std") {
71 unmodified lines
modified:Cargo.toml+1 added-1 removed