Skip to content
Myr

error.rs

This file

Path

src/error.rs

991 lines · 33.8 KiB · Rust

Last changed by

Merge pull request #268 from dtolnay/provideDavid Tolnaybefore adoption480e480

History of this file

Take 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 back
1use crate::backtrace::Backtrace;
2use crate::chain::Chain;

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 3–5why, one step back
3#[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 back
6use crate::{Error, StdError};

Merge pull request #178 from dtolnay/upDavid Tolnaybefore adoption3c586dd

line 7why, one step back
7use alloc::boxed::Box;

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

lines 8–9why, one step back
8#[cfg(backtrace)]
9use core::any::Demand;

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 10–11why, one step back
10use core::any::TypeId;
11use core::fmt::{self, Debug, Display};

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 12why, one step back
12use core::mem::ManuallyDrop;

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 13–14why, one step back
13#[cfg(not(anyhow_no_ptr_addr_of))]
14use core::ptr;

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 15why, one step back
15use core::ptr::NonNull;

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 16–29why, one step back
16
17#[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 back
30 #[cold]

Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42

line 31why, one step back
31 #[must_use]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 32–35why, one step back
32 pub fn new<E>(error: E) -> Self
33 where34 E: StdError + Send + Sync + 'static,35 {

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

line 36why, one step back
36 let backtrace = backtrace_if_absent!(&error);

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 37–76why, one step back
37 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 back
77 #[cold]

Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42

line 78why, one step back
78 #[must_use]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 79–86why, one step back
79 pub fn msg<M>(message: M) -> Self
80 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 back
87 #[cold]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 88–94why, one step back
88 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self
89 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 back
95 #[cfg(anyhow_no_ptr_addr_of)]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 96–98why, one step back
96 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 back
99 #[cfg(anyhow_no_ptr_addr_of)]

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 100why, one step back
100 object_downcast_mut: object_downcast_mut::<E>,

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 101why, one step back
101 object_drop_rest: object_drop_front::<E>,

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 102–103why, one step back
102 #[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 back
104 };
105106 // Safety: passing vtable that operates on the right type E.107 unsafe { Error::construct(error, vtable, backtrace) }108 }109

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 111–119why, one step back
111 pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self
112 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 back
120 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 121–123why, one step back
121 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 back
124 #[cfg(anyhow_no_ptr_addr_of)]

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 125why, one step back
125 object_downcast_mut: object_downcast_mut::<M>,

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 126why, one step back
126 object_drop_rest: object_drop_front::<M>,

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 127–128why, one step back
127 #[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 back
129 };
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 }135

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 137–145why, one step back
137 pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self
138 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 back
146 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 147–149why, one step back
147 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 back
150 #[cfg(anyhow_no_ptr_addr_of)]

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 151why, one step back
151 object_downcast_mut: object_downcast_mut::<M>,

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 152why, one step back
152 object_drop_rest: object_drop_front::<M>,

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 153–154why, one step back
153 #[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 back
155 };
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 back
163 #[cold]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 164–173why, one step back
164 pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self
165 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 back
174 #[cfg(anyhow_no_ptr_addr_of)]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 175–177why, one step back
175 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 back
178 #[cfg(anyhow_no_ptr_addr_of)]

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 179why, one step back
179 object_downcast_mut: context_downcast_mut::<C, E>,

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 180why, one step back
180 object_drop_rest: context_drop_rest::<C, E>,

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 181–182why, one step back
181 #[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 back
183 };
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 back
190 #[cold]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 191–199why, one step back
191 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 back
200 #[cfg(anyhow_no_ptr_addr_of)]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 201–203why, one step back
201 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 back
204 #[cfg(anyhow_no_ptr_addr_of)]

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 205why, one step back
205 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 back
206 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 back
207 #[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 back
209 };
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 back
221 #[cold]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 222–229why, one step back
222 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 back
230 let inner: Box<ErrorImpl<E>> = Box::new(ErrorImpl {

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 231–235why, one step back
231 vtable,
232 backtrace,233 _object: error,234 });235 // Erase the concrete type of E from the compile-time type system. This

Fix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b

