Skip to content
Myr

error.rs

This file

Path

src/error.rs

956 lines · 32.9 KiB · Rust

Last changed by

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

History of this file

Take it

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

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

line 1why, one step back
1use crate::alloc::Box;

Move backtrace capture to backtrace module and macroDavid Tolnaybefore adoptionebd9101

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

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

line 3why, one step back
3use crate::chain::Chain;

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 4–6why, one step back
4#[cfg(any(feature = "std", anyhow_no_ptr_addr_of))]
5use crate::ptr::Mut;6use crate::ptr::{Own, Ref};

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

lines 7–9why, one step back
7use crate::{Error, StdError};
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;

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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 #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 28–31why, one step back
28 pub fn new<E>(error: E) -> Self
29 where30 E: StdError + Send + Sync + 'static,31 {

Move Error::new backtrace capture to backtrace_if_absent macroDavid Tolnaybefore adoptioneaa819d

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

Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9

lines 33–34why, one step back
33 Error::from_std(error, backtrace)
34 }

Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298

line 35why, one step back
35

Add Error::msg constructor to simplify map_err combinatorsDavid Tolnaybefore adoption04b11c5

lines 36–79why, one step back
36 /// Create a new error object from a printable error message.
37 ///38 /// If the argument implements std::error::Error, prefer `Error::new`39 /// instead which preserves the underlying error's cause chain and40 /// backtrace. If the argument may or may not implement std::error::Error41 /// now or in the future, use `anyhow!(err)` which handles either way42 /// correctly.43 ///44 /// `Error::msg("...")` is equivalent to `anyhow!("...")` but occasionally45 /// convenient in places where a function is preferable over a macro, such46 /// as iterator or stream combinators:47 ///48 /// ```49 /// # mod ffi {50 /// # pub struct Input;51 /// # pub struct Output;52 /// # pub async fn do_some_work(_: Input) -> Result<Output, &'static str> {53 /// # unimplemented!()54 /// # }55 /// # }56 /// #57 /// # use ffi::{Input, Output};58 /// #59 /// use anyhow::{Error, Result};60 /// use futures::stream::{Stream, StreamExt, TryStreamExt};61 ///62 /// async fn demo<S>(stream: S) -> Result<Vec<Output>>63 /// where64 /// S: Stream<Item = Input>,65 /// {66 /// stream67 /// .then(ffi::do_some_work) // returns Result<Output, &str>68 /// .map_err(Error::msg)69 /// .try_collect()70 /// .await71 /// }72 /// ```73 pub fn msg<M>(message: M) -> Self74 where75 M: Display + Debug + Send + Sync + 'static,76 {77 Error::from_adhoc(message, backtrace!())78 }79

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9

lines 81–84why, one step back
81 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self
82 where83 E: StdError + Send + Sync + 'static,84 {

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 85–87why, one step back
85 let vtable = &ErrorVTable {
86 object_drop: object_drop::<E>,87 object_ref: object_ref::<E>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 89–90why, one step back
89 object_mut: object_mut::<E>,
90 object_boxed: object_boxed::<E>,

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 91why, one step back
91 object_downcast: object_downcast::<E>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

line 97why, one step back
97 };

Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9

line 98why, one step back
98

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 99–100why, one step back
99 // Safety: passing vtable that operates on the right type E.
100 unsafe { Error::construct(error, vtable, backtrace) }

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 101–102why, one step back
101 }
102

Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 104–106why, one step back
104 where
105 M: Display + Debug + Send + Sync + 'static,106 {

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Specify types where the manual vtable relies on themDavid Tolnaybefore adoptionb261a64

line 108why, one step back
108 let error: MessageError<M> = MessageError(message);

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 109–111why, one step back
109 let vtable = &ErrorVTable {
110 object_drop: object_drop::<MessageError<M>>,111 object_ref: object_ref::<MessageError<M>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 113–114why, one step back
113 object_mut: object_mut::<MessageError<M>>,
114 object_boxed: object_boxed::<MessageError<M>>,

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 115why, one step back
115 object_downcast: object_downcast::<M>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

line 121why, one step back
121 };

Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298

line 122why, one step back
122

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 123–125why, one step back
123 // Safety: MessageError is repr(transparent) so it is okay for the
124 // vtable to allow casting the MessageError<M> to M.125 unsafe { Error::construct(error, vtable, backtrace) }

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 126–127why, one step back
126 }
127

Merge pull request #8 from dtolnay/optionDavid Tolnaybefore adoption240cc83

