Skip to content
Myr

error.rs

This file

Path

src/error.rs

1,218 lines · 44.1 KiB · Rust

Last changed by

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

History of this file

Take it

The walk stopped after 200 commits: lines older than 07acdac point at it.

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1–2why, one step back
1use crate::backtrace::Backtrace;
2use crate::chain::Chain;

Merge pull request #414 from dtolnay/nightlyDavid Tolnaybefore adoption213a9c2

lines 3–4why, one step back
3#[cfg(error_generic_member_access)]
4use crate::nightly::{self, Request};

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 5why, one step back
5#[cfg(any(feature = "std", not(anyhow_no_core_error), anyhow_no_ptr_addr_of))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 6–12why, one step back
6use crate::ptr::Mut;
7use crate::ptr::{Own, Ref};8use crate::{Error, StdError};9use alloc::boxed::Box;10use core::any::TypeId;11use core::fmt::{self, Debug, Display};12use core::mem::ManuallyDrop;

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

lines 13–14why, one step back
13#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
14use core::ops::{Deref, DerefMut};

Merge pull request #386 from dtolnay/unwindsafeDavid Tolnaybefore adoption830c399

lines 15–16why, one step back
15#[cfg(not(anyhow_no_core_unwind_safe))]
16use core::panic::{RefUnwindSafe, UnwindSafe};

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 17–19why, one step back
17#[cfg(not(anyhow_no_ptr_addr_of))]
18use core::ptr;19use core::ptr::NonNull;

Merge pull request #386 from dtolnay/unwindsafeDavid Tolnaybefore adoption830c399

lines 20–21why, one step back
20#[cfg(all(feature = "std", anyhow_no_core_unwind_safe))]
21use std::panic::{RefUnwindSafe, UnwindSafe};

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 22–30why, one step back
22
23impl Error {24 /// Create a new error object from any error type.25 ///26 /// The error type must be threadsafe and `'static`, so that the `Error`27 /// will be as well.28 ///29 /// If the error type does not provide a backtrace, a backtrace will be30 /// created here to ensure that a backtrace exists.

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 31why, one step back
31 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 32–38why, one step back
32 #[cold]
33 #[must_use]34 pub fn new<E>(error: E) -> Self35 where36 E: StdError + Send + Sync + 'static,37 {38 let backtrace = backtrace_if_absent!(&error);

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 39why, one step back
39 Error::construct_from_std(error, backtrace)

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 40–84why, one step back
40 }
4142 /// Create a new error object from a printable error message.43 ///44 /// If the argument implements std::error::Error, prefer `Error::new`45 /// instead which preserves the underlying error's cause chain and46 /// backtrace. If the argument may or may not implement std::error::Error47 /// now or in the future, use `anyhow!(err)` which handles either way48 /// correctly.49 ///50 /// `Error::msg("...")` is equivalent to `anyhow!("...")` but occasionally51 /// convenient in places where a function is preferable over a macro, such52 /// as iterator or stream combinators:53 ///54 /// ```55 /// # mod ffi {56 /// # pub struct Input;57 /// # pub struct Output;58 /// # pub async fn do_some_work(_: Input) -> Result<Output, &'static str> {59 /// # unimplemented!()60 /// # }61 /// # }62 /// #63 /// # use ffi::{Input, Output};64 /// #65 /// use anyhow::{Error, Result};66 /// use futures::stream::{Stream, StreamExt, TryStreamExt};67 ///68 /// async fn demo<S>(stream: S) -> Result<Vec<Output>>69 /// where70 /// S: Stream<Item = Input>,71 /// {72 /// stream73 /// .then(ffi::do_some_work) // returns Result<Output, &str>74 /// .map_err(Error::msg)75 /// .try_collect()76 /// .await77 /// }78 /// ```79 #[cold]80 #[must_use]81 pub fn msg<M>(message: M) -> Self82 where83 M: Display + Debug + Send + Sync + 'static,84 {

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 85why, one step back
85 Error::construct_from_adhoc(message, backtrace!())

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 86–87why, one step back
86 }
87

Merge pull request #402 from dtolnay/fromboxedDavid Tolnaybefore adoptiona03d6d6

lines 88–148why, one step back
88 /// Construct an error object from a type-erased standard library error.
89 ///90 /// This is mostly useful for interop with other error libraries.91 ///92 /// # Example93 ///94 /// Here is a skeleton of a library that provides its own error abstraction.95 /// The pair of `From` impls provide bidirectional support for `?`96 /// conversion between `Report` and `anyhow::Error`.97 ///98 /// ```99 /// use std::error::Error as StdError;100 ///101 /// pub struct Report {/* ... */}102 ///103 /// impl<E> From<E> for Report104 /// where105 /// E: Into<anyhow::Error>,106 /// Result<(), E>: anyhow::Context<(), E>,107 /// {108 /// fn from(error: E) -> Self {109 /// let anyhow_error: anyhow::Error = error.into();110 /// let boxed_error: Box<dyn StdError + Send + Sync + 'static> = anyhow_error.into();111 /// Report::from_boxed(boxed_error)112 /// }113 /// }114 ///115 /// impl From<Report> for anyhow::Error {116 /// fn from(report: Report) -> Self {117 /// let boxed_error: Box<dyn StdError + Send + Sync + 'static> = report.into_boxed();118 /// anyhow::Error::from_boxed(boxed_error)119 /// }120 /// }121 ///122 /// impl Report {123 /// fn from_boxed(boxed_error: Box<dyn StdError + Send + Sync + 'static>) -> Self {124 /// todo!()125 /// }126 /// fn into_boxed(self) -> Box<dyn StdError + Send + Sync + 'static> {127 /// todo!()128 /// }129 /// }130 ///131 /// // Example usage: can use `?` in both directions.132 /// fn a() -> anyhow::Result<()> {133 /// b()?;134 /// Ok(())135 /// }136 /// fn b() -> Result<(), Report> {137 /// a()?;138 /// Ok(())139 /// }140 /// ```141 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]142 #[cold]143 #[must_use]144 pub fn from_boxed(boxed_error: Box<dyn StdError + Send + Sync + 'static>) -> Self {145 let backtrace = backtrace_if_absent!(&*boxed_error);146 Error::construct_from_boxed(boxed_error, backtrace)147 }148

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 149why, one step back
149 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 150why, one step back
150 #[cold]

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 151why, one step back
151 pub(crate) fn construct_from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 152–159why, one step back
152 where
153 E: StdError + Send + Sync + 'static,154 {155 let vtable = &ErrorVTable {156 object_drop: object_drop::<E>,157 object_ref: object_ref::<E>,158 #[cfg(anyhow_no_ptr_addr_of)]159 object_mut: object_mut::<E>,

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 160why, one step back
160 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 161why, one step back
161 object_boxed: object_boxed::<E>,

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

