error.rs
This file
Path
src/error.rs
967 lines · 32.9 KiB · Rust
Last changed by
Merge pull request #178 from dtolnay/upDavid Tolnaybefore adoption3c586dd
History of this fileTake it
1use crate::backtrace::Backtrace;2use crate::chain::Chain;3#[cfg(any(feature = "std", anyhow_no_ptr_addr_of))]4use crate::ptr::Mut;5use crate::ptr::{Own, Ref};6use crate::{Error, StdError};7use alloc::boxed::Box;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;1415#[cfg(feature = "std")]16use core::ops::{Deref, DerefMut};1718impl Error {19 /// Create a new error object from any error type.20 ///21 /// The error type must be threadsafe and `'static`, so that the `Error`22 /// will be as well.23 ///24 /// If the error type does not provide a backtrace, a backtrace will be25 /// created here to ensure that a backtrace exists.26 #[cfg(feature = "std")]27 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]28 #[cold]29 pub fn new<E>(error: E) -> Self30 where31 E: StdError + Send + Sync + 'static,32 {33 let backtrace = backtrace_if_absent!(error);34 Error::from_std(error, backtrace)35 }3637 /// Create a new error object from a printable error message.38 ///39 /// If the argument implements std::error::Error, prefer `Error::new`40 /// instead which preserves the underlying error's cause chain and41 /// backtrace. If the argument may or may not implement std::error::Error42 /// now or in the future, use `anyhow!(err)` which handles either way43 /// correctly.44 ///45 /// `Error::msg("...")` is equivalent to `anyhow!("...")` but occasionally46 /// convenient in places where a function is preferable over a macro, such47 /// as iterator or stream combinators:48 ///49 /// ```50 /// # mod ffi {51 /// # pub struct Input;52 /// # pub struct Output;53 /// # pub async fn do_some_work(_: Input) -> Result<Output, &'static str> {54 /// # unimplemented!()55 /// # }56 /// # }57 /// #58 /// # use ffi::{Input, Output};59 /// #60 /// use anyhow::{Error, Result};61 /// use futures::stream::{Stream, StreamExt, TryStreamExt};62 ///63 /// async fn demo<S>(stream: S) -> Result<Vec<Output>>64 /// where65 /// S: Stream<Item = Input>,66 /// {67 /// stream68 /// .then(ffi::do_some_work) // returns Result<Output, &str>69 /// .map_err(Error::msg)70 /// .try_collect()71 /// .await72 /// }73 /// ```74 #[cold]75 pub fn msg<M>(message: M) -> Self76 where77 M: Display + Debug + Send + Sync + 'static,78 {79 Error::from_adhoc(message, backtrace!())80 }8182 #[cfg(feature = "std")]83 #[cold]84 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self85 where86 E: StdError + Send + Sync + 'static,87 {88 let vtable = &ErrorVTable {89 object_drop: object_drop::<E>,90 object_ref: object_ref::<E>,91 #[cfg(anyhow_no_ptr_addr_of)]92 object_mut: object_mut::<E>,93 object_boxed: object_boxed::<E>,94 object_downcast: object_downcast::<E>,95 #[cfg(anyhow_no_ptr_addr_of)]96 object_downcast_mut: object_downcast_mut::<E>,97 object_drop_rest: object_drop_front::<E>,98 #[cfg(all(not(backtrace), feature = "backtrace"))]99 object_backtrace: no_backtrace,100 };101102 // Safety: passing vtable that operates on the right type E.103 unsafe { Error::construct(error, vtable, backtrace) }104 }105106 #[cold]107 pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self108 where109 M: Display + Debug + Send + Sync + 'static,110 {111 use crate::wrapper::MessageError;112 let error: MessageError<M> = MessageError(message);113 let vtable = &ErrorVTable {114 object_drop: object_drop::<MessageError<M>>,115 object_ref: object_ref::<MessageError<M>>,116 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]117 object_mut: object_mut::<MessageError<M>>,118 object_boxed: object_boxed::<MessageError<M>>,119 object_downcast: object_downcast::<M>,120 #[cfg(anyhow_no_ptr_addr_of)]121 object_downcast_mut: object_downcast_mut::<M>,122 object_drop_rest: object_drop_front::<M>,123 #[cfg(all(not(backtrace), feature = "backtrace"))]124 object_backtrace: no_backtrace,125 };126127 // Safety: MessageError is repr(transparent) so it is okay for the128 // vtable to allow casting the MessageError<M> to M.129 unsafe { Error::construct(error, vtable, backtrace) }130 }131132 #[cold]133 pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self134 where135 M: Display + Send + Sync + 'static,136 {137 use crate::wrapper::DisplayError;138 let error: DisplayError<M> = DisplayError(message);139 let vtable = &ErrorVTable {140 object_drop: object_drop::<DisplayError<M>>,141 object_ref: object_ref::<DisplayError<M>>,142 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]143 object_mut: object_mut::<DisplayError<M>>,144 object_boxed: object_boxed::<DisplayError<M>>,145 object_downcast: object_downcast::<M>,146 #[cfg(anyhow_no_ptr_addr_of)]147 object_downcast_mut: object_downcast_mut::<M>,148 object_drop_rest: object_drop_front::<M>,149 #[cfg(all(not(backtrace), feature = "backtrace"))]150 object_backtrace: no_backtrace,151 };152153 // Safety: DisplayError is repr(transparent) so it is okay for the154 // vtable to allow casting the DisplayError<M> to M.155 unsafe { Error::construct(error, vtable, backtrace) }156 }157158 #[cfg(feature = "std")]159 #[cold]160 pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self161 where162 C: Display + Send + Sync + 'static,163 E: StdError + Send + Sync + 'static,164 {165 let error: ContextError<C, E> = ContextError { context, error };166167 let vtable = &ErrorVTable {168 object_drop: object_drop::<ContextError<C, E>>,169 object_ref: object_ref::<ContextError<C, E>>,170 #[cfg(anyhow_no_ptr_addr_of)]171 object_mut: object_mut::<ContextError<C, E>>,172 object_boxed: object_boxed::<ContextError<C, E>>,173 object_downcast: context_downcast::<C, E>,174 #[cfg(anyhow_no_ptr_addr_of)]175 object_downcast_mut: context_downcast_mut::<C, E>,176 object_drop_rest: context_drop_rest::<C, E>,177 #[cfg(all(not(backtrace), feature = "backtrace"))]178 object_backtrace: no_backtrace,179 };180181 // Safety: passing vtable that operates on the right type.182 unsafe { Error::construct(error, vtable, backtrace) }183 }184185 #[cfg(feature = "std")]186 #[cold]187 pub(crate) fn from_boxed(188 error: Box<dyn StdError + Send + Sync>,189 backtrace: Option<Backtrace>,190 ) -> Self {191 use crate::wrapper::BoxedError;192 let error = BoxedError(error);193 let vtable = &ErrorVTable {194 object_drop: object_drop::<BoxedError>,195 object_ref: object_ref::<BoxedError>,196 #[cfg(anyhow_no_ptr_addr_of)]197 object_mut: object_mut::<BoxedError>,198 object_boxed: object_boxed::<BoxedError>,199 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,200 #[cfg(anyhow_no_ptr_addr_of)]201 object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,202 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,203 #[cfg(all(not(backtrace), feature = "backtrace"))]204 object_backtrace: no_backtrace,205 };206207 // Safety: BoxedError is repr(transparent) so it is okay for the vtable208 // to allow casting to Box<dyn StdError + Send + Sync>.209 unsafe { Error::construct(error, vtable, backtrace) }210 }211212 // Takes backtrace as argument rather than capturing it here so that the213 // user sees one fewer layer of wrapping noise in the backtrace.214 //215 // Unsafe because the given vtable must have sensible behavior on the error216 // value of type E.217 #[cold]218 unsafe fn construct<E>(219 error: E,220 vtable: &'static ErrorVTable,221 backtrace: Option<Backtrace>,222 ) -> Self223 where224 E: StdError + Send + Sync + 'static,225 {226 let inner: Box<ErrorImpl<E>> = Box::new(ErrorImpl {227 vtable,228 backtrace,229 _object: error,230 });231 // Erase the concrete type of E from the compile-time type system. This232 // is equivalent to the safe unsize coercion from Box<ErrorImpl<E>> to233 // Box<ErrorImpl<dyn StdError + Send + Sync + 'static>> except that the234 // result is a thin pointer. The necessary behavior for manipulating the235 // underlying ErrorImpl<E> is preserved in the vtable provided by the236 // caller rather than a builtin fat pointer vtable.237 let inner = Own::new(inner).cast::<ErrorImpl>();238 Error { inner }239 }240241 /// Wrap the error value with additional context.242 ///243 /// For attaching context to a `Result` as it is propagated, the244 /// [`Context`][crate::Context] extension trait may be more convenient than245 /// this function.246 ///247 /// The primary reason to use `error.context(...)` instead of248 /// `result.context(...)` via the `Context` trait would be if the context249 /// needs to depend on some data held by the underlying error:250 ///251 /// ```252 /// # use std::fmt::{self, Debug, Display};253 /// #254 /// # type T = ();255 /// #256 /// # impl std::error::Error for ParseError {}257 /// # impl Debug for ParseError {258 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {259 /// # unimplemented!()260 /// # }261 /// # }262 /// # impl Display for ParseError {263 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {264 /// # unimplemented!()265 /// # }266 /// # }267 /// #268 /// use anyhow::Result;269 /// use std::fs::File;270 /// use std::path::Path;271 ///272 /// struct ParseError {273 /// line: usize,274 /// column: usize,275 /// }276 ///277 /// fn parse_impl(file: File) -> Result<T, ParseError> {278 /// # const IGNORE: &str = stringify! {279 /// ...280 /// # };281 /// # unimplemented!()282 /// }283 ///284 /// pub fn parse(path: impl AsRef<Path>) -> Result<T> {285 /// let file = File::open(&path)?;286 /// parse_impl(file).map_err(|error| {287 /// let context = format!(288 /// "only the first {} lines of {} are valid",289 /// error.line, path.as_ref().display(),290 /// );291 /// anyhow::Error::new(error).context(context)292 /// })293 /// }294 /// ```295 #[cold]296 pub fn context<C>(self, context: C) -> Self297 where298 C: Display + Send + Sync + 'static,299 {300 let error: ContextError<C, Error> = ContextError {301 context,302 error: self,303 };304305 let vtable = &ErrorVTable {306 object_drop: object_drop::<ContextError<C, Error>>,307 object_ref: object_ref::<ContextError<C, Error>>,308 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]309 object_mut: object_mut::<ContextError<C, Error>>,310 object_boxed: object_boxed::<ContextError<C, Error>>,311 object_downcast: context_chain_downcast::<C>,312 #[cfg(anyhow_no_ptr_addr_of)]313 object_downcast_mut: context_chain_downcast_mut::<C>,314 object_drop_rest: context_chain_drop_rest::<C>,315 #[cfg(all(not(backtrace), feature = "backtrace"))]316 object_backtrace: context_backtrace::<C>,317 };318319 // As the cause is anyhow::Error, we already have a backtrace for it.320 let backtrace = None;321322 // Safety: passing vtable that operates on the right type.323 unsafe { Error::construct(error, vtable, backtrace) }324 }325326 /// Get the backtrace for this Error.327 ///328 /// In order for the backtrace to be meaningful, one of the two environment329 /// variables `RUST_LIB_BACKTRACE=1` or `RUST_BACKTRACE=1` must be defined330 /// and `RUST_LIB_BACKTRACE` must not be `0`. Backtraces are somewhat331 /// expensive to capture in Rust, so we don't necessarily want to be332 /// capturing them all over the place all the time.333 ///334 /// - If you want panics and errors to both have backtraces, set335 /// `RUST_BACKTRACE=1`;336 /// - If you want only errors to have backtraces, set337 /// `RUST_LIB_BACKTRACE=1`;338 /// - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and339 /// `RUST_LIB_BACKTRACE=0`.340 ///341 /// # Stability342 ///343 /// Standard library backtraces are only available on the nightly channel.344 /// Tracking issue: [rust-lang/rust#53487][tracking].345 ///346 /// On stable compilers, this function is only available if the crate's347 /// "backtrace" feature is enabled, and will use the `backtrace` crate as348 /// the underlying backtrace implementation.349 ///350 /// ```toml351 /// [dependencies]352 /// anyhow = { version = "1.0", features = ["backtrace"] }353 /// ```354 ///355 /// [tracking]: https://github.com/rust-lang/rust/issues/53487356 #[cfg(any(backtrace, feature = "backtrace"))]357 #[cfg_attr(doc_cfg, doc(cfg(any(nightly, feature = "backtrace"))))]358 pub fn backtrace(&self) -> &impl_backtrace!() {359 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }360 }361362 /// An iterator of the chain of source errors contained by this Error.363 ///364 /// This iterator will visit every error in the cause chain of this error365 /// object, beginning with the error that this error object was created366 /// from.367 ///368 /// # Example369 ///370 /// ```371 /// use anyhow::Error;372 /// use std::io;373 ///374 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {375 /// for cause in error.chain() {376 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {377 /// return Some(io_error.kind());378 /// }379 /// }380 /// None381 /// }382 /// ```383 #[cfg(feature = "std")]384 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]385 #[cold]386 pub fn chain(&self) -> Chain {387 unsafe { ErrorImpl::chain(self.inner.by_ref()) }388 }389390 /// The lowest level cause of this error — this error's cause's391 /// cause's cause etc.392 ///393 /// The root cause is the last error in the iterator produced by394 /// [`chain()`][Error::chain].395 #[cfg(feature = "std")]396 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]397 pub fn root_cause(&self) -> &(dyn StdError + 'static) {398 self.chain().last().unwrap()399 }400401 /// Returns true if `E` is the type held by this error object.402 ///403 /// For errors with context, this method returns true if `E` matches the404 /// type of the context `C` **or** the type of the error on which the405 /// context has been attached. For details about the interaction between406 /// context and downcasting, [see here].407 ///408 /// [see here]: trait.Context.html#effect-on-downcasting409 pub fn is<E>(&self) -> bool410 where411 E: Display + Debug + Send + Sync + 'static,412 {413 self.downcast_ref::<E>().is_some()414 }415416 /// Attempt to downcast the error object to a concrete type.417 pub fn downcast<E>(mut self) -> Result<E, Self>418 where419 E: Display + Debug + Send + Sync + 'static,420 {421 let target = TypeId::of::<E>();422 let inner = self.inner.by_mut();423 unsafe {424 // Use vtable to find NonNull<()> which points to a value of type E425 // somewhere inside the data structure.426 #[cfg(not(anyhow_no_ptr_addr_of))]427 let addr = match (vtable(inner.ptr).object_downcast)(inner.by_ref(), target) {428 Some(addr) => addr.by_mut().extend(),429 None => return Err(self),430 };431 #[cfg(anyhow_no_ptr_addr_of)]432 let addr = match (vtable(inner.ptr).object_downcast_mut)(inner, target) {433 Some(addr) => addr.extend(),434 None => return Err(self),435 };436437 // Prepare to read E out of the data structure. We'll drop the rest438 // of the data structure separately so that E is not dropped.439 let outer = ManuallyDrop::new(self);440441 // Read E from where the vtable found it.442 let error = addr.cast::<E>().read();443444 // Drop rest of the data structure outside of E.445 (vtable(outer.inner.ptr).object_drop_rest)(outer.inner, target);446447 Ok(error)448 }449 }450451 /// Downcast this error object by reference.452 ///453 /// # Example454 ///455 /// ```456 /// # use anyhow::anyhow;457 /// # use std::fmt::{self, Display};458 /// # use std::task::Poll;459 /// #460 /// # #[derive(Debug)]461 /// # enum DataStoreError {462 /// # Censored(()),463 /// # }464 /// #465 /// # impl Display for DataStoreError {466 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {467 /// # unimplemented!()468 /// # }469 /// # }470 /// #471 /// # impl std::error::Error for DataStoreError {}472 /// #473 /// # const REDACTED_CONTENT: () = ();474 /// #475 /// # let error = anyhow!("...");476 /// # let root_cause = &error;477 /// #478 /// # let ret =479 /// // If the error was caused by redaction, then return a tombstone instead480 /// // of the content.481 /// match root_cause.downcast_ref::<DataStoreError>() {482 /// Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),483 /// None => Err(error),484 /// }485 /// # ;486 /// ```487 pub fn downcast_ref<E>(&self) -> Option<&E>488 where489 E: Display + Debug + Send + Sync + 'static,490 {491 let target = TypeId::of::<E>();492 unsafe {493 // Use vtable to find NonNull<()> which points to a value of type E494 // somewhere inside the data structure.495 let addr = (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?;496 Some(addr.cast::<E>().deref())497 }498 }499500 /// Downcast this error object by mutable reference.501 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>502 where503 E: Display + Debug + Send + Sync + 'static,504 {505 let target = TypeId::of::<E>();506 unsafe {507 // Use vtable to find NonNull<()> which points to a value of type E508 // somewhere inside the data structure.509510 #[cfg(not(anyhow_no_ptr_addr_of))]511 let addr =512 (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?.by_mut();513514 #[cfg(anyhow_no_ptr_addr_of)]515 let addr = (vtable(self.inner.ptr).object_downcast_mut)(self.inner.by_mut(), target)?;516517 Some(addr.cast::<E>().deref_mut())518 }519 }520}521522#[cfg(feature = "std")]523#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]524impl<E> From<E> for Error525where526 E: StdError + Send + Sync + 'static,527{528 #[cold]529 fn from(error: E) -> Self {530 let backtrace = backtrace_if_absent!(error);531 Error::from_std(error, backtrace)532 }533}534535#[cfg(feature = "std")]536#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]537impl Deref for Error {538 type Target = dyn StdError + Send + Sync + 'static;539540 fn deref(&self) -> &Self::Target {541 unsafe { ErrorImpl::error(self.inner.by_ref()) }542 }543}544545#[cfg(feature = "std")]546#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]547impl DerefMut for Error {548 fn deref_mut(&mut self) -> &mut Self::Target {549 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }550 }551}552553impl Display for Error {554 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {555 unsafe { ErrorImpl::display(self.inner.by_ref(), formatter) }556 }557}558559impl Debug for Error {560 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {561 unsafe { ErrorImpl::debug(self.inner.by_ref(), formatter) }562 }563}564565impl Drop for Error {566 fn drop(&mut self) {567 unsafe {568 // Invoke the vtable's drop behavior.569 (vtable(self.inner.ptr).object_drop)(self.inner);570 }571 }572}573574struct ErrorVTable {575 object_drop: unsafe fn(Own<ErrorImpl>),576 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,577 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]578 object_mut: unsafe fn(Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static),579 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,580 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,581 #[cfg(anyhow_no_ptr_addr_of)]582 object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,583 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),584 #[cfg(all(not(backtrace), feature = "backtrace"))]585 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,586}587588// Safety: requires layout of *e to match ErrorImpl<E>.589unsafe fn object_drop<E>(e: Own<ErrorImpl>) {590 // Cast back to ErrorImpl<E> so that the allocator receives the correct591 // Layout to deallocate the Box's memory.592 let unerased = e.cast::<ErrorImpl<E>>().boxed();593 drop(unerased);594}595596// Safety: requires layout of *e to match ErrorImpl<E>.597unsafe fn object_drop_front<E>(e: Own<ErrorImpl>, target: TypeId) {598 // Drop the fields of ErrorImpl other than E as well as the Box allocation,599 // without dropping E itself. This is used by downcast after doing a600 // ptr::read to take ownership of the E.601 let _ = target;602 let unerased = e.cast::<ErrorImpl<ManuallyDrop<E>>>().boxed();603 drop(unerased);604}605606// Safety: requires layout of *e to match ErrorImpl<E>.607unsafe fn object_ref<E>(e: Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>608where609 E: StdError + Send + Sync + 'static,610{611 // Attach E's native StdError vtable onto a pointer to self._object.612613 let unerased = e.cast::<ErrorImpl<E>>();614615 #[cfg(not(anyhow_no_ptr_addr_of))]616 return Ref::from_raw(NonNull::new_unchecked(617 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,618 ));619620 #[cfg(anyhow_no_ptr_addr_of)]621 return Ref::new(&unerased.deref()._object);622}623624// Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived625// from a `&mut`626#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]627unsafe fn object_mut<E>(e: Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static)628where629 E: StdError + Send + Sync + 'static,630{631 // Attach E's native StdError vtable onto a pointer to self._object.632 &mut e.cast::<ErrorImpl<E>>().deref_mut()._object633}634635// Safety: requires layout of *e to match ErrorImpl<E>.636unsafe fn object_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>637where638 E: StdError + Send + Sync + 'static,639{640 // Attach ErrorImpl<E>'s native StdError vtable. The StdError impl is below.641 e.cast::<ErrorImpl<E>>().boxed()642}643644// Safety: requires layout of *e to match ErrorImpl<E>.645unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>646where647 E: 'static,648{649 if TypeId::of::<E>() == target {650 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a651 // pointer to its E field.652653 let unerased = e.cast::<ErrorImpl<E>>();654655 #[cfg(not(anyhow_no_ptr_addr_of))]656 return Some(657 Ref::from_raw(NonNull::new_unchecked(658 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,659 ))660 .cast::<()>(),661 );662663 #[cfg(anyhow_no_ptr_addr_of)]664 return Some(Ref::new(&unerased.deref()._object).cast::<()>());665 } else {666 None667 }668}669670// Safety: requires layout of *e to match ErrorImpl<E>.671#[cfg(anyhow_no_ptr_addr_of)]672unsafe fn object_downcast_mut<E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>673where674 E: 'static,675{676 if TypeId::of::<E>() == target {677 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a678 // pointer to its E field.679 let unerased = e.cast::<ErrorImpl<E>>().deref_mut();680 Some(Mut::new(&mut unerased._object).cast::<()>())681 } else {682 None683 }684}685686#[cfg(all(not(backtrace), feature = "backtrace"))]687fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {688 let _ = e;689 None690}691692// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.693#[cfg(feature = "std")]694unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>695where696 C: 'static,697 E: 'static,698{699 if TypeId::of::<C>() == target {700 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();701 Some(Ref::new(&unerased._object.context).cast::<()>())702 } else if TypeId::of::<E>() == target {703 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();704 Some(Ref::new(&unerased._object.error).cast::<()>())705 } else {706 None707 }708}709710// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.711#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]712unsafe fn context_downcast_mut<C, E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>713where714 C: 'static,715 E: 'static,716{717 if TypeId::of::<C>() == target {718 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();719 Some(Mut::new(&mut unerased._object.context).cast::<()>())720 } else if TypeId::of::<E>() == target {721 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();722 Some(Mut::new(&mut unerased._object.error).cast::<()>())723 } else {724 None725 }726}727728// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.729#[cfg(feature = "std")]730unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)731where732 C: 'static,733 E: 'static,734{735 // Called after downcasting by value to either the C or the E and doing a736 // ptr::read to take ownership of that value.737 if TypeId::of::<C>() == target {738 let unerased = e739 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, E>>>()740 .boxed();741 drop(unerased);742 } else {743 let unerased = e744 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<E>>>>()745 .boxed();746 drop(unerased);747 }748}749750// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.751unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>752where753 C: 'static,754{755 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();756 if TypeId::of::<C>() == target {757 Some(Ref::new(&unerased._object.context).cast::<()>())758 } else {759 // Recurse down the context chain per the inner error's vtable.760 let source = &unerased._object.error;761 (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target)762 }763}764765// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.766#[cfg(anyhow_no_ptr_addr_of)]767unsafe fn context_chain_downcast_mut<C>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>768where769 C: 'static,770{771 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();772 if TypeId::of::<C>() == target {773 Some(Mut::new(&mut unerased._object.context).cast::<()>())774 } else {775 // Recurse down the context chain per the inner error's vtable.776 let source = &mut unerased._object.error;777 (vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target)778 }779}780781// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.782unsafe fn context_chain_drop_rest<C>(e: Own<ErrorImpl>, target: TypeId)783where784 C: 'static,785{786 // Called after downcasting by value to either the C or one of the causes787 // and doing a ptr::read to take ownership of that value.788 if TypeId::of::<C>() == target {789 let unerased = e790 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, Error>>>()791 .boxed();792 // Drop the entire rest of the data structure rooted in the next Error.793 drop(unerased);794 } else {795 let unerased = e796 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<Error>>>>()797 .boxed();798 // Read the Own<ErrorImpl> from the next error.799 let inner = unerased._object.error.inner;800 drop(unerased);801 let vtable = vtable(inner.ptr);802 // Recursively drop the next error using the same target typeid.803 (vtable.object_drop_rest)(inner, target);804 }805}806807// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.808#[cfg(all(not(backtrace), feature = "backtrace"))]809#[allow(clippy::unnecessary_wraps)]810unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>811where812 C: 'static,813{814 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();815 let backtrace = ErrorImpl::backtrace(unerased._object.error.inner.by_ref());816 Some(backtrace)817}818819// NOTE: If working with `ErrorImpl<()>`, references should be avoided in favor820// of raw pointers and `NonNull`.821// repr C to ensure that E remains in the final position.822#[repr(C)]823pub(crate) struct ErrorImpl<E = ()> {824 vtable: &'static ErrorVTable,825 backtrace: Option<Backtrace>,826 // NOTE: Don't use directly. Use only through vtable. Erased type may have827 // different alignment.828 _object: E,829}830831// Reads the vtable out of `p`. This is the same as `p.as_ref().vtable`, but832// avoids converting `p` into a reference.833unsafe fn vtable(p: NonNull<ErrorImpl>) -> &'static ErrorVTable {834 // NOTE: This assumes that `ErrorVTable` is the first field of ErrorImpl.835 *(p.as_ptr() as *const &'static ErrorVTable)836}837838// repr C to ensure that ContextError<C, E> has the same layout as839// ContextError<ManuallyDrop<C>, E> and ContextError<C, ManuallyDrop<E>>.840#[repr(C)]841pub(crate) struct ContextError<C, E> {842 pub context: C,843 pub error: E,844}845846impl<E> ErrorImpl<E> {847 fn erase(&self) -> Ref<ErrorImpl> {848 // Erase the concrete type of E but preserve the vtable in self.vtable849 // for manipulating the resulting thin pointer. This is analogous to an850 // unsize coercion.851 Ref::new(self).cast::<ErrorImpl>()852 }853}854855impl ErrorImpl {856 pub(crate) unsafe fn error(this: Ref<Self>) -> &(dyn StdError + Send + Sync + 'static) {857 // Use vtable to attach E's native StdError vtable for the right858 // original type E.859 (vtable(this.ptr).object_ref)(this).deref()860 }861862 #[cfg(feature = "std")]863 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {864 // Use vtable to attach E's native StdError vtable for the right865 // original type E.866867 #[cfg(not(anyhow_no_ptr_addr_of))]868 return (vtable(this.ptr).object_ref)(this.by_ref())869 .by_mut()870 .deref_mut();871872 #[cfg(anyhow_no_ptr_addr_of)]873 return (vtable(this.ptr).object_mut)(this);874 }875876 #[cfg(any(backtrace, feature = "backtrace"))]877 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {878 // This unwrap can only panic if the underlying error's backtrace method879 // is nondeterministic, which would only happen in maliciously880 // constructed code.881 this.deref()882 .backtrace883 .as_ref()884 .or_else(|| {885 #[cfg(backtrace)]886 return Self::error(this).backtrace();887 #[cfg(all(not(backtrace), feature = "backtrace"))]888 return (vtable(this.ptr).object_backtrace)(this);889 })890 .expect("backtrace capture failed")891 }892893 #[cold]894 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {895 Chain::new(Self::error(this))896 }897}898899impl<E> StdError for ErrorImpl<E>900where901 E: StdError,902{903 #[cfg(backtrace)]904 fn backtrace(&self) -> Option<&Backtrace> {905 Some(unsafe { ErrorImpl::backtrace(self.erase()) })906 }907908 fn source(&self) -> Option<&(dyn StdError + 'static)> {909 unsafe { ErrorImpl::error(self.erase()).source() }910 }911}912913impl<E> Debug for ErrorImpl<E>914where915 E: Debug,916{917 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {918 unsafe { ErrorImpl::debug(self.erase(), formatter) }919 }920}921922impl<E> Display for ErrorImpl<E>923where924 E: Display,925{926 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {927 unsafe { Display::fmt(ErrorImpl::error(self.erase()), formatter) }928 }929}930931impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {932 #[cold]933 fn from(error: Error) -> Self {934 let outer = ManuallyDrop::new(error);935 unsafe {936 // Use vtable to attach ErrorImpl<E>'s native StdError vtable for937 // the right original type E.938 (vtable(outer.inner.ptr).object_boxed)(outer.inner)939 }940 }941}942943impl From<Error> for Box<dyn StdError + Send + 'static> {944 fn from(error: Error) -> Self {945 Box::<dyn StdError + Send + Sync>::from(error)946 }947}948949impl From<Error> for Box<dyn StdError + 'static> {950 fn from(error: Error) -> Self {951 Box::<dyn StdError + Send + Sync>::from(error)952 }953}954955#[cfg(feature = "std")]956impl AsRef<dyn StdError + Send + Sync> for Error {957 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {958 &**self959 }960}961962#[cfg(feature = "std")]963impl AsRef<dyn StdError> for Error {964 fn as_ref(&self) -> &(dyn StdError + 'static) {965 &**self966 }967}