lines 128–131why, one step back
128 pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self
129 where130 M: Display + Send + Sync + 'static,131 {

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Specify types where the manual vtable relies on themDavid Tolnaybefore adoptionb261a64

line 133why, one step back
133 let error: DisplayError<M> = DisplayError(message);

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 134–136why, one step back
134 let vtable = &ErrorVTable {
135 object_drop: object_drop::<DisplayError<M>>,136 object_ref: object_ref::<DisplayError<M>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 138–139why, one step back
138 object_mut: object_mut::<DisplayError<M>>,
139 object_boxed: object_boxed::<DisplayError<M>>,

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 140why, one step back
140 object_downcast: object_downcast::<M>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

line 146why, one step back
146 };

Merge pull request #8 from dtolnay/optionDavid Tolnaybefore adoption240cc83

line 147why, one step back
147

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 148–150why, one step back
148 // Safety: DisplayError is repr(transparent) so it is okay for the
149 // vtable to allow casting the DisplayError<M> to M.150 unsafe { Error::construct(error, vtable, backtrace) }

Merge pull request #8 from dtolnay/optionDavid Tolnaybefore adoption240cc83

lines 151–152why, one step back
151 }
152

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6

line 154why, one step back
154 pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self

Move ContextError to error.rs in preparation for refactorDavid Tolnaybefore adoption30c2eab

lines 155–158why, one step back
155 where
156 C: Display + Send + Sync + 'static,157 E: StdError + Send + Sync + 'static,158 {

Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6

lines 159–163why, one step back
159 let error: ContextError<C, E> = ContextError { context, error };
160161 let vtable = &ErrorVTable {162 object_drop: object_drop::<ContextError<C, E>>,163 object_ref: object_ref::<ContextError<C, E>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6

lines 165–166why, one step back
165 object_mut: object_mut::<ContextError<C, E>>,
166 object_boxed: object_boxed::<ContextError<C, E>>,

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 167why, one step back
167 object_downcast: context_downcast::<C, E>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6

lines 173–175why, one step back
173 };
174175 // Safety: passing vtable that operates on the right type.

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

lines 176–178why, one step back
176 unsafe { Error::construct(error, vtable, backtrace) }
177 }178

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

lines 180–183why, one step back
180 pub(crate) fn from_boxed(
181 error: Box<dyn StdError + Send + Sync>,182 backtrace: Option<Backtrace>,183 ) -> Self {

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

lines 185–188why, one step back
185 let error = BoxedError(error);
186 let vtable = &ErrorVTable {187 object_drop: object_drop::<BoxedError>,188 object_ref: object_ref::<BoxedError>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

lines 190–192why, one step back
190 object_mut: object_mut::<BoxedError>,
191 object_boxed: object_boxed::<BoxedError>,192 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6

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

Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6

line 202why, one step back
202 unsafe { Error::construct(error, vtable, backtrace) }

Move ContextError to error.rs in preparation for refactorDavid Tolnaybefore adoption30c2eab

lines 203–204why, one step back
203 }
204

Move Error::new backtrace capture to backtrace_if_absent macroDavid Tolnaybefore adoptioneaa819d

lines 205–206why, one step back
205 // Takes backtrace as argument rather than capturing it here so that the
206 // user sees one fewer layer of wrapping noise in the backtrace.

Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298

line 207why, one step back
207 //

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 208–214why, one step back
208 // Unsafe because the given vtable must have sensible behavior on the error
209 // value of type E.210 unsafe fn construct<E>(211 error: E,212 vtable: &'static ErrorVTable,213 backtrace: Option<Backtrace>,214 ) -> Self

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 215–217why, one step back
215 where
216 E: StdError + Send + Sync + 'static,217 {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298

lines 219–220why, one step back
219 vtable,
220 backtrace,

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 221why, one step back
221 _object: error,

Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298

line 222why, one step back
222 });

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Fix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f

line 230why, one step back
230 Error { inner }

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 231–259why, one step back
231 }
232233 /// Wrap the error value with additional context.234 ///235 /// For attaching context to a `Result` as it is propagated, the236 /// [`Context`][crate::Context] extension trait may be more convenient than237 /// this function.238 ///239 /// The primary reason to use `error.context(...)` instead of240 /// `result.context(...)` via the `Context` trait would be if the context241 /// needs to depend on some data held by the underlying error:242 ///243 /// ```244 /// # use std::fmt::{self, Debug, Display};245 /// #246 /// # type T = ();247 /// #248 /// # impl std::error::Error for ParseError {}249 /// # impl Debug for ParseError {250 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {251 /// # unimplemented!()252 /// # }253 /// # }254 /// # impl Display for ParseError {255 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {256 /// # unimplemented!()257 /// # }258 /// # }259 /// #