line 236why, one step back
236 // is equivalent to the safe unsize coercion from Box<ErrorImpl<E>> to

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 237–240why, one step back
237 // Box<ErrorImpl<dyn StdError + Send + Sync + 'static>> except that the
238 // 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 back
241 let inner = Own::new(inner).cast::<ErrorImpl>();

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 242–298why, one step back
242 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 back
299 #[cold]

Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42

line 300why, one step back
300 #[must_use]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 301–312why, one step back
301 pub fn context<C>(self, context: C) -> Self
302 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 back
313 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 314–316why, one step back
314 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 back
317 #[cfg(anyhow_no_ptr_addr_of)]

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 318why, one step back
318 object_downcast_mut: context_chain_downcast_mut::<C>,

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 319why, one step back
319 object_drop_rest: context_chain_drop_rest::<C>,

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 320–321why, one step back
320 #[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 back
322 };
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 back
346 /// # Stability
347 ///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 back
360 /// [tracking]: https://github.com/rust-lang/rust/issues/53487

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 361–363why, one step back
361 #[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 back
364 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 365–388why, one step back
365 }
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 back
389 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 391why, one step back
391 pub fn chain(&self) -> Chain {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 392why, one step back
392 unsafe { ErrorImpl::chain(self.inner.by_ref()) }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 393–400why, one step back
393 }
394395 /// The lowest level cause of this error &mdash; 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 back
401 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 402why, one step back
402 pub fn root_cause(&self) -> &(dyn StdError + 'static) {

Merge pull request #108 from hbina/patch-1David Tolnaybefore adoption7954754

line 403why, one step back
403 self.chain().last().unwrap()

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 404–421why, one step back
404 }
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 back
422 pub fn downcast<E>(mut self) -> Result<E, Self>

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 423–426why, one step back
423 where
424 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 back
427 let inner = self.inner.by_mut();

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 428–430why, one step back
428 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 back
431 #[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 back
441
442 // 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 back
447 let error = addr.cast::<E>().read();

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 448–449why, one step back
448
449 // Drop rest of the data structure outside of E.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 450why, one step back
450 (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 back
451
452 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 back
500 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 back
502 }
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 back
514
515 #[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 back
520 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 back
521

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 522why, one step back
522 Some(addr.cast::<E>().deref_mut())

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 523–524why, one step back
523 }
524 }

Merge pull request #268 from dtolnay/provideDavid Tolnaybefore adoption480e480

lines 525–535why, one step back
525
526 // Called by thiserror when you have `#[source] anyhow::Error`. This provide527 // implementation includes the anyhow::Error's Backtrace if any, unlike528 // deref'ing to dyn Error where the provide implementation would include529 // only the original error's Backtrace from before it got wrapped into an530 // anyhow::Error.531 #[cfg(backtrace)]532 #[doc(hidden)]533 pub fn provide<'a>(&'a self, demand: &mut Demand<'a>) {534 unsafe { ErrorImpl::provide(self.inner.by_ref(), demand) }535 }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 536–538why, one step back
536}
537538#[cfg(feature = "std")]

Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f

line 539why, one step back
539#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 540–543why, one step back
540impl<E> From<E> for Error
541where542 E: StdError + Send + Sync + 'static,543{

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

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

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

line 546why, one step back
546 let backtrace = backtrace_if_absent!(&error);

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 547–551why, one step back
547 Error::from_std(error, backtrace)
548 }549}550551#[cfg(feature = "std")]

Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f

line 552why, one step back
552#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 553–556why, one step back
553impl Deref for Error {
554 type Target = dyn StdError + Send + Sync + 'static;555556 fn deref(&self) -> &Self::Target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 557why, one step back
557 unsafe { ErrorImpl::error(self.inner.by_ref()) }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 558–561why, one step back
558 }
559}560561#[cfg(feature = "std")]

Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f