lines 162–163why, one step back
162 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
163 object_reallocate_boxed: object_reallocate_boxed::<E>,

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 164–167why, one step back
164 object_downcast: object_downcast::<E>,
165 #[cfg(anyhow_no_ptr_addr_of)]166 object_downcast_mut: object_downcast_mut::<E>,167 object_drop_rest: object_drop_front::<E>,

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

lines 168–169why, one step back
168 #[cfg(all(
169 not(error_generic_member_access),

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 170why, one step back
170 any(std_backtrace, feature = "backtrace")

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 171why, one step back
171 ))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 172–179why, one step back
172 object_backtrace: no_backtrace,
173 };174175 // Safety: passing vtable that operates on the right type E.176 unsafe { Error::construct(error, vtable, backtrace) }177 }178179 #[cold]

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 180why, one step back
180 pub(crate) fn construct_from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 181–188why, one step back
181 where
182 M: Display + Debug + Send + Sync + 'static,183 {184 use crate::wrapper::MessageError;185 let error: MessageError<M> = MessageError(message);186 let vtable = &ErrorVTable {187 object_drop: object_drop::<MessageError<M>>,188 object_ref: object_ref::<MessageError<M>>,

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 189why, one step back
189 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 190why, one step back
190 object_mut: object_mut::<MessageError<M>>,

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 191why, one step back
191 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 192why, one step back
192 object_boxed: object_boxed::<MessageError<M>>,

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

lines 193–194why, one step back
193 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
194 object_reallocate_boxed: object_reallocate_boxed::<MessageError<M>>,

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 195–198why, one step back
195 object_downcast: object_downcast::<M>,
196 #[cfg(anyhow_no_ptr_addr_of)]197 object_downcast_mut: object_downcast_mut::<M>,198 object_drop_rest: object_drop_front::<M>,

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

lines 199–200why, one step back
199 #[cfg(all(
200 not(error_generic_member_access),

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 201why, one step back
201 any(std_backtrace, feature = "backtrace")

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 202why, one step back
202 ))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 203–211why, one step back
203 object_backtrace: no_backtrace,
204 };205206 // Safety: MessageError is repr(transparent) so it is okay for the207 // vtable to allow casting the MessageError<M> to M.208 unsafe { Error::construct(error, vtable, backtrace) }209 }210211 #[cold]

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 212why, one step back
212 pub(crate) fn construct_from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 213–220why, one step back
213 where
214 M: Display + Send + Sync + 'static,215 {216 use crate::wrapper::DisplayError;217 let error: DisplayError<M> = DisplayError(message);218 let vtable = &ErrorVTable {219 object_drop: object_drop::<DisplayError<M>>,220 object_ref: object_ref::<DisplayError<M>>,

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 221why, one step back
221 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 222why, one step back
222 object_mut: object_mut::<DisplayError<M>>,

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 223why, one step back
223 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 224why, one step back
224 object_boxed: object_boxed::<DisplayError<M>>,

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

lines 225–226why, one step back
225 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
226 object_reallocate_boxed: object_reallocate_boxed::<DisplayError<M>>,

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 227–230why, one step back
227 object_downcast: object_downcast::<M>,
228 #[cfg(anyhow_no_ptr_addr_of)]229 object_downcast_mut: object_downcast_mut::<M>,230 object_drop_rest: object_drop_front::<M>,

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

lines 231–232why, one step back
231 #[cfg(all(
232 not(error_generic_member_access),

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 233why, one step back
233 any(std_backtrace, feature = "backtrace")

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 234why, one step back
234 ))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 235–242why, one step back
235 object_backtrace: no_backtrace,
236 };237238 // Safety: DisplayError is repr(transparent) so it is okay for the239 // vtable to allow casting the DisplayError<M> to M.240 unsafe { Error::construct(error, vtable, backtrace) }241 }242

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 243why, one step back
243 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 244why, one step back
244 #[cold]

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

