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 fileTake it
Move backtrace capture to backtrace module and macroDavid Tolnaybefore adoptionebd9101
line 1why, one step back1use crate::backtrace::Backtrace;Add context method to ErrorDavid Tolnaybefore adoption41fde4c
line 2why, one step back2use crate::context::ContextError;Rename exception module to errorDavid Tolnaybefore adoption2000930
line 3why, one step back3use std::any::TypeId;Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 4why, one step back4use std::error::Error as StdError;Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 5–9why, one step back5use std::fmt::{self, Debug, Display};6use std::mem;7use std::ops::{Deref, DerefMut};8use std::ptr;9Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 10why, one step back10/// The `Error` type, a wrapper around a dynamic error type.Rename exception module to errorDavid Tolnaybefore adoption2000930
line 11why, one step back11///Touch up documentation on Error structDavid Tolnaybefore adoptiona8ce6e4
line 12why, one step back12/// `Error` works a lot like `Box<dyn std::error::Error>`, but with theseWrap comments to 80 columnsDavid Tolnaybefore adoption9745066
line 13why, one step back13/// differences:Rename exception module to errorDavid Tolnaybefore adoption2000930
line 14why, one step back14///Touch up documentation on Error structDavid Tolnaybefore adoptiona8ce6e4
lines 15–19why, one step back15/// - `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 — exactly one word in19/// size instead of two.Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 20why, one step back20pub struct Error {Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84
line 21why, one step back21 inner: Box<ErrorImpl<()>>,Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 22–23why, one step back22}23Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 24why, one step back24impl Error {Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 25why, one step back25 /// Create a new error object from any error type.Rename exception module to errorDavid Tolnaybefore adoption2000930
line 26why, one step back26 ///Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066
lines 27–28why, one step back27 /// 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 back29 ///Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066
lines 30–31why, one step back30 /// If the error type does not provide a backtrace, a backtrace will be31 /// created here to ensure that a backtrace exists.Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337
line 32why, one step back32 pub fn new<E>(error: E) -> SelfRename exception module to errorDavid Tolnaybefore adoption2000930
line 33why, one step back33 whereImport std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 34why, one step back34 E: StdError + Send + Sync + 'static,Rename exception module to errorDavid Tolnaybefore adoption2000930
line 35why, one step back35 {Move Error::new backtrace capture to backtrace_if_absent macroDavid Tolnaybefore adoptioneaa819d
line 36why, one step back36 let backtrace = backtrace_if_absent!(error);Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9
lines 37–38why, one step back37 Error::from_std(error, backtrace)38 }Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298
line 39why, one step back39Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9
lines 40–45why, one step back40 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self41 where42 E: StdError + Send + Sync + 'static,43 {44 let type_id = TypeId::of::<E>();45Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298
line 46why, one step back46 // Safety: passing typeid of the right type E.Format with rustfmt 2019-09-08David Tolnaybefore adoption2a9e5b6
line 47why, one step back47 unsafe { Error::construct(error, type_id, backtrace) }Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 48–49why, one step back48 }49Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9
line 50why, one step back50 pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> SelfRename exception module to errorDavid Tolnaybefore adoption2000930
lines 51–53why, one step back51 where52 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 back54 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 back59 unsafe { Error::construct(error, type_id, backtrace) }Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 60–61why, one step back60 }61Move Error::new backtrace capture to backtrace_if_absent macroDavid Tolnaybefore adoptioneaa819d
lines 62–63why, one step back62 // Takes backtrace as argument rather than capturing it here so that the63 // 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 back64 //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>) -> SelfRename exception module to errorDavid Tolnaybefore adoption2000930
line 68why, one step back68 whereImport std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 69why, one step back69 E: StdError + Send + Sync + 'static,Rename exception module to errorDavid Tolnaybefore adoption2000930
line 70why, one step back70 {Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298
lines 71–82why, one step back71 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 back83 }Add context method to ErrorDavid Tolnaybefore adoption41fde4c
lines 84–86why, one step back84 }8586 /// Wrap the error value with additional context.Link to Context trait from Error::context methodDavid Tolnaybefore adoption61e587f
lines 87–90why, one step back87 ///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 back91 ///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 back139 pub fn context<C>(self, context: C) -> Self140 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 back143 Error::new(ContextError {Add context method to ErrorDavid Tolnaybefore adoption41fde4c
lines 144–146why, one step back144 error: self,145 context,146 })Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 147–148why, one step back147 }148Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 149why, one step back149 /// Get the backtrace for this Error.Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e
lines 150–153why, one step back150 ///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 back154 /// In order for the backtrace to be meaningful, the environment variable155 /// `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 back159 /// [tracking]: https://github.com/rust-lang/rust/issues/53487160 #[cfg(backtrace)]Rename exception module to errorDavid Tolnaybefore adoption2000930
line 161why, one step back161 pub fn backtrace(&self) -> &Backtrace {Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066
lines 162–164why, one step back162 // NB: this unwrap can only fail if the underlying error's backtrace163 // method is nondeterministic, which would only happen in maliciously164 // constructed codeRename exception module to errorDavid Tolnaybefore adoption2000930
lines 165–168why, one step back165 self.inner166 .backtrace167 .as_ref()168 .or_else(|| self.inner.error().backtrace())Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 169why, one step back169 .expect("backtrace capture failed")Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 170–171why, one step back170 }171Rename Errors to ChainDavid Tolnaybefore adoptionf972193
line 172why, one step back172 /// An iterator of the chain of source errors contained by this Error.Rename exception module to errorDavid Tolnaybefore adoption2000930
line 173why, one step back173 ///Rename Errors to ChainDavid Tolnaybefore adoptionf972193
line 174why, one step back174 /// This iterator will visit every error in the cause chain of this errorWrap comments to 80 columnsDavid Tolnaybefore adoption9745066
lines 175–176why, one step back175 /// object, beginning with the error that this error object was created176 /// from.Add example of Error::chainDavid Tolnaybefore adoptiond159b79
lines 177–192why, one step back177 ///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 back193 pub fn chain(&self) -> Chain {194 Chain {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 195–198why, one step back195 next: Some(self.inner.error()),196 }197 }198Add Error::root_cause accessorDavid Tolnaybefore adoption1ea72bb
lines 199–212why, one step back199 /// The lowest level cause of this error — this error's cause's200 /// 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 }212Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 213why, one step back213 /// 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 back214 pub fn is<E>(&self) -> bool215 where216 E: Display + Debug + Send + Sync + 'static,217 {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 218–220why, one step back218 TypeId::of::<E>() == self.inner.type_id219 }220Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 221why, one step back221 /// Attempt to downcast the error object to a concrete type.Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337
line 222why, one step back222 pub fn downcast<E>(self) -> Result<E, Self>Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 223–225why, one step back223 where224 E: Display + Debug + Send + Sync + 'static,225 {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 226–237why, one step back226 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 }237Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 238why, one step back238 /// Downcast this error object by reference.Add example to Error::downcast_refDavid Tolnaybefore adoption0c21b7a
lines 239–273why, one step back239 ///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 back274 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 back278 if self.is::<E>() {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 279why, one step back279 unsafe { Some(&*(self.inner.error() as *const dyn StdError as *const E)) }Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 280–284why, one step back280 } else {281 None282 }283 }284Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 285why, one step back285 /// Downcast this error object by mutable reference.Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 286–289why, one step back286 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 back290 if self.is::<E>() {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 291why, one step back291 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 back292 } else {293 None294 }295 }296}Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f
line 297why, one step back297Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 298–301why, one step back298impl<E> From<E> for Error299where300 E: StdError + Send + Sync + 'static,301{Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337
line 302why, one step back302 fn from(error: E) -> Self {Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 303why, one step back303 Error::new(error)Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 304–306why, one step back304 }305}306Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 307why, one step back307impl Deref for Error {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 308why, one step back308 type Target = dyn StdError + Send + Sync + 'static;Remove as_dyn_error methods from ErrorDavid Tolnaybefore adoption73c7283
line 309why, one step back309Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 310–314why, one step back310 fn deref(&self) -> &Self::Target {311 self.inner.error()312 }313}314Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 315why, one step back315impl DerefMut for Error {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 316–320why, one step back316 fn deref_mut(&mut self) -> &mut Self::Target {317 self.inner.error_mut()318 }319}320Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 321why, one step back321impl Debug for Error {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 322–324why, one step back322 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {323 writeln!(f, "{}", self.inner.error())?;324Omit '0:' if only one causeDavid Tolnaybefore adoptionb0c8c75
line 325why, one step back325 let mut chain = self.chain().skip(1).enumerate().peekable();Touch up Debug impl for ErrorDavid Tolnaybefore adoption3cd7273
line 326why, one step back326 if let Some((n, error)) = chain.next() {Omit '0:' if only one causeDavid Tolnaybefore adoptionb0c8c75
lines 327–331why, one step back327 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 back332 for (n, error) in chain {Indent causes in Debug representation with spacesDavid Tolnaybefore adoptionad275d5
line 333why, one step back333 writeln!(f, " {}: {}", n, error)?;Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 334–336why, one step back334 }335 }336Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e
lines 337–338why, one step back337 #[cfg(backtrace)]338 {Move backtrace capture to backtrace module and macroDavid Tolnaybefore adoptionebd9101
lines 339–340why, one step back339 use std::backtrace::BacktraceStatus;340Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e
lines 341–348why, one step back341 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 back349 "\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 back350 )?;351 }352 _ => {}Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 353–359why, one step back353 }354 }355356 Ok(())357 }358}359Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 360why, one step back360impl Display for Error {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 361–365why, one step back361 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {362 write!(f, "{}", self.inner.error())363 }364}365Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
lines 366–367why, one step back366unsafe impl Send for Error {}367unsafe impl Sync for Error {}Rename exception module to errorDavid Tolnaybefore adoption2000930
line 368why, one step back368Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 369why, one step back369impl Drop for Error {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 370–374why, one step back370 fn drop(&mut self) {371 unsafe { ptr::drop_in_place(self.inner.error_mut()) }372 }373}374Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a
line 375why, one step back375struct ErrorVTable {Rename source to object in vtableDavid Tolnaybefore adoption03ef684
lines 376–377why, one step back376 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 back378}379Rename source to object in vtableDavid Tolnaybefore adoption03ef684
line 380why, one step back380unsafe 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 back381where382 E: StdError + Send + Sync + 'static,383{384 e as *const E385}386Rename source to object in vtableDavid Tolnaybefore adoption03ef684
line 387why, one step back387unsafe 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 back388where389 E: StdError + Send + Sync + 'static,390{391 e as *mut E392}393Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 394–395why, one step back394// repr C to ensure that `E` remains in the final position395#[repr(C)]Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84
line 396why, one step back396struct ErrorImpl<E> {Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a
line 397why, one step back397 vtable: &'static ErrorVTable,Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 398–403why, one step back398 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 back404struct MessageError<M>(M);Rename exception module to errorDavid Tolnaybefore adoption2000930
line 405why, one step back405Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 406–409why, one step back406impl<M> Debug for MessageError<M>407where408 M: Display + Debug,409{Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 410–414why, one step back410 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {411 Debug::fmt(&self.0, f)412 }413}414Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 415–418why, one step back415impl<M> Display for MessageError<M>416where417 M: Display + Debug,418{Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 419–423why, one step back419 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {420 Display::fmt(&self.0, f)421 }422}423Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
line 424why, one step back424impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}Rename exception module to errorDavid Tolnaybefore adoption2000930
line 425why, one step back425Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84
line 426why, one step back426impl ErrorImpl<()> {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 427why, one step back427 fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {Rename source to object in vtableDavid Tolnaybefore adoption03ef684
line 428why, one step back428 unsafe { &*(self.vtable.object)(&self.error) }Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 429–430why, one step back429 }430Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 431why, one step back431 fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {Rename source to object in vtableDavid Tolnaybefore adoption03ef684
line 432why, one step back432 unsafe { &mut *(self.vtable.object_mut)(&mut self.error) }Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 433–435why, one step back433 }434}435Rename Errors to ChainDavid Tolnaybefore adoptionf972193
line 436why, one step back436/// Iterator of a chain of source errors.Add example to Chain iteratorDavid Tolnaybefore adoptionfeb9f33
lines 437–454why, one step back437///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 back455pub struct Chain<'a> {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 456why, one step back456 next: Option<&'a (dyn StdError + 'static)>,Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 457–458why, one step back457}458Rename Errors to ChainDavid Tolnaybefore adoptionf972193
line 459why, one step back459impl<'a> Iterator for Chain<'a> {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 460why, one step back460 type Item = &'a (dyn StdError + 'static);Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 461–473why, one step back461462 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 back474 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 back477478 #[test]Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 479why, one step back479 fn size_of_error() {Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 480why, one step back480 assert_eq!(mem::size_of::<Error>(), mem::size_of::<usize>());Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 481–482why, one step back481 }482Simplify autotraits testDavid Tolnaybefore adoptionb0e25c5
lines 483–486why, one step back483 #[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 back487 }488489 #[test]Improve naming in Drop testDavid Tolnaybefore adoption04931ad
line 490why, one step back490 fn drop_works() {Rename exception module to errorDavid Tolnaybefore adoption2000930
line 491why, one step back491 #[derive(Debug)]Improve naming in Drop testDavid Tolnaybefore adoption04931ad
lines 492–498why, one step back492 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 back499 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 back503504 impl Drop for DetectDrop {Rename exception module to errorDavid Tolnaybefore adoption2000930
line 505why, one step back505 fn drop(&mut self) {Improve naming in Drop testDavid Tolnaybefore adoption04931ad
lines 506–507why, one step back506 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 back508 }509 }510Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39
line 511why, one step back511 let has_dropped = Arc::new(AtomicBool::new(false));Rename exception module to errorDavid Tolnaybefore adoption2000930
line 512why, one step back512Change Error::from calls to Error::new to cut one level of noise from backtracesDavid Tolnaybefore adoption2c7d3ca
line 513why, one step back513 drop(Error::new(DetectDrop {Improve naming in Drop testDavid Tolnaybefore adoption04931ad
lines 514–515why, one step back514 has_dropped: has_dropped.clone(),515 }));Rename exception module to errorDavid Tolnaybefore adoption2000930
line 516why, one step back516Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39
line 517why, one step back517 assert!(has_dropped.load(SeqCst));Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 518–519why, one step back518 }519}