Skip to content
Myr

error.rs

This file

Path

src/error.rs

511 lines · 13.9 KiB · Rust

Last changed by

Omit '0:' if only one causeDavid Tolnaybefore adoptionb0c8c75

History of this file

Take it

Add context method to ErrorDavid Tolnaybefore adoption41fde4c

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

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

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

lines 9–11why, one step back
9#[cfg(backtrace)]
10use std::backtrace::{Backtrace, BacktraceStatus};11

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 13why, one step back
13///

Touch up documentation on Error structDavid Tolnaybefore adoptiona8ce6e4

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

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 16why, one step back
16///

Touch up documentation on Error structDavid Tolnaybefore adoptiona8ce6e4

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

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 24–25why, one step back
24}
25

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 26why, one step back
26impl Error {

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 28why, one step back
28 ///

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 31why, one step back
31 ///

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

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

Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 35why, one step back
35 where

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

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 37why, one step back
37 {

Remove a layer of wrapping from debug mode backtracesDavid Tolnaybefore adoption97314ac

lines 38–45why, one step back
38 // Captured here instead of in Error::construct to have one fewer layer
39 // of wrapping visible in the backtrace.40 #[cfg(backtrace)]41 let backtrace = match error.backtrace() {42 Some(_) => None,43 None => Some(Backtrace::capture()),44 };45

Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f

lines 46–49why, one step back
46 #[cfg(not(backtrace))]
47 let backtrace = None;4849 Error::construct(error, TypeId::of::<E>(), backtrace)

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 50–51why, one step back
50 }
51

Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f

line 56why, one step back
56 Error::construct(MessageError(message), TypeId::of::<M>(), backtrace)

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 57–58why, one step back
57 }
58

Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f

line 59why, one step back
59 fn construct<E>(error: E, type_id: TypeId, backtrace: Option<Backtrace>) -> Self

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 60why, one step back
60 where

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

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 62–63why, one step back
62 {
63 unsafe {

Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742

lines 64–65why, one step back
64 let obj = mem::transmute::<&dyn StdError, TraitObject>(&error);
65 let inner = Box::new(ErrorImpl {

Remove a layer of wrapping from debug mode backtracesDavid Tolnaybefore adoption97314ac

line 66why, one step back
66 vtable: obj.vtable,

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 67–69why, one step back
67 type_id,
68 backtrace,69 error,

Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742

line 70why, one step back
70 });

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

line 71why, one step back
71 Error {

Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742

line 72why, one step back
72 inner: mem::transmute::<Box<ErrorImpl<E>>, Box<ErrorImpl<()>>>(inner),

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 73–74why, one step back
73 }
74 }

Add context method to ErrorDavid Tolnaybefore adoption41fde4c

lines 75–77why, one step back
75 }
7677 /// Wrap the error value with additional context.

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

lines 78–81why, one step back
78 ///
79 /// For attaching context to a `Result` as it is propagated, the80 /// [`Context`][crate::Context] extension trait may be more convenient than81 /// this function.

Explain the purpose of Error::contextDavid Tolnaybefore adoption896dd7a

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

Add context method to ErrorDavid Tolnaybefore adoption41fde4c

lines 130–137why, one step back
130 pub fn context<C>(self, context: C) -> Self
131 where132 C: Display + Send + Sync + 'static,133 {134 Error::from(ContextError {135 error: self,136 context,137 })

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 138–139why, one step back
138 }
139

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

lines 141–144why, one step back
141 ///
142 /// Backtraces are only available on the nightly channel. Tracking issue:143 /// [rust-lang/rust#53487][tracking].144 ///

Mention env var in Error::backtrace documentationDavid Tolnaybefore adoption4119806

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

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

lines 153–155why, one step back
153 // NB: this unwrap can only fail if the underlying error's backtrace
154 // method is nondeterministic, which would only happen in maliciously155 // constructed code

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 156–159why, one step back
156 self.inner
157 .backtrace158 .as_ref()159 .or_else(|| self.inner.error().backtrace())

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 161–162why, one step back
161 }
162

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 164why, one step back
164 ///

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

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

Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066

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