lines 245–249why, one step back
245 pub(crate) fn construct_from_context<C, E>(
246 context: C,247 error: E,248 backtrace: Option<Backtrace>,249 ) -> Self

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 250–260why, one step back
250 where
251 C: Display + Send + Sync + 'static,252 E: StdError + Send + Sync + 'static,253 {254 let error: ContextError<C, E> = ContextError { context, error };255256 let vtable = &ErrorVTable {257 object_drop: object_drop::<ContextError<C, E>>,258 object_ref: object_ref::<ContextError<C, E>>,259 #[cfg(anyhow_no_ptr_addr_of)]260 object_mut: object_mut::<ContextError<C, E>>,

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 261why, one step back
261 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 262why, one step back
262 object_boxed: object_boxed::<ContextError<C, E>>,

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

lines 263–264why, one step back
263 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
264 object_reallocate_boxed: object_reallocate_boxed::<ContextError<C, E>>,

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 265–268why, one step back
265 object_downcast: context_downcast::<C, E>,
266 #[cfg(anyhow_no_ptr_addr_of)]267 object_downcast_mut: context_downcast_mut::<C, E>,268 object_drop_rest: context_drop_rest::<C, E>,

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

lines 269–270why, one step back
269 #[cfg(all(
270 not(error_generic_member_access),

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 271why, one step back
271 any(std_backtrace, feature = "backtrace")

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 272why, one step back
272 ))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 273–279why, one step back
273 object_backtrace: no_backtrace,
274 };275276 // Safety: passing vtable that operates on the right type.277 unsafe { Error::construct(error, vtable, backtrace) }278 }279

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 280why, one step back
280 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 281why, one step back
281 #[cold]

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 282why, one step back
282 pub(crate) fn construct_from_boxed(

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 283–292why, one step back
283 error: Box<dyn StdError + Send + Sync>,
284 backtrace: Option<Backtrace>,285 ) -> Self {286 use crate::wrapper::BoxedError;287 let error = BoxedError(error);288 let vtable = &ErrorVTable {289 object_drop: object_drop::<BoxedError>,290 object_ref: object_ref::<BoxedError>,291 #[cfg(anyhow_no_ptr_addr_of)]292 object_mut: object_mut::<BoxedError>,

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 293why, one step back
293 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 294why, one step back
294 object_boxed: object_boxed::<BoxedError>,

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

lines 295–296why, one step back
295 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
296 object_reallocate_boxed: object_reallocate_boxed::<BoxedError>,

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 297–300why, one step back
297 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,
298 #[cfg(anyhow_no_ptr_addr_of)]299 object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,300 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

lines 301–302why, one step back
301 #[cfg(all(
302 not(error_generic_member_access),

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 303why, one step back
303 any(std_backtrace, feature = "backtrace")

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 304why, one step back
304 ))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 305–409why, one step back
305 object_backtrace: no_backtrace,
306 };307308 // Safety: BoxedError is repr(transparent) so it is okay for the vtable309 // to allow casting to Box<dyn StdError + Send + Sync>.310 unsafe { Error::construct(error, vtable, backtrace) }311 }312313 // Takes backtrace as argument rather than capturing it here so that the314 // user sees one fewer layer of wrapping noise in the backtrace.315 //316 // Unsafe because the given vtable must have sensible behavior on the error317 // value of type E.318 #[cold]319 unsafe fn construct<E>(320 error: E,321 vtable: &'static ErrorVTable,322 backtrace: Option<Backtrace>,323 ) -> Self324 where325 E: StdError + Send + Sync + 'static,326 {327 let inner: Box<ErrorImpl<E>> = Box::new(ErrorImpl {328 vtable,329 backtrace,330 _object: error,331 });332 // Erase the concrete type of E from the compile-time type system. This333 // is equivalent to the safe unsize coercion from Box<ErrorImpl<E>> to334 // Box<ErrorImpl<dyn StdError + Send + Sync + 'static>> except that the335 // result is a thin pointer. The necessary behavior for manipulating the336 // underlying ErrorImpl<E> is preserved in the vtable provided by the337 // caller rather than a builtin fat pointer vtable.338 let inner = Own::new(inner).cast::<ErrorImpl>();339 Error { inner }340 }341342 /// Wrap the error value with additional context.343 ///344 /// For attaching context to a `Result` as it is propagated, the345 /// [`Context`][crate::Context] extension trait may be more convenient than346 /// this function.347 ///348 /// The primary reason to use `error.context(...)` instead of349 /// `result.context(...)` via the `Context` trait would be if the context350 /// needs to depend on some data held by the underlying error:351 ///352 /// ```353 /// # use std::fmt::{self, Debug, Display};354 /// #355 /// # type T = ();356 /// #357 /// # impl std::error::Error for ParseError {}358 /// # impl Debug for ParseError {359 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {360 /// # unimplemented!()361 /// # }362 /// # }363 /// # impl Display for ParseError {364 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {365 /// # unimplemented!()366 /// # }367 /// # }368 /// #369 /// use anyhow::Result;370 /// use std::fs::File;371 /// use std::path::Path;372 ///373 /// struct ParseError {374 /// line: usize,375 /// column: usize,376 /// }377 ///378 /// fn parse_impl(file: File) -> Result<T, ParseError> {379 /// # const IGNORE: &str = stringify! {380 /// ...381 /// # };382 /// # unimplemented!()383 /// }384 ///385 /// pub fn parse(path: impl AsRef<Path>) -> Result<T> {386 /// let file = File::open(&path)?;387 /// parse_impl(file).map_err(|error| {388 /// let context = format!(389 /// "only the first {} lines of {} are valid",390 /// error.line, path.as_ref().display(),391 /// );392 /// anyhow::Error::new(error).context(context)393 /// })394 /// }395 /// ```396 #[cold]397 #[must_use]398 pub fn context<C>(self, context: C) -> Self399 where400 C: Display + Send + Sync + 'static,401 {402 let error: ContextError<C, Error> = ContextError {403 context,404 error: self,405 };406407 let vtable = &ErrorVTable {408 object_drop: object_drop::<ContextError<C, Error>>,409 object_ref: object_ref::<ContextError<C, Error>>,

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 410why, one step back
410 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 411why, one step back
411 object_mut: object_mut::<ContextError<C, Error>>,

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 412why, one step back
412 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 413why, one step back
413 object_boxed: object_boxed::<ContextError<C, Error>>,

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

lines 414–415why, one step back
414 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
415 object_reallocate_boxed: object_reallocate_boxed::<ContextError<C, Error>>,

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 416–419why, one step back
416 object_downcast: context_chain_downcast::<C>,
417 #[cfg(anyhow_no_ptr_addr_of)]418 object_downcast_mut: context_chain_downcast_mut::<C>,419 object_drop_rest: context_chain_drop_rest::<C>,

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

lines 420–421why, one step back
420 #[cfg(all(
421 not(error_generic_member_access),

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 422why, one step back
422 any(std_backtrace, feature = "backtrace")

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 423why, one step back
423 ))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 424–450why, one step back
424 object_backtrace: context_backtrace::<C>,
425 };426427 // As the cause is anyhow::Error, we already have a backtrace for it.428 let backtrace = None;429430 // Safety: passing vtable that operates on the right type.431 unsafe { Error::construct(error, vtable, backtrace) }432 }433434 /// Get the backtrace for this Error.435 ///436 /// In order for the backtrace to be meaningful, one of the two environment437 /// variables `RUST_LIB_BACKTRACE=1` or `RUST_BACKTRACE=1` must be defined438 /// and `RUST_LIB_BACKTRACE` must not be `0`. Backtraces are somewhat439 /// expensive to capture in Rust, so we don't necessarily want to be440 /// capturing them all over the place all the time.441 ///442 /// - If you want panics and errors to both have backtraces, set443 /// `RUST_BACKTRACE=1`;444 /// - If you want only errors to have backtraces, set445 /// `RUST_LIB_BACKTRACE=1`;446 /// - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and447 /// `RUST_LIB_BACKTRACE=0`.448 ///449 /// # Stability450 ///

Touch up PR 293David Tolnaybefore adoptionb6ac995

lines 451–452why, one step back
451 /// Standard library backtraces are only available when using Rust &ge;
452 /// 1.65. On older compilers, this function is only available if the crate's

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 453why, one step back
453 /// "backtrace" feature is enabled, and will use the `backtrace` crate as

Touch up PR 293David Tolnaybefore adoptionb6ac995