line 562why, one step back
562#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 563–564why, one step back
563impl DerefMut for Error {
564 fn deref_mut(&mut self) -> &mut Self::Target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 565why, one step back
565 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 566–570why, one step back
566 }
567}568569impl Display for Error {570 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 571why, one step back
571 unsafe { ErrorImpl::display(self.inner.by_ref(), formatter) }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 572–576why, one step back
572 }
573}574575impl Debug for Error {576 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 577why, one step back
577 unsafe { ErrorImpl::debug(self.inner.by_ref(), formatter) }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 578–584why, one step back
578 }
579}580581impl Drop for Error {582 fn drop(&mut self) {583 unsafe {584 // Invoke the vtable's drop behavior.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 585why, one step back
585 (vtable(self.inner.ptr).object_drop)(self.inner);

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 586–590why, one step back
586 }
587 }588}589590struct ErrorVTable {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 591why, one step back
591 object_drop: unsafe fn(Own<ErrorImpl>),

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 592–593why, one step back
592 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,
593 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 594–596why, one step back
594 object_mut: unsafe fn(Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static),
595 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,596 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 597why, one step back
597 #[cfg(anyhow_no_ptr_addr_of)]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 598–599why, one step back
598 object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,
599 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 600–601why, one step back
600 #[cfg(all(not(backtrace), feature = "backtrace"))]
601 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 602–604why, one step back
602}
603604// Safety: requires layout of *e to match ErrorImpl<E>.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 605why, one step back
605unsafe fn object_drop<E>(e: Own<ErrorImpl>) {

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 606–607why, one step back
606 // Cast back to ErrorImpl<E> so that the allocator receives the correct
607 // Layout to deallocate the Box's memory.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 608why, one step back
608 let unerased = e.cast::<ErrorImpl<E>>().boxed();

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 609–612why, one step back
609 drop(unerased);
610}611612// Safety: requires layout of *e to match ErrorImpl<E>.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 613why, one step back
613unsafe fn object_drop_front<E>(e: Own<ErrorImpl>, target: TypeId) {

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 614–617why, one step back
614 // Drop the fields of ErrorImpl other than E as well as the Box allocation,
615 // without dropping E itself. This is used by downcast after doing a616 // ptr::read to take ownership of the E.617 let _ = target;

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 618why, one step back
618 let unerased = e.cast::<ErrorImpl<ManuallyDrop<E>>>().boxed();

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 619–622why, one step back
619 drop(unerased);
620}621622// Safety: requires layout of *e to match ErrorImpl<E>.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 623why, one step back
623unsafe fn object_ref<E>(e: Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 624–627why, one step back
624where
625 E: StdError + Send + Sync + 'static,626{627 // Attach E's native StdError vtable onto a pointer to self._object.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 628–637why, one step back
628
629 let unerased = e.cast::<ErrorImpl<E>>();630631 #[cfg(not(anyhow_no_ptr_addr_of))]632 return Ref::from_raw(NonNull::new_unchecked(633 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,634 ));635636 #[cfg(anyhow_no_ptr_addr_of)]637 return Ref::new(&unerased.deref()._object);

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 638–639why, one step back
638}
639

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 640–641why, one step back
640// Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived
641// from a `&mut`

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 642why, one step back
642#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 644–647why, one step back
644where
645 E: StdError + Send + Sync + 'static,646{647 // Attach E's native StdError vtable onto a pointer to self._object.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 648why, one step back
648 &mut e.cast::<ErrorImpl<E>>().deref_mut()._object

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 649–651why, one step back
649}
650651// Safety: requires layout of *e to match ErrorImpl<E>.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 653–656why, one step back
653where
654 E: StdError + Send + Sync + 'static,655{656 // Attach ErrorImpl<E>'s native StdError vtable. The StdError impl is below.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 657why, one step back
657 e.cast::<ErrorImpl<E>>().boxed()

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 658–660why, one step back
658}
659660// Safety: requires layout of *e to match ErrorImpl<E>.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 661why, one step back
661unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 662–667why, one step back
662where
663 E: 'static,664{665 if TypeId::of::<E>() == target {666 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a667 // pointer to its E field.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 668–680why, one step back
668
669 let unerased = e.cast::<ErrorImpl<E>>();670671 #[cfg(not(anyhow_no_ptr_addr_of))]672 return Some(673 Ref::from_raw(NonNull::new_unchecked(674 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,675 ))676 .cast::<()>(),677 );678679 #[cfg(anyhow_no_ptr_addr_of)]680 return Some(Ref::new(&unerased.deref()._object).cast::<()>());

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 681–685why, one step back
681 } else {
682 None683 }684}685

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 686why, one step back
686// Safety: requires layout of *e to match ErrorImpl<E>.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 687why, one step back
687#[cfg(anyhow_no_ptr_addr_of)]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 688why, one step back
688unsafe fn object_downcast_mut<E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 689–694why, one step back
689where
690 E: 'static,691{692 if TypeId::of::<E>() == target {693 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a694 // pointer to its E field.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 695–696why, one step back
695 let unerased = e.cast::<ErrorImpl<E>>().deref_mut();
696 Some(Mut::new(&mut unerased._object).cast::<()>())

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 697–701why, one step back
697 } else {
698 None699 }700}701

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 702–707why, one step back
702#[cfg(all(not(backtrace), feature = "backtrace"))]
703fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {704 let _ = e;705 None706}707

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 708–709why, one step back
708// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.
709#[cfg(feature = "std")]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 710why, one step back
710unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 711–715why, one step back
711where
712 C: 'static,713 E: 'static,714{715 if TypeId::of::<C>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 716–717why, one step back
716 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();
717 Some(Ref::new(&unerased._object.context).cast::<()>())

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 718why, one step back
718 } else if TypeId::of::<E>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 719–720why, one step back
719 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();
720 Some(Ref::new(&unerased._object.error).cast::<()>())

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 721–726why, one step back
721 } else {
722 None723 }724}725726// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 727why, one step back
727#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 728why, one step back
728unsafe fn context_downcast_mut<C, E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 729–732why, one step back
729where
730 C: 'static,731 E: 'static,732{

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 733why, one step back
733 if TypeId::of::<C>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 734–735why, one step back
734 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();
735 Some(Mut::new(&mut unerased._object.context).cast::<()>())

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 736why, one step back
736 } else if TypeId::of::<E>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 737–738why, one step back
737 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();
738 Some(Mut::new(&mut unerased._object.error).cast::<()>())

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 739–745why, one step back
739 } else {
740 None741 }742}743744// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.745#[cfg(feature = "std")]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 746why, one step back
746unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 747–750why, one step back
747where
748 C: 'static,749 E: 'static,750{

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 751–753why, one step back
751 // Called after downcasting by value to either the C or the E and doing a
752 // ptr::read to take ownership of that value.753 if TypeId::of::<C>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 754–756why, one step back
754 let unerased = e
755 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, E>>>()756 .boxed();

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 757–758why, one step back
757 drop(unerased);
758 } else {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 759–761why, one step back
759 let unerased = e
760 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<E>>>>()761 .boxed();

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 762–766why, one step back
762 drop(unerased);
763 }764}765766// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 767why, one step back
767unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 768–770why, one step back
768where
769 C: 'static,770{

