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 fileTake it
Add context method to ErrorDavid Tolnaybefore adoption41fde4c
line 1why, one step back1use crate::context::ContextError;Rename exception module to errorDavid Tolnaybefore adoption2000930
line 2why, one step back2use std::any::TypeId;Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 3why, one step back3use std::error::Error as StdError;Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 4–8why, one step back4use std::fmt::{self, Debug, Display};5use std::mem;6use std::ops::{Deref, DerefMut};7use std::ptr;8Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e
lines 9–11why, one step back9#[cfg(backtrace)]10use std::backtrace::{Backtrace, BacktraceStatus};11Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 12why, one step back12/// The `Error` type, a wrapper around a dynamic error type.Rename exception module to errorDavid Tolnaybefore adoption2000930
line 13why, one step back13///Touch up documentation on Error structDavid Tolnaybefore adoptiona8ce6e4
line 14why, one step back14/// `Error` works a lot like `Box<dyn std::error::Error>`, but with theseWrap comments to 80 columnsDavid Tolnaybefore adoption9745066
line 15why, one step back15/// differences:Rename exception module to errorDavid Tolnaybefore adoption2000930
line 16why, one step back16///Touch up documentation on Error structDavid Tolnaybefore adoptiona8ce6e4
lines 17–21why, one step back17/// - `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 — exactly one word in21/// size instead of two.Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 22why, one step back22pub struct Error {Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84
line 23why, one step back23 inner: Box<ErrorImpl<()>>,Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 24–25why, one step back24}25Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 26why, one step back26impl Error {Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 27why, one step back27 /// Create a new error object from any error type.Rename exception module to errorDavid Tolnaybefore adoption2000930
line 28why, one step back28 ///Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066
lines 29–30why, one step back29 /// 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 back31 ///Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066
lines 32–33why, one step back32 /// If the error type does not provide a backtrace, a backtrace will be33 /// created here to ensure that a backtrace exists.Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337
line 34why, one step back34 pub fn new<E>(error: E) -> SelfRename exception module to errorDavid Tolnaybefore adoption2000930
line 35why, one step back35 whereImport std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 36why, one step back36 E: StdError + Send + Sync + 'static,Rename exception module to errorDavid Tolnaybefore adoption2000930
line 37why, one step back37 {Remove a layer of wrapping from debug mode backtracesDavid Tolnaybefore adoption97314ac
lines 38–45why, one step back38 // Captured here instead of in Error::construct to have one fewer layer39 // of wrapping visible in the backtrace.40 #[cfg(backtrace)]41 let backtrace = match error.backtrace() {42 Some(_) => None,43 None => Some(Backtrace::capture()),44 };45Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f
lines 46–49why, one step back46 #[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 back50 }51Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f
line 52why, one step back52 pub(crate) fn new_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> SelfRename exception module to errorDavid Tolnaybefore adoption2000930
lines 53–55why, one step back53 where54 M: Display + Debug + Send + Sync + 'static,55 {Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f
line 56why, one step back56 Error::construct(MessageError(message), TypeId::of::<M>(), backtrace)Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 57–58why, one step back57 }58Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f
line 59why, one step back59 fn construct<E>(error: E, type_id: TypeId, backtrace: Option<Backtrace>) -> SelfRename exception module to errorDavid Tolnaybefore adoption2000930
line 60why, one step back60 whereImport std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 61why, one step back61 E: StdError + Send + Sync + 'static,Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 62–63why, one step back62 {63 unsafe {Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742
lines 64–65why, one step back64 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 back66 vtable: obj.vtable,Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 67–69why, one step back67 type_id,68 backtrace,69 error,Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742
line 70why, one step back70 });Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 71why, one step back71 Error {Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742
line 72why, one step back72 inner: mem::transmute::<Box<ErrorImpl<E>>, Box<ErrorImpl<()>>>(inner),Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 73–74why, one step back73 }74 }Add context method to ErrorDavid Tolnaybefore adoption41fde4c
lines 75–77why, one step back75 }7677 /// Wrap the error value with additional context.Link to Context trait from Error::context methodDavid Tolnaybefore adoption61e587f
lines 78–81why, one step back78 ///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 back82 ///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 back130 pub fn context<C>(self, context: C) -> Self131 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 back138 }139Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 140why, one step back140 /// Get the backtrace for this Error.Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e
lines 141–144why, one step back141 ///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 back145 /// In order for the backtrace to be meaningful, the environment variable146 /// `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 back150 /// [tracking]: https://github.com/rust-lang/rust/issues/53487151 #[cfg(backtrace)]Rename exception module to errorDavid Tolnaybefore adoption2000930
line 152why, one step back152 pub fn backtrace(&self) -> &Backtrace {Wrap comments to 80 columnsDavid Tolnaybefore adoption9745066
lines 153–155why, one step back153 // NB: this unwrap can only fail if the underlying error's backtrace154 // method is nondeterministic, which would only happen in maliciously155 // constructed codeRename exception module to errorDavid Tolnaybefore adoption2000930
lines 156–159why, one step back156 self.inner157 .backtrace158 .as_ref()159 .or_else(|| self.inner.error().backtrace())Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 160why, one step back160 .expect("backtrace capture failed")Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 161–162why, one step back161 }162Rename Errors to ChainDavid Tolnaybefore adoptionf972193
line 163why, one step back163 /// An iterator of the chain of source errors contained by this Error.Rename exception module to errorDavid Tolnaybefore adoption2000930
line 164why, one step back164 ///Rename Errors to ChainDavid Tolnaybefore adoptionf972193
line 165why, one step back165 /// This iterator will visit every error in the cause chain of this errorWrap comments to 80 columnsDavid Tolnaybefore adoption9745066
lines 166–167why, one step back166 /// object, beginning with the error that this error object was created167 /// from.Add example of Error::chainDavid Tolnaybefore adoptiond159b79
lines 168–183why, one step back168 ///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 back184 pub fn chain(&self) -> Chain {185 Chain {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 186–189why, one step back186 next: Some(self.inner.error()),187 }188 }189Add Error::root_cause accessorDavid Tolnaybefore adoption1ea72bb
lines 190–203why, one step back190 /// The lowest level cause of this error — this error's cause's191 /// 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 }203Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 204why, one step back204 /// 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 back205 pub fn is<E>(&self) -> bool206 where207 E: Display + Debug + Send + Sync + 'static,208 {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 209–211why, one step back209 TypeId::of::<E>() == self.inner.type_id210 }211Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 212why, one step back212 /// Attempt to downcast the error object to a concrete type.Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337
line 213why, one step back213 pub fn downcast<E>(self) -> Result<E, Self>Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 214–216why, one step back214 where215 E: Display + Debug + Send + Sync + 'static,216 {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 217–228why, one step back217 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 }228Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 229why, one step back229 /// Downcast this error object by reference.Add example to Error::downcast_refDavid Tolnaybefore adoption0c21b7a
lines 230–264why, one step back230 ///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 back265 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 back269 if self.is::<E>() {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 270why, one step back270 unsafe { Some(&*(self.inner.error() as *const dyn StdError as *const E)) }Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 271–275why, one step back271 } else {272 None273 }274 }275Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 276why, one step back276 /// Downcast this error object by mutable reference.Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 277–280why, one step back277 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 back281 if self.is::<E>() {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 282why, one step back282 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 back283 } else {284 None285 }286 }287}Remove use of attributes on function parametersDavid Tolnaybefore adoptiona2d7e2f
lines 288–290why, one step back288289#[cfg(not(backtrace))]290pub(crate) enum Backtrace {}Rename exception module to errorDavid Tolnaybefore adoption2000930
line 291why, one step back291Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 292–295why, one step back292impl<E> From<E> for Error293where294 E: StdError + Send + Sync + 'static,295{Use Self in signatures of Error constructorsDavid Tolnaybefore adoption1ee4337
line 296why, one step back296 fn from(error: E) -> Self {Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 297why, one step back297 Error::new(error)Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 298–300why, one step back298 }299}300Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 301why, one step back301impl Deref for Error {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 302why, one step back302 type Target = dyn StdError + Send + Sync + 'static;Remove as_dyn_error methods from ErrorDavid Tolnaybefore adoption73c7283
line 303why, one step back303Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 304–308why, one step back304 fn deref(&self) -> &Self::Target {305 self.inner.error()306 }307}308Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 309why, one step back309impl DerefMut for Error {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 310–314why, one step back310 fn deref_mut(&mut self) -> &mut Self::Target {311 self.inner.error_mut()312 }313}314Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 315why, one step back315impl Debug for Error {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 316–318why, one step back316 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {317 writeln!(f, "{}", self.inner.error())?;318Omit '0:' if only one causeDavid Tolnaybefore adoptionb0c8c75
line 319why, one step back319 let mut chain = self.chain().skip(1).enumerate().peekable();Touch up Debug impl for ErrorDavid Tolnaybefore adoption3cd7273
line 320why, one step back320 if let Some((n, error)) = chain.next() {Omit '0:' if only one causeDavid Tolnaybefore adoptionb0c8c75
lines 321–325why, one step back321 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 back326 for (n, error) in chain {Indent causes in Debug representation with spacesDavid Tolnaybefore adoptionad275d5
line 327why, one step back327 writeln!(f, " {}: {}", n, error)?;Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 328–330why, one step back328 }329 }330Detect nightly and only expose backtrace if availableDavid Tolnaybefore adoption270ea5e
lines 331–340why, one step back331 #[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 back341 "\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 back342 )?;343 }344 _ => {}Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 345–351why, one step back345 }346 }347348 Ok(())349 }350}351Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 352why, one step back352impl Display for Error {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 353–357why, one step back353 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {354 write!(f, "{}", self.inner.error())355 }356}357Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
lines 358–359why, one step back358unsafe impl Send for Error {}359unsafe impl Sync for Error {}Rename exception module to errorDavid Tolnaybefore adoption2000930
line 360why, one step back360Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 361why, one step back361impl Drop for Error {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 362–368why, one step back362 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 back369struct ErrorImpl<E> {Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 370–383why, one step back370 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 back384struct MessageError<M>(M);Rename exception module to errorDavid Tolnaybefore adoption2000930
line 385why, one step back385Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 386–389why, one step back386impl<M> Debug for MessageError<M>387where388 M: Display + Debug,389{Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 390–394why, one step back390 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {391 Debug::fmt(&self.0, f)392 }393}394Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
lines 395–398why, one step back395impl<M> Display for MessageError<M>396where397 M: Display + Debug,398{Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 399–403why, one step back399 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {400 Display::fmt(&self.0, f)401 }402}403Move trait bounds to where-clausesDavid Tolnaybefore adoption7dcc851
line 404why, one step back404impl<M> StdError for MessageError<M> where M: Display + Debug + 'static {}Rename exception module to errorDavid Tolnaybefore adoption2000930
line 405why, one step back405Rename InnerException to ErrorImplDavid Tolnaybefore adoption68dcc84
line 406why, one step back406impl ErrorImpl<()> {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 407why, one step back407 fn error(&self) -> &(dyn StdError + Send + Sync + 'static) {Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742
lines 408–413why, one step back408 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 back414 }415Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 416why, one step back416 fn error_mut(&mut self) -> &mut (dyn StdError + Send + Sync + 'static) {Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742
lines 417–421why, one step back417 let object = TraitObject {418 data: &mut self.error,419 vtable: self.vtable,420 };421Rename exception module to errorDavid Tolnaybefore adoption2000930
line 422why, one step back422 unsafe {Specify generic params on every transmuteDavid Tolnaybefore adoption46ad742
line 423why, one step back423 mem::transmute::<TraitObject, &mut (dyn StdError + Send + Sync + 'static)>(object)Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 424–427why, one step back424 }425 }426}427Rename Errors to ChainDavid Tolnaybefore adoptionf972193
line 428why, one step back428/// Iterator of a chain of source errors.Add example to Chain iteratorDavid Tolnaybefore adoptionfeb9f33
lines 429–446why, one step back429///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 back447pub struct Chain<'a> {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 448why, one step back448 next: Option<&'a (dyn StdError + 'static)>,Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 449–450why, one step back449}450Rename Errors to ChainDavid Tolnaybefore adoptionf972193
line 451why, one step back451impl<'a> Iterator for Chain<'a> {Import std::error::Error as StdErrorDavid Tolnaybefore adoption7afc190
line 452why, one step back452 type Item = &'a (dyn StdError + 'static);Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 453–465why, one step back453454 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 back466 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 back469470 #[test]Change all remaining exception related wordingDavid Tolnaybefore adoption94d944c
line 471why, one step back471 fn size_of_error() {Rename Exception to ErrorDavid Tolnaybefore adoption5535ffc
line 472why, one step back472 assert_eq!(mem::size_of::<Error>(), mem::size_of::<usize>());Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 473–474why, one step back473 }474Simplify autotraits testDavid Tolnaybefore adoptionb0e25c5
lines 475–478why, one step back475 #[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 back479 }480481 #[test]Improve naming in Drop testDavid Tolnaybefore adoption04931ad
line 482why, one step back482 fn drop_works() {Rename exception module to errorDavid Tolnaybefore adoption2000930
line 483why, one step back483 #[derive(Debug)]Improve naming in Drop testDavid Tolnaybefore adoption04931ad
lines 484–490why, one step back484 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 back491 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 back495496 impl Drop for DetectDrop {Rename exception module to errorDavid Tolnaybefore adoption2000930
line 497why, one step back497 fn drop(&mut self) {Improve naming in Drop testDavid Tolnaybefore adoption04931ad
lines 498–499why, one step back498 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 back500 }501 }502Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39
line 503why, one step back503 let has_dropped = Arc::new(AtomicBool::new(false));Rename exception module to errorDavid Tolnaybefore adoption2000930
line 504why, one step back504Improve naming in Drop testDavid Tolnaybefore adoption04931ad
lines 505–507why, one step back505 drop(Error::from(DetectDrop {506 has_dropped: has_dropped.clone(),507 }));Rename exception module to errorDavid Tolnaybefore adoption2000930
line 508why, one step back508Replace Mutex<bool> with AtomicBoolDavid Tolnaybefore adoption794ba39
line 509why, one step back509 assert!(has_dropped.load(SeqCst));Rename exception module to errorDavid Tolnaybefore adoption2000930
lines 510–511why, one step back510 }511}