lines 454–455why, one step back
454 /// the underlying backtrace implementation. The return type of this
455 /// function on old compilers is `&(impl Debug + Display)`.

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 456–460why, one step back
456 ///
457 /// ```toml458 /// [dependencies]459 /// anyhow = { version = "1.0", features = ["backtrace"] }460 /// ```

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 461why, one step back
461 #[cfg(any(std_backtrace, feature = "backtrace"))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 462–486why, one step back
462 pub fn backtrace(&self) -> &impl_backtrace!() {
463 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }464 }465466 /// An iterator of the chain of source errors contained by this Error.467 ///468 /// This iterator will visit every error in the cause chain of this error469 /// object, beginning with the error that this error object was created470 /// from.471 ///472 /// # Example473 ///474 /// ```475 /// use anyhow::Error;476 /// use std::io;477 ///478 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {479 /// for cause in error.chain() {480 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {481 /// return Some(io_error.kind());482 /// }483 /// }484 /// None485 /// }486 /// ```

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 487why, one step back
487 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 488–497why, one step back
488 #[cold]
489 pub fn chain(&self) -> Chain {490 unsafe { ErrorImpl::chain(self.inner.by_ref()) }491 }492493 /// The lowest level cause of this error &mdash; this error's cause's494 /// cause's cause etc.495 ///496 /// The root cause is the last error in the iterator produced by497 /// [`chain()`][Error::chain].

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 498why, one step back
498 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Ignore double_ended_iterator_last clippy lintDavid Tolnaybefore adoption4d71a84

line 499why, one step back
499 #[allow(clippy::double_ended_iterator_last)]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 500–510why, one step back
500 pub fn root_cause(&self) -> &(dyn StdError + 'static) {
501 self.chain().last().unwrap()502 }503504 /// Returns true if `E` is the type held by this error object.505 ///506 /// For errors with context, this method returns true if `E` matches the507 /// type of the context `C` **or** the type of the error on which the508 /// context has been attached. For details about the interaction between509 /// context and downcasting, [see here].510 ///

Convert html links to intra-doc linksDavid Tolnaybefore adoptionbc33c24