Resolve branches_sharing_code clippy lintDavid Tolnaybefore adoptionaa6c83d

line 771why, one step back
771 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 772why, one step back
772 if TypeId::of::<C>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 773why, one step back
773 Some(Ref::new(&unerased._object.context).cast::<()>())

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 774–775why, one step back
774 } else {
775 // Recurse down the context chain per the inner error's vtable.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 776–777why, one step back
776 let source = &unerased._object.error;
777 (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target)

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 778–781why, one step back
778 }
779}780781// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 782why, one step back
782#[cfg(anyhow_no_ptr_addr_of)]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 783why, one step back
783unsafe fn context_chain_downcast_mut<C>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 784–786why, one step back
784where
785 C: 'static,786{

Resolve branches_sharing_code clippy lintDavid Tolnaybefore adoptionaa6c83d

line 787why, one step back
787 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 788why, one step back
788 if TypeId::of::<C>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 789why, one step back
789 Some(Mut::new(&mut unerased._object.context).cast::<()>())

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 790–791why, one step back
790 } else {
791 // Recurse down the context chain per the inner error's vtable.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 792–793why, one step back
792 let source = &mut unerased._object.error;
793 (vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target)

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 794–797why, one step back
794 }
795}796797// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 798why, one step back
798unsafe fn context_chain_drop_rest<C>(e: Own<ErrorImpl>, target: TypeId)

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 799–801why, one step back
799where
800 C: 'static,801{

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 802–804why, one step back
802 // Called after downcasting by value to either the C or one of the causes
803 // and doing a ptr::read to take ownership of that value.804 if TypeId::of::<C>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 805–807why, one step back
805 let unerased = e
806 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, Error>>>()807 .boxed();

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 808–810why, one step back
808 // Drop the entire rest of the data structure rooted in the next Error.
809 drop(unerased);810 } else {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 811–814why, one step back
811 let unerased = e
812 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<Error>>>>()813 .boxed();814 // Read the Own<ErrorImpl> from the next error.

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 815why, one step back
815 let inner = unerased._object.error.inner;

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 816why, one step back
816 drop(unerased);

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 817why, one step back
817 let vtable = vtable(inner.ptr);

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 819why, one step back
819 (vtable.object_drop_rest)(inner, target);

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 820–822why, one step back
820 }
821}822

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 823–834why, one step back
823// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.
824#[cfg(all(not(backtrace), feature = "backtrace"))]825#[allow(clippy::unnecessary_wraps)]826unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>827where828 C: 'static,829{830 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();831 let backtrace = ErrorImpl::backtrace(unerased._object.error.inner.by_ref());832 Some(backtrace)833}834

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 835–836why, one step back
835// NOTE: If working with `ErrorImpl<()>`, references should be avoided in favor
836// of raw pointers and `NonNull`.

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 837–838why, one step back
837// repr C to ensure that E remains in the final position.
838#[repr(C)]

Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b