Add example of Error::chainDavid Tolnaybefore adoptiond159b79

lines 168–183why, one step back
168 ///
169 /// # Example170 ///171 /// ```172 /// use anyhow::Error;173 /// use std::io;174 ///175 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {176 /// for cause in error.chain() {177 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {178 /// return Some(io_error.kind());179 /// }180 /// }181 /// None182 /// }183 /// ```

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 186–189why, one step back
186 next: Some(self.inner.error()),
187 }188 }189

Add Error::root_cause accessorDavid Tolnaybefore adoption1ea72bb

lines 190–203why, one step back
190 /// The lowest level cause of this error &mdash; this error's cause's
191 /// cause's cause etc.192 ///193 /// The root cause is the last error in the iterator produced by194 /// [`chain()`][Error::chain].195 pub fn root_cause(&self) -> &(dyn StdError + 'static) {196 let mut chain = self.chain();197 let mut root_cause = chain.next().unwrap();198 for cause in chain {199 root_cause = cause;200 }201 root_cause202 }203

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

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

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 205–208why, one step back
205 pub fn is<E>(&self) -> bool
206 where207 E: Display + Debug + Send + Sync + 'static,208 {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 209–211why, one step back
209 TypeId::of::<E>() == self.inner.type_id
210 }211

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

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

Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337

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

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 214–216why, one step back
214 where
215 E: Display + Debug + Send + Sync + 'static,216 {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 217–228why, one step back
217 if let Some(error) = self.downcast_ref::<E>() {
218 unsafe {219 let error = ptr::read(error);220 drop(ptr::read(&self.inner));221 mem::forget(self);222 Ok(error)223 }224 } else {225 Err(self)226 }227 }228

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

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

Add example to Error::downcast_refDavid Tolnaybefore adoption0c21b7a

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

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 265–268why, one step back
265 pub fn downcast_ref<E>(&self) -> Option<&E>
266 where267 E: Display + Debug + Send + Sync + 'static,268 {

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

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

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 271–275why, one step back
271 } else {
272 None273 }274 }275

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

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

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 277–280why, one step back
277 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>
278 where279 E: Display + Debug + Send + Sync + 'static,280 {

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

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

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 283–287why, one step back
283 } else {
284 None285 }286 }287}

Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f

lines 288–290why, one step back
288
289#[cfg(not(backtrace))]290pub(crate) enum Backtrace {}

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 291why, one step back
291

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 292–295why, one step back
292impl<E> From<E> for Error
293where294 E: StdError + Send + Sync + 'static,295{

Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337

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

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 298–300why, one step back
298 }
299}300

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

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

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

Remove as_dyn_error methods from ErrorDavid Tolnaybefore adoption73c7283

line 303why, one step back
303

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 304–308why, one step back
304 fn deref(&self) -> &Self::Target {
305 self.inner.error()306 }307}308

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Omit '0:' if only one causeDavid Tolnaybefore adoptionb0c8c75

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

Touch up Debug impl for ErrorDavid Tolnaybefore adoption3cd7273

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

Omit '0:' if only one causeDavid Tolnaybefore adoptionb0c8c75

lines 321–325why, one step back
321 write!(f, "\nCaused by:\n ")?;
322 if chain.peek().is_some() {323 write!(f, "{}: ", n)?;324 }325 writeln!(f, "{}", error)?;

Touch up Debug impl for ErrorDavid Tolnaybefore adoption3cd7273

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

Indent causes in Debug representation with spacesDavid Tolnaybefore adoptionad275d5

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 328–330why, one step back
328 }
329 }330

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

lines 331–340why, one step back
331 #[cfg(backtrace)]
332 {333 let backtrace = self.backtrace();334 match backtrace.status() {335 BacktraceStatus::Captured => {336 writeln!(f, "\n{}", backtrace)?;337 }338 BacktraceStatus::Disabled => {339 writeln!(340 f,

Capitalize lines of Debug representationDavid Tolnaybefore adoptiona97f8dc

line 341why, one step back
341 "\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 342–344why, one step back
342 )?;
343 }344 _ => {}

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 345–351why, one step back
345 }
346 }347348 Ok(())349 }350}351

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 353–357why, one step back
353 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
354 write!(f, "{}", self.inner.error())355 }356}357

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

