Skip to content
Myr

error.rs

This file

Path

src/error.rs

519 lines · 14.4 KiB · Rust

Last changed by

Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9

History of this file

Take it

Move backtrace capture to backtrace module and macroDavid Tolnaybefore adoptionebd9101

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

Add context method to ErrorDavid Tolnaybefore adoption41fde4c

line 2why, one step back
2use crate::context::ContextError;

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 3why, one step back
3use std::any::TypeId;

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 4why, one step back
4use std::error::Error as StdError;

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 5–9why, one step back
5use std::fmt::{self, Debug, Display};
6use std::mem;7use std::ops::{Deref, DerefMut};8use std::ptr;9

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 10why, one step back
10/// The `Error` type, a wrapper around a dynamic error type.

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 11why, one step back
11///

Touch up documentation on Error structDavid Tolnaybefore adoptiona8ce6e4

line 12why, one step back
12/// `Error` works a lot like `Box<dyn std::error::Error>`, but with these

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

line 13why, one step back
13/// differences:

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 14why, one step back
14///

Touch up documentation on Error structDavid Tolnaybefore adoptiona8ce6e4

lines 15–19why, one step back
15/// - `Error` requires that the error is `Send`, `Sync`, and `'static`.
16/// - `Error` guarantees that a backtrace is available, even if the underlying17/// error type does not provide one.18/// - `Error` is represented as a narrow pointer &mdash; exactly one word in19/// size instead of two.

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 20why, one step back
20pub struct Error {

Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84

line 21why, one step back
21 inner: Box<ErrorImpl<()>>,

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 22–23why, one step back
22}
23

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 24why, one step back
24impl Error {

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

line 25why, one step back
25 /// Create a new error object from any error type.

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 26why, one step back
26 ///

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

lines 27–28why, one step back
27 /// The error type must be threadsafe and `'static`, so that the `Error`
28 /// will be as well.

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 29why, one step back
29 ///

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

lines 30–31why, one step back
30 /// If the error type does not provide a backtrace, a backtrace will be
31 /// created here to ensure that a backtrace exists.

Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337

line 32why, one step back
32 pub fn new<E>(error: E) -> Self

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 33why, one step back
33 where

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 34why, one step back
34 E: StdError + Send + Sync + 'static,

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 35why, one step back
35 {

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

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

Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9

lines 37–38why, one step back
37 Error::from_std(error, backtrace)
38 }

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

line 39why, one step back
39

Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9

lines 40–45why, one step back
40 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self
41 where42 E: StdError + Send + Sync + 'static,43 {44 let type_id = TypeId::of::<E>();45

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

line 46why, one step back
46 // Safety: passing typeid of the right type E.

Format with rustfmt 2019-09-08David Tolnaybefore adoption2a9e5b6

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 48–49why, one step back
48 }
49

Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 51–53why, one step back
51 where
52 M: Display + Debug + Send + Sync + 'static,53 {

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

lines 54–58why, one step back
54 let error = MessageError(message);
55 let type_id = TypeId::of::<M>();5657 // Safety: MessageError is repr(transparent) so MessageError<M> has the58 // same layout as the typeid specifies.

Format with rustfmt 2019-09-08David Tolnaybefore adoption2a9e5b6

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 60–61why, one step back
60 }
61

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

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

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

lines 64–67why, one step back
64 //
65 // Unsafe because the type represented by type_id must have the same layout66 // as E or else we allow invalid downcasts.67 unsafe fn construct<E>(error: E, type_id: TypeId, backtrace: Option<Backtrace>) -> Self

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 68why, one step back
68 where

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 69why, one step back
69 E: StdError + Send + Sync + 'static,

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 70why, one step back
70 {

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

lines 71–82why, one step back
71 let vtable = &ErrorVTable {
72 object: object_raw::<E>,73 object_mut: object_mut_raw::<E>,74 };75 let inner = Box::new(ErrorImpl {76 vtable,77 type_id,78 backtrace,79 error,80 });81 Error {82 inner: mem::transmute::<Box<ErrorImpl<E>>, Box<ErrorImpl<()>>>(inner),

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 83why, one step back
83 }

Add context method to ErrorDavid Tolnaybefore adoption41fde4c

lines 84–86why, one step back
84 }
8586 /// Wrap the error value with additional context.

