Skip to content
Myr

error.rs

This file

Path

src/error.rs

970 lines · 33.0 KiB · Rust

Last changed by

Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42

History of this file

Take it

The walk stopped after 200 commits: lines older than 87e85ab point at it.

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

line 1why, one step back
1use crate::backtrace::Backtrace;

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

line 2why, one step back
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};

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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 #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

line 14why, one step back
14

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

lines 15–17why, one step back
15#[cfg(feature = "std")]
16use core::ops::{Deref, DerefMut};17

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Merge pull request #78 from dtolnay/cfgDavid Tolnaybefore adoptione2eb1f5

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

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 30–74why, one step back
30 pub fn new<E>(error: E) -> Self
31 where32 E: StdError + Send + Sync + 'static,33 {34 let backtrace = backtrace_if_absent!(error);35 Error::from_std(error, backtrace)36 }3738 /// Create a new error object from a printable error message.39 ///40 /// If the argument implements std::error::Error, prefer `Error::new`41 /// instead which preserves the underlying error's cause chain and42 /// backtrace. If the argument may or may not implement std::error::Error43 /// now or in the future, use `anyhow!(err)` which handles either way44 /// correctly.45 ///46 /// `Error::msg("...")` is equivalent to `anyhow!("...")` but occasionally47 /// convenient in places where a function is preferable over a macro, such48 /// as iterator or stream combinators:49 ///50 /// ```51 /// # mod ffi {52 /// # pub struct Input;53 /// # pub struct Output;54 /// # pub async fn do_some_work(_: Input) -> Result<Output, &'static str> {55 /// # unimplemented!()56 /// # }57 /// # }58 /// #59 /// # use ffi::{Input, Output};60 /// #61 /// use anyhow::{Error, Result};62 /// use futures::stream::{Stream, StreamExt, TryStreamExt};63 ///64 /// async fn demo<S>(stream: S) -> Result<Vec<Output>>65 /// where66 /// S: Stream<Item = Input>,67 /// {68 /// stream69 /// .then(ffi::do_some_work) // returns Result<Output, &str>70 /// .map_err(Error::msg)71 /// .try_collect()72 /// .await73 /// }74 /// ```

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 77–83why, one step back
77 pub fn msg<M>(message: M) -> Self
78 where79 M: Display + Debug + Send + Sync + 'static,80 {81 Error::from_adhoc(message, backtrace!())82 }83

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 86–92why, one step back
86 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self
87 where88 E: StdError + Send + Sync + 'static,89 {90 let vtable = &ErrorVTable {91 object_drop: object_drop::<E>,92 object_ref: object_ref::<E>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 94–96why, one step back
94 object_mut: object_mut::<E>,
95 object_boxed: object_boxed::<E>,96 object_downcast: object_downcast::<E>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 100–101why, one step back
100 #[cfg(all(not(backtrace), feature = "backtrace"))]
101 object_backtrace: no_backtrace,

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 102–107why, one step back
102 };
103104 // Safety: passing vtable that operates on the right type E.105 unsafe { Error::construct(error, vtable, backtrace) }106 }107

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 109–112why, one step back
109 pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self
110 where111 M: Display + Debug + Send + Sync + 'static,112 {

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

line 113why, one step back
113 use crate::wrapper::MessageError;

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 114–117why, one step back
114 let error: MessageError<M> = MessageError(message);
115 let vtable = &ErrorVTable {116 object_drop: object_drop::<MessageError<M>>,117 object_ref: object_ref::<MessageError<M>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 119–121why, one step back
119 object_mut: object_mut::<MessageError<M>>,
120 object_boxed: object_boxed::<MessageError<M>>,121 object_downcast: object_downcast::<M>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 125–126why, one step back
125 #[cfg(all(not(backtrace), feature = "backtrace"))]
126 object_backtrace: no_backtrace,

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 127–133why, one step back
127 };
128129 // Safety: MessageError is repr(transparent) so it is okay for the130 // vtable to allow casting the MessageError<M> to M.131 unsafe { Error::construct(error, vtable, backtrace) }132 }133

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 135–138why, one step back
135 pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self
136 where137 M: Display + Send + Sync + 'static,138 {

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