line 511why, one step back
511 /// [see here]: crate::Context#effect-on-downcasting

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 512–623why, one step back
512 pub fn is<E>(&self) -> bool
513 where514 E: Display + Debug + Send + Sync + 'static,515 {516 self.downcast_ref::<E>().is_some()517 }518519 /// Attempt to downcast the error object to a concrete type.520 pub fn downcast<E>(mut self) -> Result<E, Self>521 where522 E: Display + Debug + Send + Sync + 'static,523 {524 let target = TypeId::of::<E>();525 let inner = self.inner.by_mut();526 unsafe {527 // Use vtable to find NonNull<()> which points to a value of type E528 // somewhere inside the data structure.529 #[cfg(not(anyhow_no_ptr_addr_of))]530 let addr = match (vtable(inner.ptr).object_downcast)(inner.by_ref(), target) {531 Some(addr) => addr.by_mut().extend(),532 None => return Err(self),533 };534 #[cfg(anyhow_no_ptr_addr_of)]535 let addr = match (vtable(inner.ptr).object_downcast_mut)(inner, target) {536 Some(addr) => addr.extend(),537 None => return Err(self),538 };539540 // Prepare to read E out of the data structure. We'll drop the rest541 // of the data structure separately so that E is not dropped.542 let outer = ManuallyDrop::new(self);543544 // Read E from where the vtable found it.545 let error = addr.cast::<E>().read();546547 // Drop rest of the data structure outside of E.548 (vtable(outer.inner.ptr).object_drop_rest)(outer.inner, target);549550 Ok(error)551 }552 }553554 /// Downcast this error object by reference.555 ///556 /// # Example557 ///558 /// ```559 /// # use anyhow::anyhow;560 /// # use std::fmt::{self, Display};561 /// # use std::task::Poll;562 /// #563 /// # #[derive(Debug)]564 /// # enum DataStoreError {565 /// # Censored(()),566 /// # }567 /// #568 /// # impl Display for DataStoreError {569 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {570 /// # unimplemented!()571 /// # }572 /// # }573 /// #574 /// # impl std::error::Error for DataStoreError {}575 /// #576 /// # const REDACTED_CONTENT: () = ();577 /// #578 /// # let error = anyhow!("...");579 /// # let root_cause = &error;580 /// #581 /// # let ret =582 /// // If the error was caused by redaction, then return a tombstone instead583 /// // of the content.584 /// match root_cause.downcast_ref::<DataStoreError>() {585 /// Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),586 /// None => Err(error),587 /// }588 /// # ;589 /// ```590 pub fn downcast_ref<E>(&self) -> Option<&E>591 where592 E: Display + Debug + Send + Sync + 'static,593 {594 let target = TypeId::of::<E>();595 unsafe {596 // Use vtable to find NonNull<()> which points to a value of type E597 // somewhere inside the data structure.598 let addr = (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?;599 Some(addr.cast::<E>().deref())600 }601 }602603 /// Downcast this error object by mutable reference.604 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>605 where606 E: Display + Debug + Send + Sync + 'static,607 {608 let target = TypeId::of::<E>();609 unsafe {610 // Use vtable to find NonNull<()> which points to a value of type E611 // somewhere inside the data structure.612613 #[cfg(not(anyhow_no_ptr_addr_of))]614 let addr =615 (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?.by_mut();616617 #[cfg(anyhow_no_ptr_addr_of)]618 let addr = (vtable(self.inner.ptr).object_downcast_mut)(self.inner.by_mut(), target)?;619620 Some(addr.cast::<E>().deref_mut())621 }622 }623

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

lines 624–715why, one step back
624 /// Convert to a standard library error trait object.
625 ///626 /// This is implemented as a cheap pointer cast that does not allocate or627 /// deallocate memory. Like [`anyhow::Error::from_boxed`], it's useful for628 /// interop with other error libraries.629 ///630 /// The same conversion is also available as631 /// <code style="display:inline;white-space:normal;">impl From&lt;anyhow::Error&gt;632 /// for Box&lt;dyn Error + Send + Sync + &apos;static&gt;</code>.633 ///634 /// If a backtrace was collected during construction of the `anyhow::Error`,635 /// that backtrace remains accessible using the standard library `Error`636 /// trait's provider API, but as a consequence, the resulting boxed error637 /// can no longer be downcast to its original underlying type.638 ///639 /// ```640 #[cfg_attr(not(error_generic_member_access), doc = "# _ = stringify! {")]641 /// #![feature(error_generic_member_access)]642 ///643 /// use anyhow::anyhow;644 /// use std::backtrace::Backtrace;645 /// use thiserror::Error;646 ///647 /// #[derive(Error, Debug)]648 /// #[error("...")]649 /// struct MyError;650 ///651 /// let anyhow_error = anyhow!(MyError);652 /// println!("{}", anyhow_error.backtrace()); // has Backtrace653 /// assert!(anyhow_error.downcast_ref::<MyError>().is_some()); // can downcast654 ///655 /// let boxed_dyn_error = anyhow_error.into_boxed_dyn_error();656 /// assert!(std::error::request_ref::<Backtrace>(&*boxed_dyn_error).is_some()); // has Backtrace657 /// assert!(boxed_dyn_error.downcast_ref::<MyError>().is_none()); // can no longer downcast658 #[cfg_attr(not(error_generic_member_access), doc = "# };")]659 /// ```660 ///661 /// [`anyhow::Error::from_boxed`]: Self::from_boxed662 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]663 #[must_use]664 pub fn into_boxed_dyn_error(self) -> Box<dyn StdError + Send + Sync + 'static> {665 let outer = ManuallyDrop::new(self);666 unsafe {667 // Use vtable to attach ErrorImpl<E>'s native StdError vtable for668 // the right original type E.669 (vtable(outer.inner.ptr).object_boxed)(outer.inner)670 }671 }672673 /// Convert to a standard library error trait object.674 ///675 /// Unlike `self.into_boxed_dyn_error()`, this method relocates the676 /// underlying error into a new allocation in order to make it downcastable677 /// to `&E` or `Box<E>` for its original underlying error type. Any678 /// backtrace collected during construction of the `anyhow::Error` is679 /// discarded.680 ///681 /// ```682 #[cfg_attr(not(error_generic_member_access), doc = "# _ = stringify!{")]683 /// #![feature(error_generic_member_access)]684 ///685 /// use anyhow::anyhow;686 /// use std::backtrace::Backtrace;687 /// use thiserror::Error;688 ///689 /// #[derive(Error, Debug)]690 /// #[error("...")]691 /// struct MyError;692 ///693 /// let anyhow_error = anyhow!(MyError);694 /// println!("{}", anyhow_error.backtrace()); // has Backtrace695 /// assert!(anyhow_error.downcast_ref::<MyError>().is_some()); // can downcast696 ///697 /// let boxed_dyn_error = anyhow_error.reallocate_into_boxed_dyn_error_without_backtrace();698 /// assert!(std::error::request_ref::<Backtrace>(&*boxed_dyn_error).is_none()); // Backtrace lost699 /// assert!(boxed_dyn_error.downcast_ref::<MyError>().is_some()); // can downcast to &MyError700 /// assert!(boxed_dyn_error.downcast::<MyError>().is_ok()); // can downcast to Box<MyError>701 #[cfg_attr(not(error_generic_member_access), doc = "# };")]702 /// ```703 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]704 #[must_use]705 pub fn reallocate_into_boxed_dyn_error_without_backtrace(706 self,707 ) -> Box<dyn StdError + Send + Sync + 'static> {708 let outer = ManuallyDrop::new(self);709 unsafe {710 // Use vtable to attach E's native StdError vtable for the right711 // original type E.712 (vtable(outer.inner.ptr).object_reallocate_boxed)(outer.inner)713 }714 }715

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 716why, one step back
716 #[cfg(error_generic_member_access)]

Merge pull request #319 from dtolnay/errorprovideDavid Tolnaybefore adoption8cf1d15

lines 717–720why, one step back
717 pub(crate) fn provide<'a>(&'a self, request: &mut Request<'a>) {
718 unsafe { ErrorImpl::provide(self.inner.by_ref(), request) }719 }720

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 721–725why, one step back
721 // Called by thiserror when you have `#[source] anyhow::Error`. This provide
722 // implementation includes the anyhow::Error's Backtrace if any, unlike723 // deref'ing to dyn Error where the provide implementation would include724 // only the original error's Backtrace from before it got wrapped into an725 // anyhow::Error.

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 726why, one step back
726 #[cfg(error_generic_member_access)]

Merge pull request #319 from dtolnay/errorprovideDavid Tolnaybefore adoption8cf1d15

lines 727–729why, one step back
727 #[doc(hidden)]
728 pub fn thiserror_provide<'a>(&'a self, request: &mut Request<'a>) {729 Self::provide(self, request);

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 730–732why, one step back
730 }
731}732

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 733why, one step back
733#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 734–740why, one step back
734impl<E> From<E> for Error
735where736 E: StdError + Send + Sync + 'static,737{738 #[cold]739 fn from(error: E) -> Self {740 let backtrace = backtrace_if_absent!(&error);

Merge pull request #401 from dtolnay/constructDavid Tolnaybefore adoptionffecefc