Link to Context trait from Error::context methodDavid Tolnaybefore adoption61e587f

lines 87–90why, one step back
87 ///
88 /// For attaching context to a `Result` as it is propagated, the89 /// [`Context`][crate::Context] extension trait may be more convenient than90 /// this function.

Explain the purpose of Error::contextDavid Tolnaybefore adoption896dd7a

lines 91–138why, one step back
91 ///
92 /// The primary reason to use `error.context(...)` instead of93 /// `result.context(...)` via the `Context` trait would be if the context94 /// needs to depend on some data held by the underlying error:95 ///96 /// ```97 /// # use std::fmt::{self, Debug, Display};98 /// #99 /// # type T = ();100 /// #101 /// # impl std::error::Error for ParseError {}102 /// # impl Debug for ParseError {103 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {104 /// # unimplemented!()105 /// # }106 /// # }107 /// # impl Display for ParseError {108 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {109 /// # unimplemented!()110 /// # }111 /// # }112 /// #113 /// use std::fs::File;114 /// use std::path::Path;115 ///116 /// struct ParseError {117 /// line: usize,118 /// column: usize,119 /// }120 ///121 /// fn parse_impl(file: File) -> Result<T, ParseError> {122 /// # const IGNORE: &str = stringify! {123 /// ...124 /// # };125 /// # unimplemented!()126 /// }127 ///128 /// pub fn parse(path: impl AsRef<Path>) -> anyhow::Result<T> {129 /// let file = File::open(&path)?;130 /// parse_impl(file).map_err(|error| {131 /// let context = format!(132 /// "only the first {} lines of {} are valid",133 /// error.line, path.as_ref().display(),134 /// );135 /// anyhow::Error::new(error).context(context)136 /// })137 /// }138 /// ```

Add context method to ErrorDavid Tolnaybefore adoption41fde4c

lines 139–142why, one step back
139 pub fn context<C>(self, context: C) -> Self
140 where141 C: Display + Send + Sync + 'static,142 {

Change Error::from calls to Error::new to cut one level of noise from backtracesDavid Tolnaybefore adoption2c7d3ca

line 143why, one step back
143 Error::new(ContextError {

Add context method to ErrorDavid Tolnaybefore adoption41fde4c

lines 144–146why, one step back
144 error: self,
145 context,146 })

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 147–148why, one step back
147 }
148

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 149why, one step back
149 /// Get the backtrace for this Error.

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

lines 150–153why, one step back
150 ///
151 /// Backtraces are only available on the nightly channel. Tracking issue:152 /// [rust-lang/rust#53487][tracking].153 ///

Mention env var in Error::backtrace documentationDavid Tolnaybefore adoption4119806

lines 154–158why, one step back
154 /// In order for the backtrace to be meaningful, the environment variable
155 /// `RUST_LIB_BACKTRACE=1` must be defined. Backtraces are somewhat156 /// expensive to capture in Rust, so we don't necessarily want to be157 /// capturing them all over the place all the time.158 ///

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

lines 159–160why, one step back
159 /// [tracking]: https://github.com/rust-lang/rust/issues/53487
160 #[cfg(backtrace)]

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 161why, one step back
161 pub fn backtrace(&self) -> &Backtrace {

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

lines 162–164why, one step back
162 // NB: this unwrap can only fail if the underlying error's backtrace
163 // method is nondeterministic, which would only happen in maliciously164 // constructed code

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 165–168why, one step back
165 self.inner
166 .backtrace167 .as_ref()168 .or_else(|| self.inner.error().backtrace())

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

line 169why, one step back
169 .expect("backtrace capture failed")

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 170–171why, one step back
170 }
171

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

line 172why, one step back
172 /// An iterator of the chain of source errors contained by this Error.

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 173why, one step back
173 ///

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

line 174why, one step back
174 /// This iterator will visit every error in the cause chain of this error

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

lines 175–176why, one step back
175 /// object, beginning with the error that this error object was created
176 /// from.

Add example of Error::chainDavid Tolnaybefore adoptiond159b79

lines 177–192why, one step back
177 ///
178 /// # Example179 ///180 /// ```181 /// use anyhow::Error;182 /// use std::io;183 ///184 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {185 /// for cause in error.chain() {186 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {187 /// return Some(io_error.kind());188 /// }189 /// }190 /// None191 /// }192 /// ```

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