line 139why, one step back
139 use crate::wrapper::DisplayError;

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 140–143why, one step back
140 let error: DisplayError<M> = DisplayError(message);
141 let vtable = &ErrorVTable {142 object_drop: object_drop::<DisplayError<M>>,143 object_ref: object_ref::<DisplayError<M>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 145–147why, one step back
145 object_mut: object_mut::<DisplayError<M>>,
146 object_boxed: object_boxed::<DisplayError<M>>,147 object_downcast: object_downcast::<M>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 151–152why, one step back
151 #[cfg(all(not(backtrace), feature = "backtrace"))]
152 object_backtrace: no_backtrace,

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 153–159why, one step back
153 };
154155 // Safety: DisplayError is repr(transparent) so it is okay for the156 // vtable to allow casting the DisplayError<M> to M.157 unsafe { Error::construct(error, vtable, backtrace) }158 }159

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 162–171why, one step back
162 pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self
163 where164 C: Display + Send + Sync + 'static,165 E: StdError + Send + Sync + 'static,166 {167 let error: ContextError<C, E> = ContextError { context, error };168169 let vtable = &ErrorVTable {170 object_drop: object_drop::<ContextError<C, E>>,171 object_ref: object_ref::<ContextError<C, E>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 173–175why, one step back
173 object_mut: object_mut::<ContextError<C, E>>,
174 object_boxed: object_boxed::<ContextError<C, E>>,175 object_downcast: context_downcast::<C, E>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 179–180why, one step back
179 #[cfg(all(not(backtrace), feature = "backtrace"))]
180 object_backtrace: no_backtrace,

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 181–183why, one step back
181 };
182183 // Safety: passing vtable that operates on the right type.

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

lines 184–186why, one step back
184 unsafe { Error::construct(error, vtable, backtrace) }
185 }186

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

lines 189–192why, one step back
189 pub(crate) fn from_boxed(
190 error: Box<dyn StdError + Send + Sync>,191 backtrace: Option<Backtrace>,192 ) -> Self {

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

line 193why, one step back
193 use crate::wrapper::BoxedError;

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

lines 194–197why, one step back
194 let error = BoxedError(error);
195 let vtable = &ErrorVTable {196 object_drop: object_drop::<BoxedError>,197 object_ref: object_ref::<BoxedError>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

lines 199–201why, one step back
199 object_mut: object_mut::<BoxedError>,
200 object_boxed: object_boxed::<BoxedError>,201 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 203why, one step back
203 object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

line 204why, one step back
204 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 205–206why, one step back
205 #[cfg(all(not(backtrace), feature = "backtrace"))]
206 object_backtrace: no_backtrace,

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

lines 207–210why, one step back
207 };
208209 // Safety: BoxedError is repr(transparent) so it is okay for the vtable210 // to allow casting to Box<dyn StdError + Send + Sync>.

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 211–218why, one step back
211 unsafe { Error::construct(error, vtable, backtrace) }
212 }213214 // Takes backtrace as argument rather than capturing it here so that the215 // user sees one fewer layer of wrapping noise in the backtrace.216 //217 // Unsafe because the given vtable must have sensible behavior on the error218 // value of type E.

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 220–227why, one step back
220 unsafe fn construct<E>(
221 error: E,222 vtable: &'static ErrorVTable,223 backtrace: Option<Backtrace>,224 ) -> Self225 where226 E: StdError + Send + Sync + 'static,227 {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 228why, one step back
228 let inner: Box<ErrorImpl<E>> = Box::new(ErrorImpl {

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 229–232why, one step back
229 vtable,
230 backtrace,231 _object: error,232 });

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 233why, one step back
233 // Erase the concrete type of E from the compile-time type system. This

Fix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

lines 235–238why, one step back
235 // Box<ErrorImpl<dyn StdError + Send + Sync + 'static>> except that the
236 // result is a thin pointer. The necessary behavior for manipulating the237 // underlying ErrorImpl<E> is preserved in the vtable provided by the238 // caller rather than a builtin fat pointer vtable.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 239why, one step back
239 let inner = Own::new(inner).cast::<ErrorImpl>();

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 240–296why, one step back
240 Error { inner }
241 }242243 /// Wrap the error value with additional context.244 ///245 /// For attaching context to a `Result` as it is propagated, the246 /// [`Context`][crate::Context] extension trait may be more convenient than247 /// this function.248 ///249 /// The primary reason to use `error.context(...)` instead of250 /// `result.context(...)` via the `Context` trait would be if the context251 /// needs to depend on some data held by the underlying error:252 ///253 /// ```254 /// # use std::fmt::{self, Debug, Display};255 /// #256 /// # type T = ();257 /// #258 /// # impl std::error::Error for ParseError {}259 /// # impl Debug for ParseError {260 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {261 /// # unimplemented!()262 /// # }263 /// # }264 /// # impl Display for ParseError {265 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {266 /// # unimplemented!()267 /// # }268 /// # }269 /// #270 /// use anyhow::Result;271 /// use std::fs::File;272 /// use std::path::Path;273 ///274 /// struct ParseError {275 /// line: usize,276 /// column: usize,277 /// }278 ///279 /// fn parse_impl(file: File) -> Result<T, ParseError> {280 /// # const IGNORE: &str = stringify! {281 /// ...282 /// # };283 /// # unimplemented!()284 /// }285 ///286 /// pub fn parse(path: impl AsRef<Path>) -> Result<T> {287 /// let file = File::open(&path)?;288 /// parse_impl(file).map_err(|error| {289 /// let context = format!(290 /// "only the first {} lines of {} are valid",291 /// error.line, path.as_ref().display(),292 /// );293 /// anyhow::Error::new(error).context(context)294 /// })295 /// }296 /// ```

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 299–310why, one step back
299 pub fn context<C>(self, context: C) -> Self
300 where301 C: Display + Send + Sync + 'static,302 {303 let error: ContextError<C, Error> = ContextError {304 context,305 error: self,306 };307308 let vtable = &ErrorVTable {309 object_drop: object_drop::<ContextError<C, Error>>,310 object_ref: object_ref::<ContextError<C, Error>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 312–314why, one step back
312 object_mut: object_mut::<ContextError<C, Error>>,
313 object_boxed: object_boxed::<ContextError<C, Error>>,314 object_downcast: context_chain_downcast::<C>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 318–319why, one step back
318 #[cfg(all(not(backtrace), feature = "backtrace"))]
319 object_backtrace: context_backtrace::<C>,

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 320–330why, one step back
320 };
321322 // As the cause is anyhow::Error, we already have a backtrace for it.323 let backtrace = None;324325 // Safety: passing vtable that operates on the right type.326 unsafe { Error::construct(error, vtable, backtrace) }327 }328329 /// Get the backtrace for this Error.330 ///