line 839why, one step back
839pub(crate) struct ErrorImpl<E = ()> {

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 840–846why, one step back
840 vtable: &'static ErrorVTable,
841 backtrace: Option<Backtrace>,842 // NOTE: Don't use directly. Use only through vtable. Erased type may have843 // different alignment.844 _object: E,845}846

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 847–848why, one step back
847// Reads the vtable out of `p`. This is the same as `p.as_ref().vtable`, but
848// avoids converting `p` into a reference.

Only need vtable function for a type erased ErrorImplDavid Tolnaybefore adoptiond1b0c9a

line 849why, one step back
849unsafe fn vtable(p: NonNull<ErrorImpl>) -> &'static ErrorVTable {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 850why, one step back
850 // NOTE: This assumes that `ErrorVTable` is the first field of ErrorImpl.

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

line 851why, one step back
851 *(p.as_ptr() as *const &'static ErrorVTable)

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 852–853why, one step back
852}
853

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 854–862why, one step back
854// repr C to ensure that ContextError<C, E> has the same layout as
855// ContextError<ManuallyDrop<C>, E> and ContextError<C, ManuallyDrop<E>>.856#[repr(C)]857pub(crate) struct ContextError<C, E> {858 pub context: C,859 pub error: E,860}861862impl<E> ErrorImpl<E> {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 863why, one step back
863 fn erase(&self) -> Ref<ErrorImpl> {

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 864–865why, one step back
864 // Erase the concrete type of E but preserve the vtable in self.vtable
865 // for manipulating the resulting thin pointer. This is analogous to an

Fix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b

line 866why, one step back
866 // unsize coercion.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 867why, one step back
867 Ref::new(self).cast::<ErrorImpl>()

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 868–870why, one step back
868 }
869}870

Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b

line 871why, one step back
871impl ErrorImpl {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 872why, one step back
872 pub(crate) unsafe fn error(this: Ref<Self>) -> &(dyn StdError + Send + Sync + 'static) {

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 873–874why, one step back
873 // Use vtable to attach E's native StdError vtable for the right
874 // original type E.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 875why, one step back
875 (vtable(this.ptr).object_ref)(this).deref()

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 876–878why, one step back
876 }
877878 #[cfg(feature = "std")]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 879why, one step back
879 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 880–881why, one step back
880 // Use vtable to attach E's native StdError vtable for the right
881 // original type E.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 882–889why, one step back
882
883 #[cfg(not(anyhow_no_ptr_addr_of))]884 return (vtable(this.ptr).object_ref)(this.by_ref())885 .by_mut()886 .deref_mut();887888 #[cfg(anyhow_no_ptr_addr_of)]889 return (vtable(this.ptr).object_mut)(this);

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 890–891why, one step back
890 }
891

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 893why, one step back
893 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 894–896why, one step back
894 // This unwrap can only panic if the underlying error's backtrace method
895 // is nondeterministic, which would only happen in maliciously896 // constructed code.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 897why, one step back
897 this.deref()

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 898why, one step back
898 .backtrace

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