lines 193–194why, one step back
193 pub fn chain(&self) -> Chain {
194 Chain {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 195–198why, one step back
195 next: Some(self.inner.error()),
196 }197 }198

Add Error::root_cause accessorDavid Tolnaybefore adoption1ea72bb

lines 199–212why, one step back
199 /// The lowest level cause of this error &mdash; this error's cause's
200 /// cause's cause etc.201 ///202 /// The root cause is the last error in the iterator produced by203 /// [`chain()`][Error::chain].204 pub fn root_cause(&self) -> &(dyn StdError + 'static) {205 let mut chain = self.chain();206 let mut root_cause = chain.next().unwrap();207 for cause in chain {208 root_cause = cause;209 }210 root_cause211 }212

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

line 213why, one step back
213 /// Returns `true` if `E` is the type wrapped by this error object.

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 214–217why, one step back
214 pub fn is<E>(&self) -> bool
215 where216 E: Display + Debug + Send + Sync + 'static,217 {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 218–220why, one step back
218 TypeId::of::<E>() == self.inner.type_id
219 }220

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

line 221why, one step back
221 /// Attempt to downcast the error object to a concrete type.

Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337

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

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 223–225why, one step back
223 where
224 E: Display + Debug + Send + Sync + 'static,225 {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 226–237why, one step back
226 if let Some(error) = self.downcast_ref::<E>() {
227 unsafe {228 let error = ptr::read(error);229 drop(ptr::read(&self.inner));230 mem::forget(self);231 Ok(error)232 }233 } else {234 Err(self)235 }236 }237

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

line 238why, one step back
238 /// Downcast this error object by reference.

Add example to Error::downcast_refDavid Tolnaybefore adoption0c21b7a

lines 239–273why, one step back
239 ///
240 /// # Example241 ///242 /// ```243 /// # use anyhow::anyhow;244 /// # use std::fmt::{self, Display};245 /// # use std::task::Poll;246 /// #247 /// # #[derive(Debug)]248 /// # enum DataStoreError {249 /// # Censored(()),250 /// # }251 /// #252 /// # impl Display for DataStoreError {253 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {254 /// # unimplemented!()255 /// # }256 /// # }257 /// #258 /// # impl std::error::Error for DataStoreError {}259 /// #260 /// # const REDACTED_CONTENT: () = ();261 /// #262 /// # let error = anyhow!("...");263 /// # let root_cause = &error;264 /// #265 /// # let ret =266 /// // If the error was caused by redaction, then return a tombstone instead267 /// // of the content.268 /// match root_cause.downcast_ref::<DataStoreError>() {269 /// Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),270 /// None => Err(error),271 /// }272 /// # ;273 /// ```

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 274–277why, one step back
274 pub fn downcast_ref<E>(&self) -> Option<&E>
275 where276 E: Display + Debug + Send + Sync + 'static,277 {

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 278why, one step back
278 if self.is::<E>() {

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 279why, one step back
279 unsafe { Some(&*(self.inner.error() as *const dyn StdError as *const E)) }

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 280–284why, one step back
280 } else {
281 None282 }283 }284

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

line 285why, one step back
285 /// Downcast this error object by mutable reference.

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 286–289why, one step back
286 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>
287 where288 E: Display + Debug + Send + Sync + 'static,289 {

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 290why, one step back
290 if self.is::<E>() {

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 291why, one step back
291 unsafe { Some(&mut *(self.inner.error_mut() as *mut dyn StdError as *mut E)) }

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 292–296why, one step back
292 } else {
293 None294 }295 }296}

Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f