Merge pull request #70 from dtolnay/envDavid Tolnaybefore adoption8d2acea

lines 331–333why, one step back
331 /// In order for the backtrace to be meaningful, one of the two environment
332 /// variables `RUST_LIB_BACKTRACE=1` or `RUST_BACKTRACE=1` must be defined333 /// and `RUST_LIB_BACKTRACE` must not be `0`. Backtraces are somewhat

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 334–335why, one step back
334 /// expensive to capture in Rust, so we don't necessarily want to be
335 /// capturing them all over the place all the time.

Merge pull request #70 from dtolnay/envDavid Tolnaybefore adoption8d2acea

lines 336–342why, one step back
336 ///
337 /// - If you want panics and errors to both have backtraces, set338 /// `RUST_BACKTRACE=1`;339 /// - If you want only errors to have backtraces, set340 /// `RUST_LIB_BACKTRACE=1`;341 /// - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and342 /// `RUST_LIB_BACKTRACE=0`.

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

line 343why, one step back
343 ///

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 344–357why, one step back
344 /// # Stability
345 ///346 /// Standard library backtraces are only available on the nightly channel.347 /// Tracking issue: [rust-lang/rust#53487][tracking].348 ///349 /// On stable compilers, this function is only available if the crate's350 /// "backtrace" feature is enabled, and will use the `backtrace` crate as351 /// the underlying backtrace implementation.352 ///353 /// ```toml354 /// [dependencies]355 /// anyhow = { version = "1.0", features = ["backtrace"] }356 /// ```357 ///

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