Clean up Error::context example using anyhow ResultDavid Tolnaybefore adoptionf8457fb

line 260why, one step back
260 /// use anyhow::Result;

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 261–275why, one step back
261 /// use std::fs::File;
262 /// use std::path::Path;263 ///264 /// struct ParseError {265 /// line: usize,266 /// column: usize,267 /// }268 ///269 /// fn parse_impl(file: File) -> Result<T, ParseError> {270 /// # const IGNORE: &str = stringify! {271 /// ...272 /// # };273 /// # unimplemented!()274 /// }275 ///

Clean up Error::context example using anyhow ResultDavid Tolnaybefore adoptionf8457fb

line 276why, one step back
276 /// pub fn parse(path: impl AsRef<Path>) -> Result<T> {

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 277–290why, one step back
277 /// let file = File::open(&path)?;
278 /// parse_impl(file).map_err(|error| {279 /// let context = format!(280 /// "only the first {} lines of {} are valid",281 /// error.line, path.as_ref().display(),282 /// );283 /// anyhow::Error::new(error).context(context)284 /// })285 /// }286 /// ```287 pub fn context<C>(self, context: C) -> Self288 where289 C: Display + Send + Sync + 'static,290 {

Specify types where the manual vtable relies on themDavid Tolnaybefore adoptionb261a64

line 291why, one step back
291 let error: ContextError<C, Error> = ContextError {

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

line 292why, one step back
292 context,

Swap ContextError type parameters to match type nameDavid Tolnaybefore adoption9f38495

line 293why, one step back
293 error: self,

Eliminate backtrace_if_absent in Error::contextDavid Tolnaybefore adoptiona2846fd

lines 294–298why, one step back
294 };
295296 let vtable = &ErrorVTable {297 object_drop: object_drop::<ContextError<C, Error>>,298 object_ref: object_ref::<ContextError<C, Error>>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Eliminate backtrace_if_absent in Error::contextDavid Tolnaybefore adoptiona2846fd

lines 300–301why, one step back
300 object_mut: object_mut::<ContextError<C, Error>>,
301 object_boxed: object_boxed::<ContextError<C, Error>>,

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 302why, one step back
302 object_downcast: context_chain_downcast::<C>,

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Eliminate backtrace_if_absent in Error::contextDavid Tolnaybefore adoptiona2846fd

lines 308–314why, one step back
308 };
309310 // As the cause is anyhow::Error, we already have a backtrace for it.311 let backtrace = None;312313 // Safety: passing vtable that operates on the right type.314 unsafe { Error::construct(error, vtable, backtrace) }

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 315–318why, one step back
315 }
316317 /// Get the backtrace for this Error.318 ///

Merge pull request #70 from dtolnay/envDavid Tolnaybefore adoption8d2acea

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

Merge pull request #70 from dtolnay/envDavid Tolnaybefore adoption8d2acea

lines 324–330why, one step back
324 ///
325 /// - If you want panics and errors to both have backtraces, set326 /// `RUST_BACKTRACE=1`;327 /// - If you want only errors to have backtraces, set328 /// `RUST_LIB_BACKTRACE=1`;329 /// - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and330 /// `RUST_LIB_BACKTRACE=0`.

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