line 741why, one step back
741 Error::construct_from_std(error, backtrace)

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 742–744why, one step back
742 }
743}744

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 745why, one step back
745#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 746–753why, one step back
746impl Deref for Error {
747 type Target = dyn StdError + Send + Sync + 'static;748749 fn deref(&self) -> &Self::Target {750 unsafe { ErrorImpl::error(self.inner.by_ref()) }751 }752}753

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 754why, one step back
754#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 755–784why, one step back
755impl DerefMut for Error {
756 fn deref_mut(&mut self) -> &mut Self::Target {757 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }758 }759}760761impl Display for Error {762 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {763 unsafe { ErrorImpl::display(self.inner.by_ref(), formatter) }764 }765}766767impl Debug for Error {768 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {769 unsafe { ErrorImpl::debug(self.inner.by_ref(), formatter) }770 }771}772773impl Drop for Error {774 fn drop(&mut self) {775 unsafe {776 // Invoke the vtable's drop behavior.777 (vtable(self.inner.ptr).object_drop)(self.inner);778 }779 }780}781782struct ErrorVTable {783 object_drop: unsafe fn(Own<ErrorImpl>),784 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 785why, one step back
785 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 786why, one step back
786 object_mut: unsafe fn(Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static),

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 787why, one step back
787 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 788why, one step back
788 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

lines 789–790why, one step back
789 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
790 object_reallocate_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 791–794why, one step back
791 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,
792 #[cfg(anyhow_no_ptr_addr_of)]793 object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,794 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

lines 795–796why, one step back
795 #[cfg(all(
796 not(error_generic_member_access),

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 797why, one step back
797 any(std_backtrace, feature = "backtrace")

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 798why, one step back
798 ))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 799–805why, one step back
799 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,
800}801802// Safety: requires layout of *e to match ErrorImpl<E>.803unsafe fn object_drop<E>(e: Own<ErrorImpl>) {804 // Cast back to ErrorImpl<E> so that the allocator receives the correct805 // Layout to deallocate the Box's memory.

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 806–807why, one step back
806 let unerased_own = e.cast::<ErrorImpl<E>>();
807 drop(unsafe { unerased_own.boxed() });

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 808–815why, one step back
808}
809810// Safety: requires layout of *e to match ErrorImpl<E>.811unsafe fn object_drop_front<E>(e: Own<ErrorImpl>, target: TypeId) {812 // Drop the fields of ErrorImpl other than E as well as the Box allocation,813 // without dropping E itself. This is used by downcast after doing a814 // ptr::read to take ownership of the E.815 let _ = target;

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 816–817why, one step back
816 let unerased_own = e.cast::<ErrorImpl<ManuallyDrop<E>>>();
817 drop(unsafe { unerased_own.boxed() });

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 818–826why, one step back
818}
819820// Safety: requires layout of *e to match ErrorImpl<E>.821unsafe fn object_ref<E>(e: Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>822where823 E: StdError + Send + Sync + 'static,824{825 // Attach E's native StdError vtable onto a pointer to self._object.826

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 827why, one step back
827 let unerased_ref = e.cast::<ErrorImpl<E>>();

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 828–829why, one step back
828
829 #[cfg(not(anyhow_no_ptr_addr_of))]

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 830–832why, one step back
830 return Ref::from_raw(unsafe {
831 NonNull::new_unchecked(ptr::addr_of!((*unerased_ref.as_ptr())._object) as *mut E)832 });

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 833–834why, one step back
833
834 #[cfg(anyhow_no_ptr_addr_of)]

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 835why, one step back
835 return Ref::new(unsafe { &unerased_ref.deref()._object });

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 836–839why, one step back
836}
837838// Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived839// from a `&mut`

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 840why, one step back
840#[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 841–845why, one step back
841unsafe fn object_mut<E>(e: Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static)
842where843 E: StdError + Send + Sync + 'static,844{845 // Attach E's native StdError vtable onto a pointer to self._object.

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 846–847why, one step back
846 let unerased_mut = e.cast::<ErrorImpl<E>>();
847 unsafe { &mut unerased_mut.deref_mut()._object }

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 848–850why, one step back
848}
849850// Safety: requires layout of *e to match ErrorImpl<E>.

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 851why, one step back
851#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 852–856why, one step back
852unsafe fn object_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>
853where854 E: StdError + Send + Sync + 'static,855{856 // Attach ErrorImpl<E>'s native StdError vtable. The StdError impl is below.

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 857–858why, one step back
857 let unerased_own = e.cast::<ErrorImpl<E>>();
858 unsafe { unerased_own.boxed() }

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 859–861why, one step back
859}
860861// Safety: requires layout of *e to match ErrorImpl<E>.

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

lines 862–872why, one step back
862#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
863unsafe fn object_reallocate_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>864where865 E: StdError + Send + Sync + 'static,866{867 // Attach E's native StdError vtable.868 let unerased_own = e.cast::<ErrorImpl<E>>();869 Box::new(unsafe { unerased_own.boxed() }._object)870}871872// Safety: requires layout of *e to match ErrorImpl<E>.

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 873–880why, one step back
873unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
874where875 E: 'static,876{877 if TypeId::of::<E>() == target {878 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a879 // pointer to its E field.880

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 881why, one step back
881 let unerased_ref = e.cast::<ErrorImpl<E>>();

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 882–884why, one step back
882
883 #[cfg(not(anyhow_no_ptr_addr_of))]884 return Some(

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 885–887why, one step back
885 Ref::from_raw(unsafe {
886 NonNull::new_unchecked(ptr::addr_of!((*unerased_ref.as_ptr())._object) as *mut E)887 })

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 888–891why, one step back
888 .cast::<()>(),
889 );890891 #[cfg(anyhow_no_ptr_addr_of)]

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 892why, one step back
892 return Some(Ref::new(unsafe { &unerased_ref.deref()._object }).cast::<()>());

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 893–906why, one step back
893 } else {
894 None895 }896}897898// Safety: requires layout of *e to match ErrorImpl<E>.899#[cfg(anyhow_no_ptr_addr_of)]900unsafe fn object_downcast_mut<E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>901where902 E: 'static,903{904 if TypeId::of::<E>() == target {905 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a906 // pointer to its E field.

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 907–908why, one step back
907 let unerased_mut = e.cast::<ErrorImpl<E>>();
908 let unerased = unsafe { unerased_mut.deref_mut() };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 909–914why, one step back
909 Some(Mut::new(&mut unerased._object).cast::<()>())
910 } else {911 None912 }913}914

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