line 358why, one step back
358 /// [tracking]: https://github.com/rust-lang/rust/issues/53487

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 359–361why, one step back
359 #[cfg(any(backtrace, feature = "backtrace"))]
360 #[cfg_attr(doc_cfg, doc(cfg(any(nightly, feature = "backtrace"))))]361 pub fn backtrace(&self) -> &impl_backtrace!() {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 363–385why, one step back
363 }
364365 /// An iterator of the chain of source errors contained by this Error.366 ///367 /// This iterator will visit every error in the cause chain of this error368 /// object, beginning with the error that this error object was created369 /// from.370 ///371 /// # Example372 ///373 /// ```374 /// use anyhow::Error;375 /// use std::io;376 ///377 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {378 /// for cause in error.chain() {379 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {380 /// return Some(io_error.kind());381 /// }382 /// }383 /// None384 /// }385 /// ```

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

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

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

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 391–397why, one step back
391 }
392393 /// The lowest level cause of this error &mdash; this error's cause's394 /// cause's cause etc.395 ///396 /// The root cause is the last error in the iterator produced by397 /// [`chain()`][Error::chain].

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

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

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

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

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 402–419why, one step back
402 }
403404 /// Returns true if `E` is the type held by this error object.405 ///406 /// For errors with context, this method returns true if `E` matches the407 /// type of the context `C` **or** the type of the error on which the408 /// context has been attached. For details about the interaction between409 /// context and downcasting, [see here].410 ///411 /// [see here]: trait.Context.html#effect-on-downcasting412 pub fn is<E>(&self) -> bool413 where414 E: Display + Debug + Send + Sync + 'static,415 {416 self.downcast_ref::<E>().is_some()417 }418419 /// Attempt to downcast the error object to a concrete type.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 420why, one step back
420 pub fn downcast<E>(mut self) -> Result<E, Self>

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 421–424why, one step back
421 where
422 E: Display + Debug + Send + Sync + 'static,423 {424 let target = TypeId::of::<E>();

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 425why, one step back
425 let inner = self.inner.by_mut();

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

line 426why, one step back
426 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

lines 427–428why, one step back
427 // Use vtable to find NonNull<()> which points to a value of type E
428 // somewhere inside the data structure.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 429–438why, one step back
429 #[cfg(not(anyhow_no_ptr_addr_of))]
430 let addr = match (vtable(inner.ptr).object_downcast)(inner.by_ref(), target) {431 Some(addr) => addr.by_mut().extend(),432 None => return Err(self),433 };434 #[cfg(anyhow_no_ptr_addr_of)]435 let addr = match (vtable(inner.ptr).object_downcast_mut)(inner, target) {436 Some(addr) => addr.extend(),437 None => return Err(self),438 };

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

lines 439–441why, one step back
439
440 // Prepare to read E out of the data structure. We'll drop the rest441 // of the data structure separately so that E is not dropped.

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

line 442why, one step back
442 let outer = ManuallyDrop::new(self);

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

lines 443–444why, one step back
443
444 // Read E from where the vtable found it.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 445why, one step back
445 let error = addr.cast::<E>().read();

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 448why, one step back
448 (vtable(outer.inner.ptr).object_drop_rest)(outer.inner, target);

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 449why, one step back
449

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 450–495why, one step back
450 Ok(error)
451 }452 }453454 /// Downcast this error object by reference.455 ///456 /// # Example457 ///458 /// ```459 /// # use anyhow::anyhow;460 /// # use std::fmt::{self, Display};461 /// # use std::task::Poll;462 /// #463 /// # #[derive(Debug)]464 /// # enum DataStoreError {465 /// # Censored(()),466 /// # }467 /// #468 /// # impl Display for DataStoreError {469 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {470 /// # unimplemented!()471 /// # }472 /// # }473 /// #474 /// # impl std::error::Error for DataStoreError {}475 /// #476 /// # const REDACTED_CONTENT: () = ();477 /// #478 /// # let error = anyhow!("...");479 /// # let root_cause = &error;480 /// #481 /// # let ret =482 /// // If the error was caused by redaction, then return a tombstone instead483 /// // of the content.484 /// match root_cause.downcast_ref::<DataStoreError>() {485 /// Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),486 /// None => Err(error),487 /// }488 /// # ;489 /// ```490 pub fn downcast_ref<E>(&self) -> Option<&E>491 where492 E: Display + Debug + Send + Sync + 'static,493 {494 let target = TypeId::of::<E>();495 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

lines 496–497why, one step back
496 // Use vtable to find NonNull<()> which points to a value of type E
497 // somewhere inside the data structure.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 498–499why, one step back
498 let addr = (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?;
499 Some(addr.cast::<E>().deref())

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 500–509why, one step back
500 }
501 }502503 /// Downcast this error object by mutable reference.504 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>505 where506 E: Display + Debug + Send + Sync + 'static,507 {508 let target = TypeId::of::<E>();509 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