line 331why, one step back
331 ///

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 332–345why, one step back
332 /// # Stability
333 ///334 /// Standard library backtraces are only available on the nightly channel.335 /// Tracking issue: [rust-lang/rust#53487][tracking].336 ///337 /// On stable compilers, this function is only available if the crate's338 /// "backtrace" feature is enabled, and will use the `backtrace` crate as339 /// the underlying backtrace implementation.340 ///341 /// ```toml342 /// [dependencies]343 /// anyhow = { version = "1.0", features = ["backtrace"] }344 /// ```345 ///

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 351–373why, one step back
351 }
352353 /// An iterator of the chain of source errors contained by this Error.354 ///355 /// This iterator will visit every error in the cause chain of this error356 /// object, beginning with the error that this error object was created357 /// from.358 ///359 /// # Example360 ///361 /// ```362 /// use anyhow::Error;363 /// use std::io;364 ///365 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {366 /// for cause in error.chain() {367 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {368 /// return Some(io_error.kind());369 /// }370 /// }371 /// None372 /// }373 /// ```

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

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

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 378–384why, one step back
378 }
379380 /// The lowest level cause of this error &mdash; this error's cause's381 /// cause's cause etc.382 ///383 /// The root cause is the last error in the iterator produced by384 /// [`chain()`][Error::chain].

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

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

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

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

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 389–390why, one step back
389 }
390

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 391–398why, one step back
391 /// Returns true if `E` is the type held by this error object.
392 ///393 /// For errors with context, this method returns true if `E` matches the394 /// type of the context `C` **or** the type of the error on which the395 /// context has been attached. For details about the interaction between396 /// context and downcasting, [see here].397 ///398 /// [see here]: trait.Context.html#effect-on-downcasting

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 399–402why, one step back
399 pub fn is<E>(&self) -> bool
400 where401 E: Display + Debug + Send + Sync + 'static,402 {

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 403why, one step back
403 self.downcast_ref::<E>().is_some()

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 404–406why, one step back
404 }
405406 /// Attempt to downcast the error object to a concrete type.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 408–410why, one step back
408 where
409 E: Display + Debug + Send + Sync + 'static,410 {

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 411why, one step back
411 let target = TypeId::of::<E>();

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 413why, one step back
413 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 416–425why, one step back
416 #[cfg(not(anyhow_no_ptr_addr_of))]
417 let addr = match (vtable(inner.ptr).object_downcast)(inner.by_ref(), target) {418 Some(addr) => addr.by_mut().extend(),419 None => return Err(self),420 };421 #[cfg(anyhow_no_ptr_addr_of)]422 let addr = match (vtable(inner.ptr).object_downcast_mut)(inner, target) {423 Some(addr) => addr.extend(),424 None => return Err(self),425 };

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

lines 426–428why, one step back
426
427 // Prepare to read E out of the data structure. We'll drop the rest428 // of the data structure separately so that E is not dropped.

Deallocate with the right Layout during downcastDavid Tolnaybefore adoption22c2608

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

line 436why, one step back
436

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 437why, one step back
437 Ok(error)

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 438–480why, one step back
438 }
439 }440441 /// Downcast this error object by reference.442 ///443 /// # Example444 ///445 /// ```446 /// # use anyhow::anyhow;447 /// # use std::fmt::{self, Display};448 /// # use std::task::Poll;449 /// #450 /// # #[derive(Debug)]451 /// # enum DataStoreError {452 /// # Censored(()),453 /// # }454 /// #455 /// # impl Display for DataStoreError {456 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {457 /// # unimplemented!()458 /// # }459 /// # }460 /// #461 /// # impl std::error::Error for DataStoreError {}462 /// #463 /// # const REDACTED_CONTENT: () = ();464 /// #465 /// # let error = anyhow!("...");466 /// # let root_cause = &error;467 /// #468 /// # let ret =469 /// // If the error was caused by redaction, then return a tombstone instead470 /// // of the content.471 /// match root_cause.downcast_ref::<DataStoreError>() {472 /// Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),473 /// None => Err(error),474 /// }475 /// # ;476 /// ```477 pub fn downcast_ref<E>(&self) -> Option<&E>478 where479 E: Display + Debug + Send + Sync + 'static,480 {

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 481–482why, one step back
481 let target = TypeId::of::<E>();
482 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 487–494why, one step back
487 }
488 }489490 /// Downcast this error object by mutable reference.491 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>492 where493 E: Display + Debug + Send + Sync + 'static,494 {

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 495–496why, one step back
495 let target = TypeId::of::<E>();
496 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 499–504why, one step back
499
500 #[cfg(not(anyhow_no_ptr_addr_of))]501 let addr =502 (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?.by_mut();503504 #[cfg(anyhow_no_ptr_addr_of)]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

line 506why, one step back
506

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 508–511why, one step back
508 }
509 }510}511

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

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

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 514–518why, one step back
514impl<E> From<E> for Error
515where516 E: StdError + Send + Sync + 'static,517{518 fn from(error: E) -> Self {

Capture backtraces earlierDavid Tolnaybefore adoptionc06155e

lines 519–520why, one step back
519 let backtrace = backtrace_if_absent!(error);
520 Error::from_std(error, backtrace)

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

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

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 531–533why, one step back
531 }
532}533

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

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

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 539–541why, one step back
539 }
540}541

Merge pull request #42 from dtolnay/fmtDavid Tolnaybefore adoption74954bd

