Skip to content
Myr

error.rs

This file

Path

src/error.rs

508 lines · 13.8 KiB · Rust

Last changed by

Capitalize lines of Debug representationDavid Tolnaybefore adoptiona97f8dc

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

Touch up Debug impl for ErrorDavid Tolnaybefore adoption3cd7273

lines 319–320why, one step back
319 let mut chain = self.chain().skip(1).enumerate();
320 if let Some((n, error)) = chain.next() {

Capitalize lines of Debug representationDavid Tolnaybefore adoptiona97f8dc

line 321why, one step back
321 writeln!(f, "\nCaused by:")?;

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Touch up Debug impl for ErrorDavid Tolnaybefore adoption3cd7273

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 324–327why, one step back
324 writeln!(f, "\t{}: {}", n, error)?;
325 }326 }327

Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e

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

Capitalize lines of Debug representationDavid Tolnaybefore adoptiona97f8dc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 342–348why, one step back
342 }
343 }344345 Ok(())346 }347}348

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

lines 355–356why, one step back
355unsafe impl Send for Error {}
356unsafe impl Sync for Error {}

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 357why, one step back
357

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 367–380why, one step back
367 vtable: *const (),
368 type_id: TypeId,369 backtrace: Option<Backtrace>,370 error: E,371}372373// repr C to ensure that transmuting from trait objects is safe374#[repr(C)]375struct TraitObject {376 data: *const (),377 vtable: *const (),378}379380#[repr(transparent)]

Remove trait bounds from MessageError structDavid Tolnaybefore adoptioneac05d1

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 382why, one step back
382

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 383–386why, one step back
383impl<M> Debug for MessageError<M>
384where385 M: Display + Debug,386{

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

lines 392–395why, one step back
392impl<M> Display for MessageError<M>
393where394 M: Display + Debug,395{

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 402why, one step back
402

Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84

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

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

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

Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 411–412why, one step back
411 }
412

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

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

Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 419why, one step back
419 unsafe {

Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 421–424why, one step back
421 }
422 }423}424

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

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

Add example to Chain iteratorDavid Tolnaybefore adoptionfeb9f33

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

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

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

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

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 446–447why, one step back
446}
447

Rename Errors to ChainDavid Tolnaybefore adoptionf972193

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

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

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39

lines 463–465why, one step back
463 use std::sync::atomic::AtomicBool;
464 use std::sync::atomic::Ordering::SeqCst;465 use std::sync::Arc;

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 466–467why, one step back
466
467 #[test]

Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c

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

Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 470–471why, one step back
470 }
471

Simplify autotraits testDavid Tolnaybefore adoptionb0e25c5

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 476–478why, one step back
476 }
477478 #[test]

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 481–487why, one step back
481 struct DetectDrop {
482 has_dropped: Arc<AtomicBool>,483 }484485 impl StdError for DetectDrop {}486487 impl Display for DetectDrop {

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 492–493why, one step back
492
493 impl Drop for DetectDrop {

Rename exception module to errorDavid Tolnaybefore adoption2000930

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

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 497–499why, one step back
497 }
498 }499

Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 501why, one step back
501

Improve naming in Drop testDavid Tolnaybefore adoption04931ad

lines 502–504why, one step back
502 drop(Error::from(DetectDrop {
503 has_dropped: has_dropped.clone(),504 }));

Rename exception module to errorDavid Tolnaybefore adoption2000930

line 505why, one step back
505

Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39

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

Rename exception module to errorDavid Tolnaybefore adoption2000930

lines 507–508why, one step back
507 }
508}