error.rs
This file
Path
src/error.rs
992 lines · 33.8 KiB · Rust
Last changed by
Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac
History of this fileTake it
The walk stopped after 200 commits: lines older than 5e1a1f8 point at it.
Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 1–2why, one step back1use crate::backtrace::Backtrace;2use crate::chain::Chain;Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 3–5why, one step back3#[cfg(any(feature = "std", anyhow_no_ptr_addr_of))]4use crate::ptr::Mut;5use crate::ptr::{Own, Ref};Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 6why, one step back6use crate::{Error, StdError};Merge pull request #178 from dtolnay/upDavid Tolnaybefore adoption3c586dd
line 7why, one step back7use alloc::boxed::Box;Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c
lines 8–9why, one step back8#[cfg(backtrace)]9use core::any::Demand;Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 10–11why, one step back10use core::any::TypeId;11use core::fmt::{self, Debug, Display};Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 12why, one step back12use core::mem::ManuallyDrop;Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 13–14why, one step back13#[cfg(not(anyhow_no_ptr_addr_of))]14use core::ptr;Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 15why, one step back15use core::ptr::NonNull;Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 16–29why, one step back1617#[cfg(feature = "std")]18use core::ops::{Deref, DerefMut};1920impl Error {21 /// Create a new error object from any error type.22 ///23 /// The error type must be threadsafe and `'static`, so that the `Error`24 /// will be as well.25 ///26 /// If the error type does not provide a backtrace, a backtrace will be27 /// created here to ensure that a backtrace exists.28 #[cfg(feature = "std")]29 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 30why, one step back30 #[cold]Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42
line 31why, one step back31 #[must_use]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 32–35why, one step back32 pub fn new<E>(error: E) -> Self33 where34 E: StdError + Send + Sync + 'static,35 {Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c
line 36why, one step back36 let backtrace = backtrace_if_absent!(&error);Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 37–76why, one step back37 Error::from_std(error, backtrace)38 }3940 /// Create a new error object from a printable error message.41 ///42 /// If the argument implements std::error::Error, prefer `Error::new`43 /// instead which preserves the underlying error's cause chain and44 /// backtrace. If the argument may or may not implement std::error::Error45 /// now or in the future, use `anyhow!(err)` which handles either way46 /// correctly.47 ///48 /// `Error::msg("...")` is equivalent to `anyhow!("...")` but occasionally49 /// convenient in places where a function is preferable over a macro, such50 /// as iterator or stream combinators:51 ///52 /// ```53 /// # mod ffi {54 /// # pub struct Input;55 /// # pub struct Output;56 /// # pub async fn do_some_work(_: Input) -> Result<Output, &'static str> {57 /// # unimplemented!()58 /// # }59 /// # }60 /// #61 /// # use ffi::{Input, Output};62 /// #63 /// use anyhow::{Error, Result};64 /// use futures::stream::{Stream, StreamExt, TryStreamExt};65 ///66 /// async fn demo<S>(stream: S) -> Result<Vec<Output>>67 /// where68 /// S: Stream<Item = Input>,69 /// {70 /// stream71 /// .then(ffi::do_some_work) // returns Result<Output, &str>72 /// .map_err(Error::msg)73 /// .try_collect()74 /// .await75 /// }76 /// ```Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 77why, one step back77 #[cold]Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42
line 78why, one step back78 #[must_use]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 79–86why, one step back79 pub fn msg<M>(message: M) -> Self80 where81 M: Display + Debug + Send + Sync + 'static,82 {83 Error::from_adhoc(message, backtrace!())84 }8586 #[cfg(feature = "std")]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 87why, one step back87 #[cold]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 88–94why, one step back88 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self89 where90 E: StdError + Send + Sync + 'static,91 {92 let vtable = &ErrorVTable {93 object_drop: object_drop::<E>,94 object_ref: object_ref::<E>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 95why, one step back95 #[cfg(anyhow_no_ptr_addr_of)]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 96–98why, one step back96 object_mut: object_mut::<E>,97 object_boxed: object_boxed::<E>,98 object_downcast: object_downcast::<E>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 99why, one step back99 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 100why, one step back100 object_downcast_mut: object_downcast_mut::<E>,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 101why, one step back101 object_drop_rest: object_drop_front::<E>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 102–103why, one step back102 #[cfg(all(not(backtrace), feature = "backtrace"))]103 object_backtrace: no_backtrace,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 104–109why, one step back104 };105106 // Safety: passing vtable that operates on the right type E.107 unsafe { Error::construct(error, vtable, backtrace) }108 }109Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 110why, one step back110 #[cold]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 111–119why, one step back111 pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self112 where113 M: Display + Debug + Send + Sync + 'static,114 {115 use crate::wrapper::MessageError;116 let error: MessageError<M> = MessageError(message);117 let vtable = &ErrorVTable {118 object_drop: object_drop::<MessageError<M>>,119 object_ref: object_ref::<MessageError<M>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 120why, one step back120 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 121–123why, one step back121 object_mut: object_mut::<MessageError<M>>,122 object_boxed: object_boxed::<MessageError<M>>,123 object_downcast: object_downcast::<M>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 124why, one step back124 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 125why, one step back125 object_downcast_mut: object_downcast_mut::<M>,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 126why, one step back126 object_drop_rest: object_drop_front::<M>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 127–128why, one step back127 #[cfg(all(not(backtrace), feature = "backtrace"))]128 object_backtrace: no_backtrace,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 129–135why, one step back129 };130131 // Safety: MessageError is repr(transparent) so it is okay for the132 // vtable to allow casting the MessageError<M> to M.133 unsafe { Error::construct(error, vtable, backtrace) }134 }135Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 136why, one step back136 #[cold]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 137–145why, one step back137 pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self138 where139 M: Display + Send + Sync + 'static,140 {141 use crate::wrapper::DisplayError;142 let error: DisplayError<M> = DisplayError(message);143 let vtable = &ErrorVTable {144 object_drop: object_drop::<DisplayError<M>>,145 object_ref: object_ref::<DisplayError<M>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 146why, one step back146 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 147–149why, one step back147 object_mut: object_mut::<DisplayError<M>>,148 object_boxed: object_boxed::<DisplayError<M>>,149 object_downcast: object_downcast::<M>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 150why, one step back150 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 151why, one step back151 object_downcast_mut: object_downcast_mut::<M>,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 152why, one step back152 object_drop_rest: object_drop_front::<M>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 153–154why, one step back153 #[cfg(all(not(backtrace), feature = "backtrace"))]154 object_backtrace: no_backtrace,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 155–162why, one step back155 };156157 // Safety: DisplayError is repr(transparent) so it is okay for the158 // vtable to allow casting the DisplayError<M> to M.159 unsafe { Error::construct(error, vtable, backtrace) }160 }161162 #[cfg(feature = "std")]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 163why, one step back163 #[cold]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 164–173why, one step back164 pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self165 where166 C: Display + Send + Sync + 'static,167 E: StdError + Send + Sync + 'static,168 {169 let error: ContextError<C, E> = ContextError { context, error };170171 let vtable = &ErrorVTable {172 object_drop: object_drop::<ContextError<C, E>>,173 object_ref: object_ref::<ContextError<C, E>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 174why, one step back174 #[cfg(anyhow_no_ptr_addr_of)]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 175–177why, one step back175 object_mut: object_mut::<ContextError<C, E>>,176 object_boxed: object_boxed::<ContextError<C, E>>,177 object_downcast: context_downcast::<C, E>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 178why, one step back178 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 179why, one step back179 object_downcast_mut: context_downcast_mut::<C, E>,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 180why, one step back180 object_drop_rest: context_drop_rest::<C, E>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 181–182why, one step back181 #[cfg(all(not(backtrace), feature = "backtrace"))]182 object_backtrace: no_backtrace,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 183–189why, one step back183 };184185 // Safety: passing vtable that operates on the right type.186 unsafe { Error::construct(error, vtable, backtrace) }187 }188189 #[cfg(feature = "std")]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 190why, one step back190 #[cold]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 191–199why, one step back191 pub(crate) fn from_boxed(192 error: Box<dyn StdError + Send + Sync>,193 backtrace: Option<Backtrace>,194 ) -> Self {195 use crate::wrapper::BoxedError;196 let error = BoxedError(error);197 let vtable = &ErrorVTable {198 object_drop: object_drop::<BoxedError>,199 object_ref: object_ref::<BoxedError>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 200why, one step back200 #[cfg(anyhow_no_ptr_addr_of)]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 201–203why, one step back201 object_mut: object_mut::<BoxedError>,202 object_boxed: object_boxed::<BoxedError>,203 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 204why, one step back204 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 205why, one step back205 object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 206why, one step back206 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 207–208why, one step back207 #[cfg(all(not(backtrace), feature = "backtrace"))]208 object_backtrace: no_backtrace,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 209–220why, one step back209 };210211 // Safety: BoxedError is repr(transparent) so it is okay for the vtable212 // to allow casting to Box<dyn StdError + Send + Sync>.213 unsafe { Error::construct(error, vtable, backtrace) }214 }215216 // Takes backtrace as argument rather than capturing it here so that the217 // user sees one fewer layer of wrapping noise in the backtrace.218 //219 // Unsafe because the given vtable must have sensible behavior on the error220 // value of type E.Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 221why, one step back221 #[cold]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 222–229why, one step back222 unsafe fn construct<E>(223 error: E,224 vtable: &'static ErrorVTable,225 backtrace: Option<Backtrace>,226 ) -> Self227 where228 E: StdError + Send + Sync + 'static,229 {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 230why, one step back230 let inner: Box<ErrorImpl<E>> = Box::new(ErrorImpl {Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 231–235why, one step back231 vtable,232 backtrace,233 _object: error,234 });235 // Erase the concrete type of E from the compile-time type system. ThisFix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b
line 236why, one step back236 // is equivalent to the safe unsize coercion from Box<ErrorImpl<E>> toTry out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 237–240why, one step back237 // Box<ErrorImpl<dyn StdError + Send + Sync + 'static>> except that the238 // result is a thin pointer. The necessary behavior for manipulating the239 // underlying ErrorImpl<E> is preserved in the vtable provided by the240 // caller rather than a builtin fat pointer vtable.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 241why, one step back241 let inner = Own::new(inner).cast::<ErrorImpl>();Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 242–298why, one step back242 Error { inner }243 }244245 /// Wrap the error value with additional context.246 ///247 /// For attaching context to a `Result` as it is propagated, the248 /// [`Context`][crate::Context] extension trait may be more convenient than249 /// this function.250 ///251 /// The primary reason to use `error.context(...)` instead of252 /// `result.context(...)` via the `Context` trait would be if the context253 /// needs to depend on some data held by the underlying error:254 ///255 /// ```256 /// # use std::fmt::{self, Debug, Display};257 /// #258 /// # type T = ();259 /// #260 /// # impl std::error::Error for ParseError {}261 /// # impl Debug for ParseError {262 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {263 /// # unimplemented!()264 /// # }265 /// # }266 /// # impl Display for ParseError {267 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {268 /// # unimplemented!()269 /// # }270 /// # }271 /// #272 /// use anyhow::Result;273 /// use std::fs::File;274 /// use std::path::Path;275 ///276 /// struct ParseError {277 /// line: usize,278 /// column: usize,279 /// }280 ///281 /// fn parse_impl(file: File) -> Result<T, ParseError> {282 /// # const IGNORE: &str = stringify! {283 /// ...284 /// # };285 /// # unimplemented!()286 /// }287 ///288 /// pub fn parse(path: impl AsRef<Path>) -> Result<T> {289 /// let file = File::open(&path)?;290 /// parse_impl(file).map_err(|error| {291 /// let context = format!(292 /// "only the first {} lines of {} are valid",293 /// error.line, path.as_ref().display(),294 /// );295 /// anyhow::Error::new(error).context(context)296 /// })297 /// }298 /// ```Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 299why, one step back299 #[cold]Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42
line 300why, one step back300 #[must_use]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 301–312why, one step back301 pub fn context<C>(self, context: C) -> Self302 where303 C: Display + Send + Sync + 'static,304 {305 let error: ContextError<C, Error> = ContextError {306 context,307 error: self,308 };309310 let vtable = &ErrorVTable {311 object_drop: object_drop::<ContextError<C, Error>>,312 object_ref: object_ref::<ContextError<C, Error>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 313why, one step back313 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 314–316why, one step back314 object_mut: object_mut::<ContextError<C, Error>>,315 object_boxed: object_boxed::<ContextError<C, Error>>,316 object_downcast: context_chain_downcast::<C>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 317why, one step back317 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 318why, one step back318 object_downcast_mut: context_chain_downcast_mut::<C>,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 319why, one step back319 object_drop_rest: context_chain_drop_rest::<C>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 320–321why, one step back320 #[cfg(all(not(backtrace), feature = "backtrace"))]321 object_backtrace: context_backtrace::<C>,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 322–345why, one step back322 };323324 // As the cause is anyhow::Error, we already have a backtrace for it.325 let backtrace = None;326327 // Safety: passing vtable that operates on the right type.328 unsafe { Error::construct(error, vtable, backtrace) }329 }330331 /// Get the backtrace for this Error.332 ///333 /// In order for the backtrace to be meaningful, one of the two environment334 /// variables `RUST_LIB_BACKTRACE=1` or `RUST_BACKTRACE=1` must be defined335 /// and `RUST_LIB_BACKTRACE` must not be `0`. Backtraces are somewhat336 /// expensive to capture in Rust, so we don't necessarily want to be337 /// capturing them all over the place all the time.338 ///339 /// - If you want panics and errors to both have backtraces, set340 /// `RUST_BACKTRACE=1`;341 /// - If you want only errors to have backtraces, set342 /// `RUST_LIB_BACKTRACE=1`;343 /// - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and344 /// `RUST_LIB_BACKTRACE=0`.345 ///Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 346–359why, one step back346 /// # Stability347 ///348 /// Standard library backtraces are only available on the nightly channel.349 /// Tracking issue: [rust-lang/rust#53487][tracking].350 ///351 /// On stable compilers, this function is only available if the crate's352 /// "backtrace" feature is enabled, and will use the `backtrace` crate as353 /// the underlying backtrace implementation.354 ///355 /// ```toml356 /// [dependencies]357 /// anyhow = { version = "1.0", features = ["backtrace"] }358 /// ```359 ///Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 360why, one step back360 /// [tracking]: https://github.com/rust-lang/rust/issues/53487Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 361–363why, one step back361 #[cfg(any(backtrace, feature = "backtrace"))]362 #[cfg_attr(doc_cfg, doc(cfg(any(nightly, feature = "backtrace"))))]363 pub fn backtrace(&self) -> &impl_backtrace!() {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 364why, one step back364 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 365–388why, one step back365 }366367 /// An iterator of the chain of source errors contained by this Error.368 ///369 /// This iterator will visit every error in the cause chain of this error370 /// object, beginning with the error that this error object was created371 /// from.372 ///373 /// # Example374 ///375 /// ```376 /// use anyhow::Error;377 /// use std::io;378 ///379 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {380 /// for cause in error.chain() {381 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {382 /// return Some(io_error.kind());383 /// }384 /// }385 /// None386 /// }387 /// ```388 #[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 389why, one step back389 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 390why, one step back390 #[cold]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 391why, one step back391 pub fn chain(&self) -> Chain {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 392why, one step back392 unsafe { ErrorImpl::chain(self.inner.by_ref()) }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 393–400why, one step back393 }394395 /// The lowest level cause of this error — this error's cause's396 /// cause's cause etc.397 ///398 /// The root cause is the last error in the iterator produced by399 /// [`chain()`][Error::chain].400 #[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 401why, one step back401 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 402why, one step back402 pub fn root_cause(&self) -> &(dyn StdError + 'static) {Merge pull request #108 from hbina/patch-1David Tolnaybefore adoption7954754
line 403why, one step back403 self.chain().last().unwrap()Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 404–421why, one step back404 }405406 /// Returns true if `E` is the type held by this error object.407 ///408 /// For errors with context, this method returns true if `E` matches the409 /// type of the context `C` **or** the type of the error on which the410 /// context has been attached. For details about the interaction between411 /// context and downcasting, [see here].412 ///413 /// [see here]: trait.Context.html#effect-on-downcasting414 pub fn is<E>(&self) -> bool415 where416 E: Display + Debug + Send + Sync + 'static,417 {418 self.downcast_ref::<E>().is_some()419 }420421 /// Attempt to downcast the error object to a concrete type.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 422why, one step back422 pub fn downcast<E>(mut self) -> Result<E, Self>Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 423–426why, one step back423 where424 E: Display + Debug + Send + Sync + 'static,425 {426 let target = TypeId::of::<E>();Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 427why, one step back427 let inner = self.inner.by_mut();Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 428–430why, one step back428 unsafe {429 // Use vtable to find NonNull<()> which points to a value of type E430 // somewhere inside the data structure.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 431–440why, one step back431 #[cfg(not(anyhow_no_ptr_addr_of))]432 let addr = match (vtable(inner.ptr).object_downcast)(inner.by_ref(), target) {433 Some(addr) => addr.by_mut().extend(),434 None => return Err(self),435 };436 #[cfg(anyhow_no_ptr_addr_of)]437 let addr = match (vtable(inner.ptr).object_downcast_mut)(inner, target) {438 Some(addr) => addr.extend(),439 None => return Err(self),440 };Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 441–446why, one step back441442 // Prepare to read E out of the data structure. We'll drop the rest443 // of the data structure separately so that E is not dropped.444 let outer = ManuallyDrop::new(self);445446 // Read E from where the vtable found it.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 447why, one step back447 let error = addr.cast::<E>().read();Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 448–449why, one step back448449 // Drop rest of the data structure outside of E.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 450why, one step back450 (vtable(outer.inner.ptr).object_drop_rest)(outer.inner, target);Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 451–499why, one step back451452 Ok(error)453 }454 }455456 /// Downcast this error object by reference.457 ///458 /// # Example459 ///460 /// ```461 /// # use anyhow::anyhow;462 /// # use std::fmt::{self, Display};463 /// # use std::task::Poll;464 /// #465 /// # #[derive(Debug)]466 /// # enum DataStoreError {467 /// # Censored(()),468 /// # }469 /// #470 /// # impl Display for DataStoreError {471 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {472 /// # unimplemented!()473 /// # }474 /// # }475 /// #476 /// # impl std::error::Error for DataStoreError {}477 /// #478 /// # const REDACTED_CONTENT: () = ();479 /// #480 /// # let error = anyhow!("...");481 /// # let root_cause = &error;482 /// #483 /// # let ret =484 /// // If the error was caused by redaction, then return a tombstone instead485 /// // of the content.486 /// match root_cause.downcast_ref::<DataStoreError>() {487 /// Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),488 /// None => Err(error),489 /// }490 /// # ;491 /// ```492 pub fn downcast_ref<E>(&self) -> Option<&E>493 where494 E: Display + Debug + Send + Sync + 'static,495 {496 let target = TypeId::of::<E>();497 unsafe {498 // Use vtable to find NonNull<()> which points to a value of type E499 // somewhere inside the data structure.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 500–501why, one step back500 let addr = (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?;501 Some(addr.cast::<E>().deref())Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 502–513why, one step back502 }503 }504505 /// Downcast this error object by mutable reference.506 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>507 where508 E: Display + Debug + Send + Sync + 'static,509 {510 let target = TypeId::of::<E>();511 unsafe {512 // Use vtable to find NonNull<()> which points to a value of type E513 // somewhere inside the data structure.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 514–519why, one step back514515 #[cfg(not(anyhow_no_ptr_addr_of))]516 let addr =517 (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?.by_mut();518519 #[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 520why, one step back520 let addr = (vtable(self.inner.ptr).object_downcast_mut)(self.inner.by_mut(), target)?;Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 521why, one step back521Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 522why, one step back522 Some(addr.cast::<E>().deref_mut())Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 523–524why, one step back523 }524 }Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac
line 525why, one step back525}Merge pull request #268 from dtolnay/provideDavid Tolnaybefore adoption480e480
line 526why, one step back526Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac
lines 527–528why, one step back527#[cfg(backtrace)]528impl std::any::Provider for Error {Merge pull request #268 from dtolnay/provideDavid Tolnaybefore adoption480e480
lines 529–533why, one step back529 // Called by thiserror when you have `#[source] anyhow::Error`. This provide530 // implementation includes the anyhow::Error's Backtrace if any, unlike531 // deref'ing to dyn Error where the provide implementation would include532 // only the original error's Backtrace from before it got wrapped into an533 // anyhow::Error.Merge pull request #271 from dtolnay/providerDavid Tolnaybefore adoption07acdac
line 534why, one step back534 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {Merge pull request #268 from dtolnay/provideDavid Tolnaybefore adoption480e480
lines 535–536why, one step back535 unsafe { ErrorImpl::provide(self.inner.by_ref(), demand) }536 }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 537–539why, one step back537}538539#[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 540why, one step back540#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 541–544why, one step back541impl<E> From<E> for Error542where543 E: StdError + Send + Sync + 'static,544{Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 545why, one step back545 #[cold]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 546why, one step back546 fn from(error: E) -> Self {Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c
line 547why, one step back547 let backtrace = backtrace_if_absent!(&error);Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 548–552why, one step back548 Error::from_std(error, backtrace)549 }550}551552#[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 553why, one step back553#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 554–557why, one step back554impl Deref for Error {555 type Target = dyn StdError + Send + Sync + 'static;556557 fn deref(&self) -> &Self::Target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 558why, one step back558 unsafe { ErrorImpl::error(self.inner.by_ref()) }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 559–562why, one step back559 }560}561562#[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 563why, one step back563#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 564–565why, one step back564impl DerefMut for Error {565 fn deref_mut(&mut self) -> &mut Self::Target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 566why, one step back566 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 567–571why, one step back567 }568}569570impl Display for Error {571 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 572why, one step back572 unsafe { ErrorImpl::display(self.inner.by_ref(), formatter) }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 573–577why, one step back573 }574}575576impl Debug for Error {577 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 578why, one step back578 unsafe { ErrorImpl::debug(self.inner.by_ref(), formatter) }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 579–585why, one step back579 }580}581582impl Drop for Error {583 fn drop(&mut self) {584 unsafe {585 // Invoke the vtable's drop behavior.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 586why, one step back586 (vtable(self.inner.ptr).object_drop)(self.inner);Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 587–591why, one step back587 }588 }589}590591struct ErrorVTable {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 592why, one step back592 object_drop: unsafe fn(Own<ErrorImpl>),Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 593–594why, one step back593 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,594 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 595–597why, one step back595 object_mut: unsafe fn(Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static),596 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,597 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 598why, one step back598 #[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 599–600why, one step back599 object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,600 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 601–602why, one step back601 #[cfg(all(not(backtrace), feature = "backtrace"))]602 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 603–605why, one step back603}604605// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 606why, one step back606unsafe fn object_drop<E>(e: Own<ErrorImpl>) {Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 607–608why, one step back607 // Cast back to ErrorImpl<E> so that the allocator receives the correct608 // Layout to deallocate the Box's memory.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 609why, one step back609 let unerased = e.cast::<ErrorImpl<E>>().boxed();Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 610–613why, one step back610 drop(unerased);611}612613// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 614why, one step back614unsafe fn object_drop_front<E>(e: Own<ErrorImpl>, target: TypeId) {Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 615–618why, one step back615 // Drop the fields of ErrorImpl other than E as well as the Box allocation,616 // without dropping E itself. This is used by downcast after doing a617 // ptr::read to take ownership of the E.618 let _ = target;Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 619why, one step back619 let unerased = e.cast::<ErrorImpl<ManuallyDrop<E>>>().boxed();Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 620–623why, one step back620 drop(unerased);621}622623// Safety: requires layout of *e to match ErrorImpl<E>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 624why, one step back624unsafe fn object_ref<E>(e: Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 625–628why, one step back625where626 E: StdError + Send + Sync + 'static,627{628 // Attach E's native StdError vtable onto a pointer to self._object.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 629–638why, one step back629630 let unerased = e.cast::<ErrorImpl<E>>();631632 #[cfg(not(anyhow_no_ptr_addr_of))]633 return Ref::from_raw(NonNull::new_unchecked(634 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,635 ));636637 #[cfg(anyhow_no_ptr_addr_of)]638 return Ref::new(&unerased.deref()._object);Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 639–640why, one step back639}640Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 641–642why, one step back641// Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived642// from a `&mut`Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 643why, one step back643#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 644why, one step back644unsafe fn object_mut<E>(e: Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static)Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 645–648why, one step back645where646 E: StdError + Send + Sync + 'static,647{648 // Attach E's native StdError vtable onto a pointer to self._object.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 649why, one step back649 &mut e.cast::<ErrorImpl<E>>().deref_mut()._objectTry out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 650–652why, one step back650}651652// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 653why, one step back653unsafe fn object_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 654–657why, one step back654where655 E: StdError + Send + Sync + 'static,656{657 // Attach ErrorImpl<E>'s native StdError vtable. The StdError impl is below.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 658why, one step back658 e.cast::<ErrorImpl<E>>().boxed()Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 659–661why, one step back659}660661// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 662why, one step back662unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 663–668why, one step back663where664 E: 'static,665{666 if TypeId::of::<E>() == target {667 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a668 // pointer to its E field.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 669–681why, one step back669670 let unerased = e.cast::<ErrorImpl<E>>();671672 #[cfg(not(anyhow_no_ptr_addr_of))]673 return Some(674 Ref::from_raw(NonNull::new_unchecked(675 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,676 ))677 .cast::<()>(),678 );679680 #[cfg(anyhow_no_ptr_addr_of)]681 return Some(Ref::new(&unerased.deref()._object).cast::<()>());Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 682–686why, one step back682 } else {683 None684 }685}686Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 687why, one step back687// Safety: requires layout of *e to match ErrorImpl<E>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 688why, one step back688#[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 689why, one step back689unsafe fn object_downcast_mut<E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 690–695why, one step back690where691 E: 'static,692{693 if TypeId::of::<E>() == target {694 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a695 // pointer to its E field.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 696–697why, one step back696 let unerased = e.cast::<ErrorImpl<E>>().deref_mut();697 Some(Mut::new(&mut unerased._object).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 698–702why, one step back698 } else {699 None700 }701}702Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 703–708why, one step back703#[cfg(all(not(backtrace), feature = "backtrace"))]704fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {705 let _ = e;706 None707}708Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 709–710why, one step back709// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.710#[cfg(feature = "std")]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 711why, one step back711unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 712–716why, one step back712where713 C: 'static,714 E: 'static,715{716 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 717–718why, one step back717 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();718 Some(Ref::new(&unerased._object.context).cast::<()>())Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 719why, one step back719 } else if TypeId::of::<E>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 720–721why, one step back720 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();721 Some(Ref::new(&unerased._object.error).cast::<()>())Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 722–727why, one step back722 } else {723 None724 }725}726727// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 728why, one step back728#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 729why, one step back729unsafe fn context_downcast_mut<C, E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 730–733why, one step back730where731 C: 'static,732 E: 'static,733{Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 734why, one step back734 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 735–736why, one step back735 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();736 Some(Mut::new(&mut unerased._object.context).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 737why, one step back737 } else if TypeId::of::<E>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 738–739why, one step back738 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();739 Some(Mut::new(&mut unerased._object.error).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 740–746why, one step back740 } else {741 None742 }743}744745// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.746#[cfg(feature = "std")]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 747why, one step back747unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 748–751why, one step back748where749 C: 'static,750 E: 'static,751{Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 752–754why, one step back752 // Called after downcasting by value to either the C or the E and doing a753 // ptr::read to take ownership of that value.754 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 755–757why, one step back755 let unerased = e756 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, E>>>()757 .boxed();Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 758–759why, one step back758 drop(unerased);759 } else {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 760–762why, one step back760 let unerased = e761 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<E>>>>()762 .boxed();Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 763–767why, one step back763 drop(unerased);764 }765}766767// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 768why, one step back768unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 769–771why, one step back769where770 C: 'static,771{Resolve branches_sharing_code clippy lintDavid Tolnaybefore adoptionaa6c83d
line 772why, one step back772 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 773why, one step back773 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 774why, one step back774 Some(Ref::new(&unerased._object.context).cast::<()>())Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 775–776why, one step back775 } else {776 // Recurse down the context chain per the inner error's vtable.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 777–778why, one step back777 let source = &unerased._object.error;778 (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target)Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 779–782why, one step back779 }780}781782// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 783why, one step back783#[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 784why, one step back784unsafe fn context_chain_downcast_mut<C>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 785–787why, one step back785where786 C: 'static,787{Resolve branches_sharing_code clippy lintDavid Tolnaybefore adoptionaa6c83d
line 788why, one step back788 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 789why, one step back789 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 790why, one step back790 Some(Mut::new(&mut unerased._object.context).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 791–792why, one step back791 } else {792 // Recurse down the context chain per the inner error's vtable.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 793–794why, one step back793 let source = &mut unerased._object.error;794 (vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 795–798why, one step back795 }796}797798// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 799why, one step back799unsafe fn context_chain_drop_rest<C>(e: Own<ErrorImpl>, target: TypeId)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 800–802why, one step back800where801 C: 'static,802{Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 803–805why, one step back803 // Called after downcasting by value to either the C or one of the causes804 // and doing a ptr::read to take ownership of that value.805 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 806–808why, one step back806 let unerased = e807 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, Error>>>()808 .boxed();Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 809–811why, one step back809 // Drop the entire rest of the data structure rooted in the next Error.810 drop(unerased);811 } else {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 812–815why, one step back812 let unerased = e813 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<Error>>>>()814 .boxed();815 // Read the Own<ErrorImpl> from the next error.Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 816why, one step back816 let inner = unerased._object.error.inner;Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 817why, one step back817 drop(unerased);Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 818why, one step back818 let vtable = vtable(inner.ptr);Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 819why, one step back819 // Recursively drop the next error using the same target typeid.Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 820why, one step back820 (vtable.object_drop_rest)(inner, target);Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 821–823why, one step back821 }822}823Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 824–835why, one step back824// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.825#[cfg(all(not(backtrace), feature = "backtrace"))]826#[allow(clippy::unnecessary_wraps)]827unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>828where829 C: 'static,830{831 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();832 let backtrace = ErrorImpl::backtrace(unerased._object.error.inner.by_ref());833 Some(backtrace)834}835Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 836–837why, one step back836// NOTE: If working with `ErrorImpl<()>`, references should be avoided in favor837// of raw pointers and `NonNull`.Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 838–839why, one step back838// repr C to ensure that E remains in the final position.839#[repr(C)]Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b
line 840why, one step back840pub(crate) struct ErrorImpl<E = ()> {Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 841–847why, one step back841 vtable: &'static ErrorVTable,842 backtrace: Option<Backtrace>,843 // NOTE: Don't use directly. Use only through vtable. Erased type may have844 // different alignment.845 _object: E,846}847Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 848–849why, one step back848// Reads the vtable out of `p`. This is the same as `p.as_ref().vtable`, but849// avoids converting `p` into a reference.Only need vtable function for a type erased ErrorImplDavid Tolnaybefore adoptiond1b0c9a
line 850why, one step back850unsafe fn vtable(p: NonNull<ErrorImpl>) -> &'static ErrorVTable {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 851why, one step back851 // NOTE: This assumes that `ErrorVTable` is the first field of ErrorImpl.Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd
line 852why, one step back852 *(p.as_ptr() as *const &'static ErrorVTable)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 853–854why, one step back853}854Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 855–863why, one step back855// repr C to ensure that ContextError<C, E> has the same layout as856// ContextError<ManuallyDrop<C>, E> and ContextError<C, ManuallyDrop<E>>.857#[repr(C)]858pub(crate) struct ContextError<C, E> {859 pub context: C,860 pub error: E,861}862863impl<E> ErrorImpl<E> {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 864why, one step back864 fn erase(&self) -> Ref<ErrorImpl> {Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 865–866why, one step back865 // Erase the concrete type of E but preserve the vtable in self.vtable866 // for manipulating the resulting thin pointer. This is analogous to anFix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b
line 867why, one step back867 // unsize coercion.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 868why, one step back868 Ref::new(self).cast::<ErrorImpl>()Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 869–871why, one step back869 }870}871Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b
line 872why, one step back872impl ErrorImpl {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 873why, one step back873 pub(crate) unsafe fn error(this: Ref<Self>) -> &(dyn StdError + Send + Sync + 'static) {Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 874–875why, one step back874 // Use vtable to attach E's native StdError vtable for the right875 // original type E.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 876why, one step back876 (vtable(this.ptr).object_ref)(this).deref()Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 877–879why, one step back877 }878879 #[cfg(feature = "std")]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 880why, one step back880 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 881–882why, one step back881 // Use vtable to attach E's native StdError vtable for the right882 // original type E.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 883–890why, one step back883884 #[cfg(not(anyhow_no_ptr_addr_of))]885 return (vtable(this.ptr).object_ref)(this.by_ref())886 .by_mut()887 .deref_mut();888889 #[cfg(anyhow_no_ptr_addr_of)]890 return (vtable(this.ptr).object_mut)(this);Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 891–892why, one step back891 }892Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
line 893why, one step back893 #[cfg(any(backtrace, feature = "backtrace"))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 894why, one step back894 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 895–897why, one step back895 // This unwrap can only panic if the underlying error's backtrace method896 // is nondeterministic, which would only happen in maliciously897 // constructed code.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 898why, one step back898 this.deref()Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 899why, one step back899 .backtraceTry out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
line 900why, one step back900 .as_ref()Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 901–902why, one step back901 .or_else(|| {902 #[cfg(backtrace)]Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c
lines 903–904why, one step back903 return Self::error(this).request_ref::<Backtrace>();904 #[cfg(not(backtrace))]Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 905–906why, one step back905 return (vtable(this.ptr).object_backtrace)(this);906 })Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 907–909why, one step back907 .expect("backtrace capture failed")908 }909Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c
line 910why, one step back910 #[cfg(backtrace)]Merge pull request #267 from dtolnay/demandDavid Tolnaybefore adoption007c69c
line 911why, one step back911 unsafe fn provide<'a>(this: Ref<'a, Self>, demand: &mut Demand<'a>) {Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c
line 912why, one step back912 if let Some(backtrace) = &this.deref().backtrace {Merge pull request #267 from dtolnay/demandDavid Tolnaybefore adoption007c69c
line 913why, one step back913 demand.provide_ref(backtrace);Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c
line 914why, one step back914 }Merge pull request #267 from dtolnay/demandDavid Tolnaybefore adoption007c69c
line 915why, one step back915 Self::error(this).provide(demand);Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c
lines 916–917why, one step back916 }917Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 918why, one step back918 #[cold]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 919why, one step back919 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 920why, one step back920 Chain::new(Self::error(this))Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 921–928why, one step back921 }922}923924impl<E> StdError for ErrorImpl<E>925where926 E: StdError,927{928 fn source(&self) -> Option<&(dyn StdError + 'static)> {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 929why, one step back929 unsafe { ErrorImpl::error(self.erase()).source() }Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c
lines 930–932why, one step back930 }931932 #[cfg(backtrace)]Merge pull request #267 from dtolnay/demandDavid Tolnaybefore adoption007c69c
lines 933–934why, one step back933 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {934 unsafe { ErrorImpl::provide(self.erase(), demand) }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 935–942why, one step back935 }936}937938impl<E> Debug for ErrorImpl<E>939where940 E: Debug,941{942 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 943why, one step back943 unsafe { ErrorImpl::debug(self.erase(), formatter) }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 944–951why, one step back944 }945}946947impl<E> Display for ErrorImpl<E>948where949 E: Display,950{951 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 952why, one step back952 unsafe { Display::fmt(ErrorImpl::error(self.erase()), formatter) }Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 953–956why, one step back953 }954}955956impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 957why, one step back957 #[cold]Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 958–962why, one step back958 fn from(error: Error) -> Self {959 let outer = ManuallyDrop::new(error);960 unsafe {961 // Use vtable to attach ErrorImpl<E>'s native StdError vtable for962 // the right original type E.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 963why, one step back963 (vtable(outer.inner.ptr).object_boxed)(outer.inner)Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 964–967why, one step back964 }965 }966}967Merge pull request #104 from dtolnay/fromDavid Tolnaybefore adoption03a1916
lines 968–973why, one step back968impl From<Error> for Box<dyn StdError + Send + 'static> {969 fn from(error: Error) -> Self {970 Box::<dyn StdError + Send + Sync>::from(error)971 }972}973Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8
lines 974–992why, one step back974impl From<Error> for Box<dyn StdError + 'static> {975 fn from(error: Error) -> Self {976 Box::<dyn StdError + Send + Sync>::from(error)977 }978}979980#[cfg(feature = "std")]981impl AsRef<dyn StdError + Send + Sync> for Error {982 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {983 &**self984 }985}986987#[cfg(feature = "std")]988impl AsRef<dyn StdError> for Error {989 fn as_ref(&self) -> &(dyn StdError + 'static) {990 &**self991 }992}