line 542why, one step back
542impl Display for Error {

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

line 543why, one step back
543 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

Merge pull request #42 from dtolnay/fmtDavid Tolnaybefore adoption74954bd

lines 548–549why, one step back
548impl Debug for Error {
549 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 551–555why, one step back
551 }
552}553554impl Drop for Error {555 fn drop(&mut self) {

Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f

line 556why, one step back
556 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f

line 559why, one step back
559 }

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 560–562why, one step back
560 }
561}562

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

line 563why, one step back
563struct ErrorVTable {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f

lines 575–576why, one step back
575}
576

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Deallocate with the right Layout during downcastDavid Tolnaybefore adoption22c2608

lines 582–584why, one step back
582 drop(unerased);
583}584

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Deallocate with the right Layout during downcastDavid Tolnaybefore adoption22c2608

lines 587–589why, one step back
587 // Drop the fields of ErrorImpl other than E as well as the Box allocation,
588 // without dropping E itself. This is used by downcast after doing a589 // ptr::read to take ownership of the E.

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 590why, one step back
590 let _ = target;

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f

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

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

lines 593–594why, one step back
593}
594

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

lines 597–599why, one step back
597where
598 E: StdError + Send + Sync + 'static,599{

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 601–610why, one step back
601
602 let unerased = e.cast::<ErrorImpl<E>>();603604 #[cfg(not(anyhow_no_ptr_addr_of))]605 return Ref::from_raw(NonNull::new_unchecked(606 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,607 ));608609 #[cfg(anyhow_no_ptr_addr_of)]610 return Ref::new(&unerased.deref()._object);

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

lines 611–612why, one step back
611}
612

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

lines 617–619why, one step back
617where
618 E: StdError + Send + Sync + 'static,619{

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

lines 622–623why, one step back
622}
623

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

lines 626–628why, one step back
626where
627 E: StdError + Send + Sync + 'static,628{

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

lines 631–632why, one step back
631}
632

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

line 635why, one step back
635where

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 636why, one step back
636 E: 'static,

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

line 637why, one step back
637{

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 641–653why, one step back
641
642 let unerased = e.cast::<ErrorImpl<E>>();643644 #[cfg(not(anyhow_no_ptr_addr_of))]645 return Some(646 Ref::from_raw(NonNull::new_unchecked(647 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,648 ))649 .cast::<()>(),650 );651652 #[cfg(anyhow_no_ptr_addr_of)]653 return Some(Ref::new(&unerased.deref()._object).cast::<()>());

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 654–656why, one step back
654 } else {
655 None656 }

Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93

lines 657–658why, one step back
657}
658

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 670–674why, one step back
670 } else {
671 None672 }673}674

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 675–680why, one step back
675#[cfg(all(not(backtrace), feature = "backtrace"))]
676fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {677 let _ = e;678 None679}680

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 684–688why, one step back
684where
685 C: 'static,686 E: 'static,687{688 if TypeId::of::<C>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 694–698why, one step back
694 } else {
695 None696 }697}698

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 702–705why, one step back
702where
703 C: 'static,704 E: 'static,705{

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 712–718why, one step back
712 } else {
713 None714 }715}716717// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.718#[cfg(feature = "std")]

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 720–723why, one step back
720where
721 C: 'static,722 E: 'static,723{

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 730–731why, one step back
730 drop(unerased);
731 } else {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 735–738why, one step back
735 drop(unerased);
736 }737}738

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 741–744why, one step back
741where
742 C: 'static,743{744 if TypeId::of::<C>() == target {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 747why, one step back
747 } else {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 749–751why, one step back
749 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();
750 let source = &unerased._object.error;751 (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target)

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 752–754why, one step back
752 }
753}754

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 758–760why, one step back
758where
759 C: 'static,760{

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

lines 766–768why, one step back
766 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();
767 let source = &mut unerased._object.error;768 (vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target)

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 769–772why, one step back
769 }
770}771772// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 774–776why, one step back
774where
775 C: 'static,776{

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 784–785why, one step back
784 drop(unerased);
785 } else {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 795–797why, one step back
795 }
796}797

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 798–809why, one step back
798// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.
799#[cfg(all(not(backtrace), feature = "backtrace"))]800#[allow(clippy::unnecessary_wraps)]801unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>802where803 C: 'static,804{805 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();806 let backtrace = ErrorImpl::backtrace(unerased._object.error.inner.by_ref());807 Some(backtrace)808}809

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