line 899why, one step back
899 .as_ref()

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 900–901why, one step back
900 .or_else(|| {
901 #[cfg(backtrace)]

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

lines 902–903why, one step back
902 return Self::error(this).request_ref::<Backtrace>();
903 #[cfg(not(backtrace))]

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 904–905why, one step back
904 return (vtable(this.ptr).object_backtrace)(this);
905 })

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 906–908why, one step back
906 .expect("backtrace capture failed")
907 }908

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

line 909why, one step back
909 #[cfg(backtrace)]

Merge pull request #267 from dtolnay/demandDavid Tolnaybefore adoption007c69c

line 910why, one step back
910 unsafe fn provide<'a>(this: Ref<'a, Self>, demand: &mut Demand<'a>) {

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

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

Merge pull request #267 from dtolnay/demandDavid Tolnaybefore adoption007c69c

line 912why, one step back
912 demand.provide_ref(backtrace);

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

line 913why, one step back
913 }

Merge pull request #267 from dtolnay/demandDavid Tolnaybefore adoption007c69c

line 914why, one step back
914 Self::error(this).provide(demand);

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

lines 915–916why, one step back
915 }
916

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 918why, one step back
918 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 919why, one step back
919 Chain::new(Self::error(this))

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 920–927why, one step back
920 }
921}922923impl<E> StdError for ErrorImpl<E>924where925 E: StdError,926{927 fn source(&self) -> Option<&(dyn StdError + 'static)> {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 928why, one step back
928 unsafe { ErrorImpl::error(self.erase()).source() }

Merge pull request #266 from dtolnay/providerDavid Tolnaybefore adoption546342c

lines 929–931why, one step back
929 }
930931 #[cfg(backtrace)]

Merge pull request #267 from dtolnay/demandDavid Tolnaybefore adoption007c69c

lines 932–933why, one step back
932 fn provide<'a>(&'a self, demand: &mut Demand<'a>) {
933 unsafe { ErrorImpl::provide(self.erase(), demand) }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 934–941why, one step back
934 }
935}936937impl<E> Debug for ErrorImpl<E>938where939 E: Debug,940{941 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 942why, one step back
942 unsafe { ErrorImpl::debug(self.erase(), formatter) }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 943–950why, one step back
943 }
944}945946impl<E> Display for ErrorImpl<E>947where948 E: Display,949{950 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 951why, one step back
951 unsafe { Display::fmt(ErrorImpl::error(self.erase()), formatter) }

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 952–955why, one step back
952 }
953}954955impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 957–961why, one step back
957 fn from(error: Error) -> Self {
958 let outer = ManuallyDrop::new(error);959 unsafe {960 // Use vtable to attach ErrorImpl<E>'s native StdError vtable for961 // the right original type E.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 962why, one step back
962 (vtable(outer.inner.ptr).object_boxed)(outer.inner)

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 963–966why, one step back
963 }
964 }965}966

Merge pull request #104 from dtolnay/fromDavid Tolnaybefore adoption03a1916

lines 967–972why, one step back
967impl From<Error> for Box<dyn StdError + Send + 'static> {
968 fn from(error: Error) -> Self {969 Box::<dyn StdError + Send + Sync>::from(error)970 }971}972

Try out a new style of readme badgesDavid Tolnaybefore adoption5e1a1f8

lines 973–991why, one step back
973impl From<Error> for Box<dyn StdError + 'static> {
974 fn from(error: Error) -> Self {975 Box::<dyn StdError + Send + Sync>::from(error)976 }977}978979#[cfg(feature = "std")]980impl AsRef<dyn StdError + Send + Sync> for Error {981 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {982 &**self983 }984}985986#[cfg(feature = "std")]987impl AsRef<dyn StdError> for Error {988 fn as_ref(&self) -> &(dyn StdError + 'static) {989 &**self990 }991}