line 297why, one step back
297

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 298–301why, one step back
298impl<E> From<E> for Error
299where300 E: StdError + Send + Sync + 'static,301{

Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337

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

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 303why, one step back
303 Error::new(error)

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 304–306why, one step back
304 }
305}306

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 307why, one step back
307impl Deref for Error {

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 308why, one step back
308 type Target = dyn StdError + Send + Sync + 'static;

Remove as_dyn_error methods from ErrorDavid Tolnaybefore adoption73c7283

line 309why, one step back
309

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 310–314why, one step back
310 fn deref(&self) -> &Self::Target {
311 self.inner.error()312 }313}314

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 315why, one step back
315impl DerefMut for Error {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 316–320why, one step back
316 fn deref_mut(&mut self) -> &mut Self::Target {
317 self.inner.error_mut()318 }319}320

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 321why, one step back
321impl Debug for Error {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 322–324why, one step back
322 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
323 writeln!(f, "{}", self.inner.error())?;324

Omit '0:' if only one causeDavid Tolnaybefore adoptionb0c8c75

line 325why, one step back
325 let mut chain = self.chain().skip(1).enumerate().peekable();

Touch up Debug impl for ErrorDavid Tolnaybefore adoption3cd7273

line 326why, one step back
326 if let Some((n, error)) = chain.next() {

Omit '0:' if only one causeDavid Tolnaybefore adoptionb0c8c75

lines 327–331why, one step back
327 write!(f, "\nCaused by:\n ")?;
328 if chain.peek().is_some() {329 write!(f, "{}: ", n)?;330 }331 writeln!(f, "{}", error)?;

Touch up Debug impl for ErrorDavid Tolnaybefore adoption3cd7273

line 332why, one step back
332 for (n, error) in chain {

Indent causes in Debug representation with spacesDavid Tolnaybefore adoptionad275d5

line 333why, one step back
333 writeln!(f, " {}: {}", n, error)?;

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 334–336why, one step back
334 }
335 }336

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

lines 337–338why, one step back
337 #[cfg(backtrace)]
338 {

Move backtrace capture to backtrace module and macroDavid Tolnaybefore adoptionebd9101

lines 339–340why, one step back
339 use std::backtrace::BacktraceStatus;
340

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

lines 341–348why, one step back
341 let backtrace = self.backtrace();
342 match backtrace.status() {343 BacktraceStatus::Captured => {344 writeln!(f, "\n{}", backtrace)?;345 }346 BacktraceStatus::Disabled => {347 writeln!(348 f,

Capitalize lines of Debug representationDavid Tolnaybefore adoptiona97f8dc

line 349why, one step back
349 "\nBacktrace disabled; run with RUST_LIB_BACKTRACE=1 environment variable to display a backtrace"

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

lines 350–352why, one step back
350 )?;
351 }352 _ => {}

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 353–359why, one step back
353 }
354 }355356 Ok(())357 }358}359

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 361–365why, one step back
361 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
362 write!(f, "{}", self.inner.error())363 }364}365

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

lines 366–367why, one step back
366unsafe impl Send for Error {}
367unsafe impl Sync for Error {}

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 368why, one step back
368

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 369why, one step back
369impl Drop for Error {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 370–374why, one step back
370 fn drop(&mut self) {
371 unsafe { ptr::drop_in_place(self.inner.error_mut()) }372 }373}374

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