line 812why, one step back
812// repr C to ensure that E remains in the final position.

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

line 813why, one step back
813#[repr(C)]

Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b

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

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

line 815why, one step back
815 vtable: &'static ErrorVTable,

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

line 816why, one step back
816 backtrace: Option<Backtrace>,

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 817–819why, one step back
817 // NOTE: Don't use directly. Use only through vtable. Erased type may have
818 // different alignment.819 _object: E,

Move ContextError to error.rs in preparation for refactorDavid Tolnaybefore adoption30c2eab

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Only need vtable function for a type erased ErrorImplDavid Tolnaybefore adoptiond1b0c9a

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

lines 827–828why, one step back
827}
828

Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0

lines 829–831why, one step back
829// repr C to ensure that ContextError<C, E> has the same layout as
830// ContextError<ManuallyDrop<C>, E> and ContextError<C, ManuallyDrop<E>>.831#[repr(C)]

Move ContextError to error.rs in preparation for refactorDavid Tolnaybefore adoption30c2eab

lines 832–834why, one step back
832pub(crate) struct ContextError<C, E> {
833 pub context: C,834 pub error: E,

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 835–836why, one step back
835}
836

Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452

line 837why, one step back
837impl<E> ErrorImpl<E> {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Fix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452

lines 843–845why, one step back
843 }
844}845

Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b

line 846why, one step back
846impl ErrorImpl {

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

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

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

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

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0

lines 857–864why, one step back
857
858 #[cfg(not(anyhow_no_ptr_addr_of))]859 return (vtable(this.ptr).object_ref)(this.by_ref())860 .by_mut()861 .deref_mut();862863 #[cfg(anyhow_no_ptr_addr_of)]864 return (vtable(this.ptr).object_mut)(this);

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

lines 865–866why, one step back
865 }
866

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

lines 869–871why, one step back
869 // This unwrap can only panic if the underlying error's backtrace method
870 // is nondeterministic, which would only happen in maliciously871 // constructed code.

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

line 873why, one step back
873 .backtrace

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

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

Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36

lines 875–880why, one step back
875 .or_else(|| {
876 #[cfg(backtrace)]877 return Self::error(this).backtrace();878 #[cfg(all(not(backtrace), feature = "backtrace"))]879 return (vtable(this.ptr).object_backtrace)(this);880 })

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

lines 881–883why, one step back
881 .expect("backtrace capture failed")
882 }883

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

lines 886–888why, one step back
886 }
887}888

Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452

lines 889–894why, one step back
889impl<E> StdError for ErrorImpl<E>
890where891 E: StdError,892{893 #[cfg(backtrace)]894 fn backtrace(&self) -> Option<&Backtrace> {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452

line 896why, one step back
896 }

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

line 897why, one step back
897

Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452

line 898why, one step back
898 fn source(&self) -> Option<&(dyn StdError + 'static)> {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452

lines 900–902why, one step back
900 }
901}902

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

lines 903–907why, one step back
903impl<E> Debug for ErrorImpl<E>
904where905 E: Debug,906{907 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

lines 909–916why, one step back
909 }
910}911912impl<E> Display for ErrorImpl<E>913where914 E: Display,915{916 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {

Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df

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

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

lines 918–924why, one step back
918 }
919}920921impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {922 fn from(error: Error) -> Self {923 let outer = ManuallyDrop::new(error);924 unsafe {

Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60

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

Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2

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

Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8

line 928why, one step back
928 }

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 929–931why, one step back
929 }
930}931

Merge pull request #104 from dtolnay/fromDavid Tolnaybefore adoption03a1916

lines 932–937why, one step back
932impl From<Error> for Box<dyn StdError + Send + 'static> {
933 fn from(error: Error) -> Self {934 Box::<dyn StdError + Send + Sync>::from(error)935 }936}937

Merge pull request #28 from dtolnay/fromDavid Tolnaybefore adoption68225d2

lines 938–940why, one step back
938impl From<Error> for Box<dyn StdError + 'static> {
939 fn from(error: Error) -> Self {940 Box::<dyn StdError + Send + Sync>::from(error)

Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79

lines 941–942why, one step back
941 }
942}

Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164

line 943why, one step back
943

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164

lines 945–950why, one step back
945impl AsRef<dyn StdError + Send + Sync> for Error {
946 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {947 &**self948 }949}950

Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9

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

Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164

lines 952–956why, one step back
952impl AsRef<dyn StdError> for Error {
953 fn as_ref(&self) -> &(dyn StdError + 'static) {954 &**self955 }956}