lines 915–916why, one step back
915#[cfg(all(
916 not(error_generic_member_access),

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 917why, one step back
917 any(std_backtrace, feature = "backtrace")

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 918why, one step back
918))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 919–924why, one step back
919fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {
920 let _ = e;921 None922}923924// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 925why, one step back
925#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 926–931why, one step back
926unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
927where928 C: 'static,929 E: 'static,930{931 if TypeId::of::<C>() == target {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 932–933why, one step back
932 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();
933 let unerased = unsafe { unerased_ref.deref() };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 934–935why, one step back
934 Some(Ref::new(&unerased._object.context).cast::<()>())
935 } else if TypeId::of::<E>() == target {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 936–937why, one step back
936 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();
937 let unerased = unsafe { unerased_ref.deref() };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 938–951why, one step back
938 Some(Ref::new(&unerased._object.error).cast::<()>())
939 } else {940 None941 }942}943944// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.945#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]946unsafe fn context_downcast_mut<C, E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>947where948 C: 'static,949 E: 'static,950{951 if TypeId::of::<C>() == target {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 952–953why, one step back
952 let unerased_mut = e.cast::<ErrorImpl<ContextError<C, E>>>();
953 let unerased = unsafe { unerased_mut.deref_mut() };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 954–955why, one step back
954 Some(Mut::new(&mut unerased._object.context).cast::<()>())
955 } else if TypeId::of::<E>() == target {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 956–957why, one step back
956 let unerased_mut = e.cast::<ErrorImpl<ContextError<C, E>>>();
957 let unerased = unsafe { unerased_mut.deref_mut() };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 958–964why, one step back
958 Some(Mut::new(&mut unerased._object.error).cast::<()>())
959 } else {960 None961 }962}963964// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 965why, one step back
965#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 966–973why, one step back
966unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)
967where968 C: 'static,969 E: 'static,970{971 // Called after downcasting by value to either the C or the E and doing a972 // ptr::read to take ownership of that value.973 if TypeId::of::<C>() == target {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 974–975why, one step back
974 let unerased_own = e.cast::<ErrorImpl<ContextError<ManuallyDrop<C>, E>>>();
975 drop(unsafe { unerased_own.boxed() });

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 976why, one step back
976 } else {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 977–978why, one step back
977 let unerased_own = e.cast::<ErrorImpl<ContextError<C, ManuallyDrop<E>>>>();
978 drop(unsafe { unerased_own.boxed() });

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 979–986why, one step back
979 }
980}981982// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.983unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>984where985 C: 'static,986{

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 987–988why, one step back
987 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();
988 let unerased = unsafe { unerased_ref.deref() };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 989–993why, one step back
989 if TypeId::of::<C>() == target {
990 Some(Ref::new(&unerased._object.context).cast::<()>())991 } else {992 // Recurse down the context chain per the inner error's vtable.993 let source = &unerased._object.error;

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 994why, one step back
994 unsafe { (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target) }

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 995–1003why, one step back
995 }
996}997998// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.999#[cfg(anyhow_no_ptr_addr_of)]1000unsafe fn context_chain_downcast_mut<C>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>1001where1002 C: 'static,1003{

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 1004–1005why, one step back
1004 let unerased_mut = e.cast::<ErrorImpl<ContextError<C, Error>>>();
1005 let unerased = unsafe { unerased_mut.deref_mut() };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1006–1010why, one step back
1006 if TypeId::of::<C>() == target {
1007 Some(Mut::new(&mut unerased._object.context).cast::<()>())1008 } else {1009 // Recurse down the context chain per the inner error's vtable.1010 let source = &mut unerased._object.error;

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1011why, one step back
1011 unsafe { (vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target) }

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1012–1022why, one step back
1012 }
1013}10141015// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.1016unsafe fn context_chain_drop_rest<C>(e: Own<ErrorImpl>, target: TypeId)1017where1018 C: 'static,1019{1020 // Called after downcasting by value to either the C or one of the causes1021 // and doing a ptr::read to take ownership of that value.1022 if TypeId::of::<C>() == target {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1023why, one step back
1023 let unerased_own = e.cast::<ErrorImpl<ContextError<ManuallyDrop<C>, Error>>>();

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 1024why, one step back
1024 // Drop the entire rest of the data structure rooted in the next Error.

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1025why, one step back
1025 drop(unsafe { unerased_own.boxed() });

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 1026why, one step back
1026 } else {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 1027–1028why, one step back
1027 let unerased_own = e.cast::<ErrorImpl<ContextError<C, ManuallyDrop<Error>>>>();
1028 let unerased = unsafe { unerased_own.boxed() };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1029–1031why, one step back
1029 // Read the Own<ErrorImpl> from the next error.
1030 let inner = unerased._object.error.inner;1031 drop(unerased);

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1032why, one step back
1032 let vtable = unsafe { vtable(inner.ptr) };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 1033why, one step back
1033 // Recursively drop the next error using the same target typeid.

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1034why, one step back
1034 unsafe { (vtable.object_drop_rest)(inner, target) };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1035–1038why, one step back
1035 }
1036}10371038// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

lines 1039–1040why, one step back
1039#[cfg(all(
1040 not(error_generic_member_access),

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 1041why, one step back
1041 any(std_backtrace, feature = "backtrace")

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 1042why, one step back
1042))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1043–1047why, one step back
1043#[allow(clippy::unnecessary_wraps)]
1044unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>1045where1046 C: 'static,1047{

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 1048–1050why, one step back
1048 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();
1049 let unerased = unsafe { unerased_ref.deref() };1050 let backtrace = unsafe { ErrorImpl::backtrace(unerased._object.error.inner.by_ref()) };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1051–1069why, one step back
1051 Some(backtrace)
1052}10531054// NOTE: If working with `ErrorImpl<()>`, references should be avoided in favor1055// of raw pointers and `NonNull`.1056// repr C to ensure that E remains in the final position.1057#[repr(C)]1058pub(crate) struct ErrorImpl<E = ()> {1059 vtable: &'static ErrorVTable,1060 backtrace: Option<Backtrace>,1061 // NOTE: Don't use directly. Use only through vtable. Erased type may have1062 // different alignment.1063 _object: E,1064}10651066// Reads the vtable out of `p`. This is the same as `p.as_ref().vtable`, but1067// avoids converting `p` into a reference.1068unsafe fn vtable(p: NonNull<ErrorImpl>) -> &'static ErrorVTable {1069 // NOTE: This assumes that `ErrorVTable` is the first field of ErrorImpl.

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1070why, one step back
1070 unsafe { *(p.as_ptr() as *const &'static ErrorVTable) }

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1071–1093why, one step back
1071}
10721073// repr C to ensure that ContextError<C, E> has the same layout as1074// ContextError<ManuallyDrop<C>, E> and ContextError<C, ManuallyDrop<E>>.1075#[repr(C)]1076pub(crate) struct ContextError<C, E> {1077 pub context: C,1078 pub error: E,1079}10801081impl<E> ErrorImpl<E> {1082 fn erase(&self) -> Ref<ErrorImpl> {1083 // Erase the concrete type of E but preserve the vtable in self.vtable1084 // for manipulating the resulting thin pointer. This is analogous to an1085 // unsize coercion.1086 Ref::new(self).cast::<ErrorImpl>()1087 }1088}10891090impl ErrorImpl {1091 pub(crate) unsafe fn error(this: Ref<Self>) -> &(dyn StdError + Send + Sync + 'static) {1092 // Use vtable to attach E's native StdError vtable for the right1093 // original type E.

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1094why, one step back
1094 unsafe { (vtable(this.ptr).object_ref)(this).deref() }

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1095–1096why, one step back
1095 }
1096

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 1097why, one step back
1097 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1098–1102why, one step back
1098 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {
1099 // Use vtable to attach E's native StdError vtable for the right1100 // original type E.11011102 #[cfg(not(anyhow_no_ptr_addr_of))]

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

lines 1103–1107why, one step back
1103 return unsafe {
1104 (vtable(this.ptr).object_ref)(this.by_ref())1105 .by_mut()1106 .deref_mut()1107 };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1108–1109why, one step back
1108
1109 #[cfg(anyhow_no_ptr_addr_of)]

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1110why, one step back
1110 return unsafe { (vtable(this.ptr).object_mut)(this) };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1111–1112why, one step back
1111 }
1112