lines 510–511why, one step back
510 // Use vtable to find NonNull<()> which points to a value of type E
511 // somewhere inside the data structure.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 512–517why, one step back
512
513 #[cfg(not(anyhow_no_ptr_addr_of))]514 let addr =515 (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?.by_mut();516517 #[cfg(anyhow_no_ptr_addr_of)]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 518why, one step back
518 let addr = (vtable(self.inner.ptr).object_downcast_mut)(self.inner.by_mut(), target)?;

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 519why, one step back
519

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

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

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 527–530why, one step back
527impl<E> From<E> for Error
528where529 E: StdError + Send + Sync + 'static,530{

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 532–537why, one step back
532 fn from(error: E) -> Self {
533 let backtrace = backtrace_if_absent!(error);534 Error::from_std(error, backtrace)535 }536}537

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

line 538why, one step back
538#[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")))]

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 545–547why, one step back
545 }
546}547

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

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

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 553–557why, one step back
553 }
554}555556impl Display for Error {557 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 559–563why, one step back
559 }
560}561562impl Debug for Error {563 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 565–570why, one step back
565 }
566}567568impl Drop for Error {569 fn drop(&mut self) {570 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 571why, one step back
571 // Invoke the vtable's drop behavior.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 573–577why, one step back
573 }
574 }575}576577struct ErrorVTable {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 589–590why, one step back
589}
590

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 596–598why, one step back
596 drop(unerased);
597}598

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 606–608why, one step back
606 drop(unerased);
607}608

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 611–613why, one step back
611where
612 E: StdError + Send + Sync + 'static,613{

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 614why, one step back
614 // Attach E's native StdError vtable onto a pointer to self._object.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 615–624why, one step back
615
616 let unerased = e.cast::<ErrorImpl<E>>();617618 #[cfg(not(anyhow_no_ptr_addr_of))]619 return Ref::from_raw(NonNull::new_unchecked(620 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,621 ));622623 #[cfg(anyhow_no_ptr_addr_of)]624 return Ref::new(&unerased.deref()._object);

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 625–626why, one step back
625}
626

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 631–633why, one step back
631where
632 E: StdError + Send + Sync + 'static,633{

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 634why, one step back
634 // Attach E's native StdError vtable onto a pointer to self._object.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 636–637why, one step back
636}
637

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 640–642why, one step back
640where
641 E: StdError + Send + Sync + 'static,642{

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 643why, one step back
643 // Attach ErrorImpl<E>'s native StdError vtable. The StdError impl is below.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 645–646why, one step back
645}
646

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 649–652why, one step back
649where
650 E: 'static,651{652 if TypeId::of::<E>() == target {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

lines 653–654why, one step back
653 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a
654 // pointer to its E field.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 655–667why, one step back
655
656 let unerased = e.cast::<ErrorImpl<E>>();657658 #[cfg(not(anyhow_no_ptr_addr_of))]659 return Some(660 Ref::from_raw(NonNull::new_unchecked(661 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,662 ))663 .cast::<()>(),664 );665666 #[cfg(anyhow_no_ptr_addr_of)]667 return Some(Ref::new(&unerased.deref()._object).cast::<()>());

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 668–672why, one step back
668 } else {
669 None670 }671}672

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 676–681why, one step back
676where
677 E: 'static,678{679 if TypeId::of::<E>() == target {680 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a681 // pointer to its E field.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 684–688why, one step back
684 } else {
685 None686 }687}688

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 689–694why, one step back
689#[cfg(all(not(backtrace), feature = "backtrace"))]
690fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {691 let _ = e;692 None693}694

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 698–702why, one step back
698where
699 C: 'static,700 E: 'static,701{702 if TypeId::of::<C>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 708–712why, one step back
708 } else {
709 None710 }711}712

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 716–719why, one step back
716where
717 C: 'static,718 E: 'static,719{

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 726–732why, one step back
726 } else {
727 None728 }729}730731// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.732#[cfg(feature = "std")]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 734–737why, one step back
734where
735 C: 'static,736 E: 'static,737{

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 744–745why, one step back
744 drop(unerased);
745 } else {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 749–752why, one step back
749 drop(unerased);
750 }751}752

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 753why, one step back
753// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 755–757why, one step back
755where
756 C: 'static,757{

Resolve branches_sharing_code clippy lintDavid Tolnaybefore adoptionaa6c83d

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

line 761why, one step back
761 } else {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 762why, one step back
762 // Recurse down the context chain per the inner error's vtable.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 765–767why, one step back
765 }
766}767

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 768why, one step back
768// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 771–773why, one step back
771where
772 C: 'static,773{