lines 358–359why, one step back
358unsafe impl Send for Error {}
359unsafe impl Sync for Error {}

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 360why, one step back
360

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 362–368why, one step back
362 fn drop(&mut self) {
363 unsafe { ptr::drop_in_place(self.inner.error_mut()) }364 }365}366367// repr C to ensure that `E` remains in the final position368#[repr(C)]

Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 370–383why, one step back
370 vtable: *const (),
371 type_id: TypeId,372 backtrace: Option<Backtrace>,373 error: E,374}375376// repr C to ensure that transmuting from trait objects is safe377#[repr(C)]378struct TraitObject {379 data: *const (),380 vtable: *const (),381}382383#[repr(transparent)]

Remove trait bounds from MessageError structDavid Tolnaybefore adoptioneac05d1

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 385why, one step back
385

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 386–389why, one step back
386impl<M> Debug for MessageError<M>
387where388 M: Display + Debug,389{

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 390–394why, one step back
390 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
391 Debug::fmt(&self.0, f)392 }393}394

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 395–398why, one step back
395impl<M> Display for MessageError<M>
396where397 M: Display + Debug,398{

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 399–403why, one step back
399 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
400 Display::fmt(&self.0, f)401 }402}403

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 405why, one step back
405

Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84

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

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

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

Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742

lines 408–413why, one step back
408 let object = TraitObject {
409 data: &self.error,410 vtable: self.vtable,411 };412413 unsafe { mem::transmute::<TraitObject, &(dyn StdError + Send + Sync + 'static)>(object) }

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 414–415why, one step back
414 }
415

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

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

Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742

lines 417–421why, one step back
417 let object = TraitObject {
418 data: &mut self.error,419 vtable: self.vtable,420 };421

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 422why, one step back
422 unsafe {

Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742

line 423why, one step back
423 mem::transmute::<TraitObject, &mut (dyn StdError + Send + Sync + 'static)>(object)

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 424–427why, one step back
424 }
425 }426}427

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

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

Add example to Chain iteratorDavid Tolnaybefore adoptionfeb9f33

lines 429–446why, one step back
429///
430/// This type is the iterator returned by [`Error::chain`].431///432/// # Example433///434/// ```435/// use anyhow::Error;436/// use std::io;437///438/// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {439/// for cause in error.chain() {440/// if let Some(io_error) = cause.downcast_ref::<io::Error>() {441/// return Some(io_error.kind());442/// }443/// }444/// None445/// }446/// ```

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

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

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

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 449–450why, one step back
449}
450

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

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

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

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 453–465why, one step back
453
454 fn next(&mut self) -> Option<Self::Item> {455 let next = self.next.take()?;456 self.next = next.source();457 Some(next)458 }459}460461#[cfg(test)]462mod repr_correctness {463 use super::*;464 use std::marker::Unpin;465 use std::mem;

Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39

lines 466–468why, one step back
466 use std::sync::atomic::AtomicBool;
467 use std::sync::atomic::Ordering::SeqCst;468 use std::sync::Arc;

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 469–470why, one step back
469
470 #[test]

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

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

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 473–474why, one step back
473 }
474

Simplify autotraits testDavid Tolnaybefore adoptionb0e25c5

lines 475–478why, one step back
475 #[test]
476 fn error_autotraits() {477 fn assert<E: Unpin + Send + Sync + 'static>() {}478 assert::<Error>();

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 479–481why, one step back
479 }
480481 #[test]

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 484–490why, one step back
484 struct DetectDrop {
485 has_dropped: Arc<AtomicBool>,486 }487488 impl StdError for DetectDrop {}489490 impl Display for DetectDrop {

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 491–494why, one step back
491 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
492 write!(f, "does something")493 }494 }

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 495–496why, one step back
495
496 impl Drop for DetectDrop {

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 500–502why, one step back
500 }
501 }502

Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 504why, one step back
504

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 505–507why, one step back
505 drop(Error::from(DetectDrop {
506 has_dropped: has_dropped.clone(),507 }));

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 508why, one step back
508

Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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