Merge pull request #332 from dtolnay/stdbacktraceDavid Tolnaybefore adoption53109e6

line 1113why, one step back
1113 #[cfg(any(std_backtrace, feature = "backtrace"))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1114–1117why, one step back
1114 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {
1115 // This unwrap can only panic if the underlying error's backtrace method1116 // is nondeterministic, which would only happen in maliciously1117 // constructed code.

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1118why, one step back
1118 unsafe { this.deref() }

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1119–1121why, one step back
1119 .backtrace
1120 .as_ref()1121 .or_else(|| {

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 1122why, one step back
1122 #[cfg(error_generic_member_access)]

Merge pull request #414 from dtolnay/nightlyDavid Tolnaybefore adoption213a9c2

line 1123why, one step back
1123 return nightly::request_ref_backtrace(unsafe { Self::error(this) });

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 1124why, one step back
1124 #[cfg(not(error_generic_member_access))]

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1125why, one step back
1125 return unsafe { (vtable(this.ptr).object_backtrace)(this) };

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1126–1129why, one step back
1126 })
1127 .expect("backtrace capture failed")1128 }1129

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 1130why, one step back
1130 #[cfg(error_generic_member_access)]

Merge pull request #319 from dtolnay/errorprovideDavid Tolnaybefore adoption8cf1d15

line 1131why, one step back
1131 unsafe fn provide<'a>(this: Ref<'a, Self>, request: &mut Request<'a>) {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1132why, one step back
1132 if let Some(backtrace) = unsafe { &this.deref().backtrace } {

Merge pull request #414 from dtolnay/nightlyDavid Tolnaybefore adoption213a9c2

line 1133why, one step back
1133 nightly::provide_ref_backtrace(request, backtrace);

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 1134why, one step back
1134 }

Merge pull request #414 from dtolnay/nightlyDavid Tolnaybefore adoption213a9c2

line 1135why, one step back
1135 nightly::provide(unsafe { Self::error(this) }, request);

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1136–1139why, one step back
1136 }
11371138 #[cold]1139 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {

Merge pull request #329 from dtolnay/unsafeopDavid Tolnaybefore adoptiond371a49

line 1140why, one step back
1140 Chain::new(unsafe { Self::error(this) })

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1141–1151why, one step back
1141 }
1142}11431144impl<E> StdError for ErrorImpl<E>1145where1146 E: StdError,1147{1148 fn source(&self) -> Option<&(dyn StdError + 'static)> {1149 unsafe { ErrorImpl::error(self.erase()).source() }1150 }1151

Merge pull request #331 from dtolnay/backtraceDavid Tolnaybefore adoptioncf66194

line 1152why, one step back
1152 #[cfg(error_generic_member_access)]

Merge pull request #319 from dtolnay/errorprovideDavid Tolnaybefore adoption8cf1d15

lines 1153–1154why, one step back
1153 fn provide<'a>(&'a self, request: &mut Request<'a>) {
1154 unsafe { ErrorImpl::provide(self.erase(), request) }

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1155–1175why, one step back
1155 }
1156}11571158impl<E> Debug for ErrorImpl<E>1159where1160 E: Debug,1161{1162 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {1163 unsafe { ErrorImpl::debug(self.erase(), formatter) }1164 }1165}11661167impl<E> Display for ErrorImpl<E>1168where1169 E: Display,1170{1171 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {1172 unsafe { Display::fmt(ErrorImpl::error(self.erase()), formatter) }1173 }1174}1175

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 1176why, one step back
1176#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1177–1179why, one step back
1177impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {
1178 #[cold]1179 fn from(error: Error) -> Self {

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

line 1180why, one step back
1180 error.into_boxed_dyn_error()

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1181–1183why, one step back
1181 }
1182}1183

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 1184why, one step back
1184#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 1185why, one step back
1185impl From<Error> for Box<dyn StdError + Send + 'static> {

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

line 1186why, one step back
1186 #[cold]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 1187why, one step back
1187 fn from(error: Error) -> Self {

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

line 1188why, one step back
1188 error.into_boxed_dyn_error()

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1189–1191why, one step back
1189 }
1190}1191

Merge pull request #416 from dtolnay/oldnostdDavid Tolnaybefore adoption29f2edd

line 1192why, one step back
1192#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 1193why, one step back
1193impl From<Error> for Box<dyn StdError + 'static> {

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

line 1194why, one step back
1194 #[cold]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

line 1195why, one step back
1195 fn from(error: Error) -> Self {

Merge pull request #415 from dtolnay/intodynDavid Tolnaybefore adoptioncbc1ad2

line 1196why, one step back
1196 error.into_boxed_dyn_error()

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1197–1199why, one step back
1197 }
1198}1199

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 1200why, one step back
1200#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1201–1206why, one step back
1201impl AsRef<dyn StdError + Send + Sync> for Error {
1202 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {1203 &**self1204 }1205}1206

Merge pull request #383 from dtolnay/nostdDavid Tolnaybefore adoption8ecfcdf

line 1207why, one step back
1207#[cfg(any(feature = "std", not(anyhow_no_core_error)))]

Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac

lines 1208–1212why, one step back
1208impl AsRef<dyn StdError> for Error {
1209 fn as_ref(&self) -> &(dyn StdError + 'static) {1210 &**self1211 }1212}

Merge pull request #386 from dtolnay/unwindsafeDavid Tolnaybefore adoption830c399

lines 1213–1218why, one step back
1213
1214#[cfg(any(feature = "std", not(anyhow_no_core_unwind_safe)))]1215impl UnwindSafe for Error {}12161217#[cfg(any(feature = "std", not(anyhow_no_core_unwind_safe)))]1218impl RefUnwindSafe for Error {}