Resolve branches_sharing_code clippy lintDavid Tolnaybefore adoptionaa6c83d

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 786–788why, one step back
786where
787 C: 'static,788{

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 796–797why, one step back
796 drop(unerased);
797 } else {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 807–809why, one step back
807 }
808}809

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 810–821why, one step back
810// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.
811#[cfg(all(not(backtrace), feature = "backtrace"))]812#[allow(clippy::unnecessary_wraps)]813unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>814where815 C: 'static,816{817 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();818 let backtrace = ErrorImpl::backtrace(unerased._object.error.inner.by_ref());819 Some(backtrace)820}821

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 827–833why, one step back
827 vtable: &'static ErrorVTable,
828 backtrace: Option<Backtrace>,829 // NOTE: Don't use directly. Use only through vtable. Erased type may have830 // different alignment.831 _object: E,832}833

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Only need vtable function for a type erased ErrorImplDavid Tolnaybefore adoptiond1b0c9a

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 839–840why, one step back
839}
840

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 841–849why, one step back
841// repr C to ensure that ContextError<C, E> has the same layout as
842// ContextError<ManuallyDrop<C>, E> and ContextError<C, ManuallyDrop<E>>.843#[repr(C)]844pub(crate) struct ContextError<C, E> {845 pub context: C,846 pub error: E,847}848849impl<E> ErrorImpl<E> {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Fix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 855–857why, one step back
855 }
856}857

Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b

line 858why, one step back
858impl ErrorImpl {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 863–864why, one step back
863 }
864

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

line 866why, one step back
866 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 869–876why, one step back
869
870 #[cfg(not(anyhow_no_ptr_addr_of))]871 return (vtable(this.ptr).object_ref)(this.by_ref())872 .by_mut()873 .deref_mut();874875 #[cfg(anyhow_no_ptr_addr_of)]876 return (vtable(this.ptr).object_mut)(this);

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 877–878why, one step back
877 }
878

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 881–883why, one step back
881 // This unwrap can only panic if the underlying error's backtrace method
882 // is nondeterministic, which would only happen in maliciously883 // constructed code.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 885why, one step back
885 .backtrace

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 887–892why, one step back
887 .or_else(|| {
888 #[cfg(backtrace)]889 return Self::error(this).backtrace();890 #[cfg(all(not(backtrace), feature = "backtrace"))]891 return (vtable(this.ptr).object_backtrace)(this);892 })

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 893–895why, one step back
893 .expect("backtrace capture failed")
894 }895

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 899–907why, one step back
899 }
900}901902impl<E> StdError for ErrorImpl<E>903where904 E: StdError,905{906 #[cfg(backtrace)]907 fn backtrace(&self) -> Option<&Backtrace> {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 908why, one step back
908 Some(unsafe { ErrorImpl::backtrace(self.erase()) })

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 909–911why, one step back
909 }
910911 fn source(&self) -> Option<&(dyn StdError + 'static)> {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 913–920why, one step back
913 }
914}915916impl<E> Debug for ErrorImpl<E>917where918 E: Debug,919{920 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 922–929why, one step back
922 }
923}924925impl<E> Display for ErrorImpl<E>926where927 E: Display,928{929 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 931–934why, one step back
931 }
932}933934impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {

Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 936–938why, one step back
936 fn from(error: Error) -> Self {
937 let outer = ManuallyDrop::new(error);938 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

lines 939–940why, one step back
939 // Use vtable to attach ErrorImpl<E>'s native StdError vtable for
940 // the right original type E.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 942–945why, one step back
942 }
943 }944}945

Merge pull request #104 from dtolnay/fromDavid Tolnaybefore adoption03a1916

lines 946–951why, one step back
946impl From<Error> for Box<dyn StdError + Send + 'static> {
947 fn from(error: Error) -> Self {948 Box::<dyn StdError + Send + Sync>::from(error)949 }950}951

Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab

lines 952–956why, one step back
952impl From<Error> for Box<dyn StdError + 'static> {
953 fn from(error: Error) -> Self {954 Box::<dyn StdError + Send + Sync>::from(error)955 }956}

Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164

line 957why, one step back
957

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164

lines 959–964why, one step back
959impl AsRef<dyn StdError + Send + Sync> for Error {
960 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {961 &**self962 }963}964

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164

lines 966–970why, one step back
966impl AsRef<dyn StdError> for Error {
967 fn as_ref(&self) -> &(dyn StdError + 'static) {968 &**self969 }970}