line 375why, one step back
375struct ErrorVTable {

Rename source to object in vtableDavid Tolnaybefore adoption03ef684

lines 376–377why, one step back
376 object: unsafe fn(*const ()) -> *const (dyn StdError + Send + Sync + 'static),
377 object_mut: unsafe fn(*mut ()) -> *mut (dyn StdError + Send + Sync + 'static),

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

lines 378–379why, one step back
378}
379

Rename source to object in vtableDavid Tolnaybefore adoption03ef684

line 380why, one step back
380unsafe fn object_raw<E>(e: *const ()) -> *const (dyn StdError + Send + Sync + 'static)

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

lines 381–386why, one step back
381where
382 E: StdError + Send + Sync + 'static,383{384 e as *const E385}386

Rename source to object in vtableDavid Tolnaybefore adoption03ef684

line 387why, one step back
387unsafe fn object_mut_raw<E>(e: *mut ()) -> *mut (dyn StdError + Send + Sync + 'static)

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

lines 388–393why, one step back
388where
389 E: StdError + Send + Sync + 'static,390{391 e as *mut E392}393

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84

line 396why, one step back
396struct ErrorImpl<E> {

Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 398–403why, one step back
398 type_id: TypeId,
399 backtrace: Option<Backtrace>,400 error: E,401}402403#[repr(transparent)]

Remove trait bounds from MessageError structDavid Tolnaybefore adoptioneac05d1

line 404why, one step back
404struct MessageError<M>(M);

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 405why, one step back
405

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 406–409why, one step back
406impl<M> Debug for MessageError<M>
407where408 M: Display + Debug,409{

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 410–414why, one step back
410 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
411 Debug::fmt(&self.0, f)412 }413}414

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 415–418why, one step back
415impl<M> Display for MessageError<M>
416where417 M: Display + Debug,418{

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 419–423why, one step back
419 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
420 Display::fmt(&self.0, f)421 }422}423

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

line 424why, one step back
424impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 425why, one step back
425

Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84

line 426why, one step back
426impl ErrorImpl<()> {

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 427why, one step back
427 fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {

Rename source to object in vtableDavid Tolnaybefore adoption03ef684

line 428why, one step back
428 unsafe { &*(self.vtable.object)(&self.error) }

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 429–430why, one step back
429 }
430

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 431why, one step back
431 fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {

Rename source to object in vtableDavid Tolnaybefore adoption03ef684

line 432why, one step back
432 unsafe { &mut *(self.vtable.object_mut)(&mut self.error) }

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 433–435why, one step back
433 }
434}435

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

line 436why, one step back
436/// Iterator of a chain of source errors.

Add example to Chain iteratorDavid Tolnaybefore adoptionfeb9f33

lines 437–454why, one step back
437///
438/// This type is the iterator returned by [`Error::chain`].439///440/// # Example441///442/// ```443/// use anyhow::Error;444/// use std::io;445///446/// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {447/// for cause in error.chain() {448/// if let Some(io_error) = cause.downcast_ref::<io::Error>() {449/// return Some(io_error.kind());450/// }451/// }452/// None453/// }454/// ```

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

line 455why, one step back
455pub struct Chain<'a> {

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 456why, one step back
456 next: Option<&'a (dyn StdError + 'static)>,

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 457–458why, one step back
457}
458

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

line 459why, one step back
459impl<'a> Iterator for Chain<'a> {

Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190

line 460why, one step back
460 type Item = &'a (dyn StdError + 'static);

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 461–473why, one step back
461
462 fn next(&mut self) -> Option<Self::Item> {463 let next = self.next.take()?;464 self.next = next.source();465 Some(next)466 }467}468469#[cfg(test)]470mod repr_correctness {471 use super::*;472 use std::marker::Unpin;473 use std::mem;

Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39

lines 474–476why, one step back
474 use std::sync::atomic::AtomicBool;
475 use std::sync::atomic::Ordering::SeqCst;476 use std::sync::Arc;

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 477–478why, one step back
477
478 #[test]

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

line 479why, one step back
479 fn size_of_error() {

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 480why, one step back
480 assert_eq!(mem::size_of::<Error>(), mem::size_of::<usize>());

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 481–482why, one step back
481 }
482

Simplify autotraits testDavid Tolnaybefore adoptionb0e25c5

lines 483–486why, one step back
483 #[test]
484 fn error_autotraits() {485 fn assert<E: Unpin + Send + Sync + 'static>() {}486 assert::<Error>();

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 487–489why, one step back
487 }
488489 #[test]

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

line 490why, one step back
490 fn drop_works() {

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 491why, one step back
491 #[derive(Debug)]

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 492–498why, one step back
492 struct DetectDrop {
493 has_dropped: Arc<AtomicBool>,494 }495496 impl StdError for DetectDrop {}497498 impl Display for DetectDrop {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 499–502why, one step back
499 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
500 write!(f, "does something")501 }502 }

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 503–504why, one step back
503
504 impl Drop for DetectDrop {

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 505why, one step back
505 fn drop(&mut self) {

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 506–507why, one step back
506 let already_dropped = self.has_dropped.swap(true, SeqCst);
507 assert!(!already_dropped);

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39

line 511why, one step back
511 let has_dropped = Arc::new(AtomicBool::new(false));

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 512why, one step back
512

Change Error::from calls to Error::new to cut one level of noise from backtracesDavid Tolnaybefore adoption2c7d3ca

line 513why, one step back
513 drop(Error::new(DetectDrop {

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 514–515why, one step back
514 has_dropped: has_dropped.clone(),
515 }));

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 516why, one step back
516

Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39

line 517why, one step back
517 assert!(has_dropped.load(SeqCst));

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 518–519why, one step back
518 }
519}