error.rs
This file
Path
src/error.rs
970 lines · 33.0 KiB · Rust
Last changed by
Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42
History of this fileTake it
The walk stopped after 200 commits: lines older than 87e85ab point at it.
Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 1why, one step back1use crate::backtrace::Backtrace;Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 2why, one step back2use crate::chain::Chain;Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 3–5why, one step back3#[cfg(any(feature = "std", anyhow_no_ptr_addr_of))]4use crate::ptr::Mut;5use crate::ptr::{Own, Ref};Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 6why, one step back6use crate::{Error, StdError};Merge pull request #178 from dtolnay/upDavid Tolnaybefore adoption3c586dd
line 7why, one step back7use alloc::boxed::Box;Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
lines 8–9why, one step back8use core::any::TypeId;9use core::fmt::{self, Debug, Display};Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 10why, one step back10use core::mem::ManuallyDrop;Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 11–12why, one step back11#[cfg(not(anyhow_no_ptr_addr_of))]12use core::ptr;Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 13why, one step back13use core::ptr::NonNull;Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 14why, one step back14Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
lines 15–17why, one step back15#[cfg(feature = "std")]16use core::ops::{Deref, DerefMut};17Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 18–25why, one step back18impl Error {19 /// Create a new error object from any error type.20 ///21 /// The error type must be threadsafe and `'static`, so that the `Error`22 /// will be as well.23 ///24 /// If the error type does not provide a backtrace, a backtrace will be25 /// created here to ensure that a backtrace exists.Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 26why, one step back26 #[cfg(feature = "std")]Merge pull request #78 from dtolnay/cfgDavid Tolnaybefore adoptione2eb1f5
line 27why, one step back27 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 28why, one step back28 #[cold]Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42
line 29why, one step back29 #[must_use]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 30–74why, one step back30 pub fn new<E>(error: E) -> Self31 where32 E: StdError + Send + Sync + 'static,33 {34 let backtrace = backtrace_if_absent!(error);35 Error::from_std(error, backtrace)36 }3738 /// Create a new error object from a printable error message.39 ///40 /// If the argument implements std::error::Error, prefer `Error::new`41 /// instead which preserves the underlying error's cause chain and42 /// backtrace. If the argument may or may not implement std::error::Error43 /// now or in the future, use `anyhow!(err)` which handles either way44 /// correctly.45 ///46 /// `Error::msg("...")` is equivalent to `anyhow!("...")` but occasionally47 /// convenient in places where a function is preferable over a macro, such48 /// as iterator or stream combinators:49 ///50 /// ```51 /// # mod ffi {52 /// # pub struct Input;53 /// # pub struct Output;54 /// # pub async fn do_some_work(_: Input) -> Result<Output, &'static str> {55 /// # unimplemented!()56 /// # }57 /// # }58 /// #59 /// # use ffi::{Input, Output};60 /// #61 /// use anyhow::{Error, Result};62 /// use futures::stream::{Stream, StreamExt, TryStreamExt};63 ///64 /// async fn demo<S>(stream: S) -> Result<Vec<Output>>65 /// where66 /// S: Stream<Item = Input>,67 /// {68 /// stream69 /// .then(ffi::do_some_work) // returns Result<Output, &str>70 /// .map_err(Error::msg)71 /// .try_collect()72 /// .await73 /// }74 /// ```Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 75why, one step back75 #[cold]Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42
line 76why, one step back76 #[must_use]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 77–83why, one step back77 pub fn msg<M>(message: M) -> Self78 where79 M: Display + Debug + Send + Sync + 'static,80 {81 Error::from_adhoc(message, backtrace!())82 }83Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 84why, one step back84 #[cfg(feature = "std")]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 85why, one step back85 #[cold]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 86–92why, one step back86 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self87 where88 E: StdError + Send + Sync + 'static,89 {90 let vtable = &ErrorVTable {91 object_drop: object_drop::<E>,92 object_ref: object_ref::<E>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 93why, one step back93 #[cfg(anyhow_no_ptr_addr_of)]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 94–96why, one step back94 object_mut: object_mut::<E>,95 object_boxed: object_boxed::<E>,96 object_downcast: object_downcast::<E>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 97why, one step back97 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 98why, one step back98 object_downcast_mut: object_downcast_mut::<E>,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 99why, one step back99 object_drop_rest: object_drop_front::<E>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 100–101why, one step back100 #[cfg(all(not(backtrace), feature = "backtrace"))]101 object_backtrace: no_backtrace,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 102–107why, one step back102 };103104 // Safety: passing vtable that operates on the right type E.105 unsafe { Error::construct(error, vtable, backtrace) }106 }107Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 108why, one step back108 #[cold]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 109–112why, one step back109 pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self110 where111 M: Display + Debug + Send + Sync + 'static,112 {Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 113why, one step back113 use crate::wrapper::MessageError;Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 114–117why, one step back114 let error: MessageError<M> = MessageError(message);115 let vtable = &ErrorVTable {116 object_drop: object_drop::<MessageError<M>>,117 object_ref: object_ref::<MessageError<M>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 118why, one step back118 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 119–121why, one step back119 object_mut: object_mut::<MessageError<M>>,120 object_boxed: object_boxed::<MessageError<M>>,121 object_downcast: object_downcast::<M>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 122why, one step back122 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 123why, one step back123 object_downcast_mut: object_downcast_mut::<M>,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 124why, one step back124 object_drop_rest: object_drop_front::<M>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 125–126why, one step back125 #[cfg(all(not(backtrace), feature = "backtrace"))]126 object_backtrace: no_backtrace,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 127–133why, one step back127 };128129 // Safety: MessageError is repr(transparent) so it is okay for the130 // vtable to allow casting the MessageError<M> to M.131 unsafe { Error::construct(error, vtable, backtrace) }132 }133Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 134why, one step back134 #[cold]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 135–138why, one step back135 pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self136 where137 M: Display + Send + Sync + 'static,138 {Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 139why, one step back139 use crate::wrapper::DisplayError;Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 140–143why, one step back140 let error: DisplayError<M> = DisplayError(message);141 let vtable = &ErrorVTable {142 object_drop: object_drop::<DisplayError<M>>,143 object_ref: object_ref::<DisplayError<M>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 144why, one step back144 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 145–147why, one step back145 object_mut: object_mut::<DisplayError<M>>,146 object_boxed: object_boxed::<DisplayError<M>>,147 object_downcast: object_downcast::<M>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 148why, one step back148 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 149why, one step back149 object_downcast_mut: object_downcast_mut::<M>,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 150why, one step back150 object_drop_rest: object_drop_front::<M>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 151–152why, one step back151 #[cfg(all(not(backtrace), feature = "backtrace"))]152 object_backtrace: no_backtrace,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 153–159why, one step back153 };154155 // Safety: DisplayError is repr(transparent) so it is okay for the156 // vtable to allow casting the DisplayError<M> to M.157 unsafe { Error::construct(error, vtable, backtrace) }158 }159Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 160why, one step back160 #[cfg(feature = "std")]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 161why, one step back161 #[cold]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 162–171why, one step back162 pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> Self163 where164 C: Display + Send + Sync + 'static,165 E: StdError + Send + Sync + 'static,166 {167 let error: ContextError<C, E> = ContextError { context, error };168169 let vtable = &ErrorVTable {170 object_drop: object_drop::<ContextError<C, E>>,171 object_ref: object_ref::<ContextError<C, E>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 172why, one step back172 #[cfg(anyhow_no_ptr_addr_of)]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 173–175why, one step back173 object_mut: object_mut::<ContextError<C, E>>,174 object_boxed: object_boxed::<ContextError<C, E>>,175 object_downcast: context_downcast::<C, E>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 176why, one step back176 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 177why, one step back177 object_downcast_mut: context_downcast_mut::<C, E>,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 178why, one step back178 object_drop_rest: context_drop_rest::<C, E>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 179–180why, one step back179 #[cfg(all(not(backtrace), feature = "backtrace"))]180 object_backtrace: no_backtrace,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 181–183why, one step back181 };182183 // Safety: passing vtable that operates on the right type.Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 184–186why, one step back184 unsafe { Error::construct(error, vtable, backtrace) }185 }186Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 187why, one step back187 #[cfg(feature = "std")]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 188why, one step back188 #[cold]Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 189–192why, one step back189 pub(crate) fn from_boxed(190 error: Box<dyn StdError + Send + Sync>,191 backtrace: Option<Backtrace>,192 ) -> Self {Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 193why, one step back193 use crate::wrapper::BoxedError;Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 194–197why, one step back194 let error = BoxedError(error);195 let vtable = &ErrorVTable {196 object_drop: object_drop::<BoxedError>,197 object_ref: object_ref::<BoxedError>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 198why, one step back198 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 199–201why, one step back199 object_mut: object_mut::<BoxedError>,200 object_boxed: object_boxed::<BoxedError>,201 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 202why, one step back202 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 203why, one step back203 object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
line 204why, one step back204 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 205–206why, one step back205 #[cfg(all(not(backtrace), feature = "backtrace"))]206 object_backtrace: no_backtrace,Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 207–210why, one step back207 };208209 // Safety: BoxedError is repr(transparent) so it is okay for the vtable210 // to allow casting to Box<dyn StdError + Send + Sync>.Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 211–218why, one step back211 unsafe { Error::construct(error, vtable, backtrace) }212 }213214 // Takes backtrace as argument rather than capturing it here so that the215 // user sees one fewer layer of wrapping noise in the backtrace.216 //217 // Unsafe because the given vtable must have sensible behavior on the error218 // value of type E.Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 219why, one step back219 #[cold]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 220–227why, one step back220 unsafe fn construct<E>(221 error: E,222 vtable: &'static ErrorVTable,223 backtrace: Option<Backtrace>,224 ) -> Self225 where226 E: StdError + Send + Sync + 'static,227 {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 228why, one step back228 let inner: Box<ErrorImpl<E>> = Box::new(ErrorImpl {Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 229–232why, one step back229 vtable,230 backtrace,231 _object: error,232 });Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 233why, one step back233 // Erase the concrete type of E from the compile-time type system. ThisFix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b
line 234why, one step back234 // is equivalent to the safe unsize coercion from Box<ErrorImpl<E>> toFill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 235–238why, one step back235 // Box<ErrorImpl<dyn StdError + Send + Sync + 'static>> except that the236 // result is a thin pointer. The necessary behavior for manipulating the237 // underlying ErrorImpl<E> is preserved in the vtable provided by the238 // caller rather than a builtin fat pointer vtable.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 239why, one step back239 let inner = Own::new(inner).cast::<ErrorImpl>();Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 240–296why, one step back240 Error { inner }241 }242243 /// Wrap the error value with additional context.244 ///245 /// For attaching context to a `Result` as it is propagated, the246 /// [`Context`][crate::Context] extension trait may be more convenient than247 /// this function.248 ///249 /// The primary reason to use `error.context(...)` instead of250 /// `result.context(...)` via the `Context` trait would be if the context251 /// needs to depend on some data held by the underlying error:252 ///253 /// ```254 /// # use std::fmt::{self, Debug, Display};255 /// #256 /// # type T = ();257 /// #258 /// # impl std::error::Error for ParseError {}259 /// # impl Debug for ParseError {260 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {261 /// # unimplemented!()262 /// # }263 /// # }264 /// # impl Display for ParseError {265 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {266 /// # unimplemented!()267 /// # }268 /// # }269 /// #270 /// use anyhow::Result;271 /// use std::fs::File;272 /// use std::path::Path;273 ///274 /// struct ParseError {275 /// line: usize,276 /// column: usize,277 /// }278 ///279 /// fn parse_impl(file: File) -> Result<T, ParseError> {280 /// # const IGNORE: &str = stringify! {281 /// ...282 /// # };283 /// # unimplemented!()284 /// }285 ///286 /// pub fn parse(path: impl AsRef<Path>) -> Result<T> {287 /// let file = File::open(&path)?;288 /// parse_impl(file).map_err(|error| {289 /// let context = format!(290 /// "only the first {} lines of {} are valid",291 /// error.line, path.as_ref().display(),292 /// );293 /// anyhow::Error::new(error).context(context)294 /// })295 /// }296 /// ```Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 297why, one step back297 #[cold]Merge pull request #229 from dtolnay/must_useDavid Tolnaybefore adoptionc8a3e42
line 298why, one step back298 #[must_use]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 299–310why, one step back299 pub fn context<C>(self, context: C) -> Self300 where301 C: Display + Send + Sync + 'static,302 {303 let error: ContextError<C, Error> = ContextError {304 context,305 error: self,306 };307308 let vtable = &ErrorVTable {309 object_drop: object_drop::<ContextError<C, Error>>,310 object_ref: object_ref::<ContextError<C, Error>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 311why, one step back311 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 312–314why, one step back312 object_mut: object_mut::<ContextError<C, Error>>,313 object_boxed: object_boxed::<ContextError<C, Error>>,314 object_downcast: context_chain_downcast::<C>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 315why, one step back315 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 316why, one step back316 object_downcast_mut: context_chain_downcast_mut::<C>,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 317why, one step back317 object_drop_rest: context_chain_drop_rest::<C>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 318–319why, one step back318 #[cfg(all(not(backtrace), feature = "backtrace"))]319 object_backtrace: context_backtrace::<C>,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 320–330why, one step back320 };321322 // As the cause is anyhow::Error, we already have a backtrace for it.323 let backtrace = None;324325 // Safety: passing vtable that operates on the right type.326 unsafe { Error::construct(error, vtable, backtrace) }327 }328329 /// Get the backtrace for this Error.330 ///Merge pull request #70 from dtolnay/envDavid Tolnaybefore adoption8d2acea
lines 331–333why, one step back331 /// In order for the backtrace to be meaningful, one of the two environment332 /// variables `RUST_LIB_BACKTRACE=1` or `RUST_BACKTRACE=1` must be defined333 /// and `RUST_LIB_BACKTRACE` must not be `0`. Backtraces are somewhatUse instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 334–335why, one step back334 /// expensive to capture in Rust, so we don't necessarily want to be335 /// capturing them all over the place all the time.Merge pull request #70 from dtolnay/envDavid Tolnaybefore adoption8d2acea
lines 336–342why, one step back336 ///337 /// - If you want panics and errors to both have backtraces, set338 /// `RUST_BACKTRACE=1`;339 /// - If you want only errors to have backtraces, set340 /// `RUST_LIB_BACKTRACE=1`;341 /// - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and342 /// `RUST_LIB_BACKTRACE=0`.Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 343why, one step back343 ///Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 344–357why, one step back344 /// # Stability345 ///346 /// Standard library backtraces are only available on the nightly channel.347 /// Tracking issue: [rust-lang/rust#53487][tracking].348 ///349 /// On stable compilers, this function is only available if the crate's350 /// "backtrace" feature is enabled, and will use the `backtrace` crate as351 /// the underlying backtrace implementation.352 ///353 /// ```toml354 /// [dependencies]355 /// anyhow = { version = "1.0", features = ["backtrace"] }356 /// ```357 ///Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 358why, one step back358 /// [tracking]: https://github.com/rust-lang/rust/issues/53487Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 359–361why, one step back359 #[cfg(any(backtrace, feature = "backtrace"))]360 #[cfg_attr(doc_cfg, doc(cfg(any(nightly, feature = "backtrace"))))]361 pub fn backtrace(&self) -> &impl_backtrace!() {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 362why, one step back362 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 363–385why, one step back363 }364365 /// An iterator of the chain of source errors contained by this Error.366 ///367 /// This iterator will visit every error in the cause chain of this error368 /// object, beginning with the error that this error object was created369 /// from.370 ///371 /// # Example372 ///373 /// ```374 /// use anyhow::Error;375 /// use std::io;376 ///377 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {378 /// for cause in error.chain() {379 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {380 /// return Some(io_error.kind());381 /// }382 /// }383 /// None384 /// }385 /// ```Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 386why, one step back386 #[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 387why, one step back387 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 388why, one step back388 #[cold]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 389why, one step back389 pub fn chain(&self) -> Chain {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 390why, one step back390 unsafe { ErrorImpl::chain(self.inner.by_ref()) }Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 391–397why, one step back391 }392393 /// The lowest level cause of this error — this error's cause's394 /// cause's cause etc.395 ///396 /// The root cause is the last error in the iterator produced by397 /// [`chain()`][Error::chain].Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 398why, one step back398 #[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 399why, one step back399 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 400why, one step back400 pub fn root_cause(&self) -> &(dyn StdError + 'static) {Merge pull request #108 from hbina/patch-1David Tolnaybefore adoption7954754
line 401why, one step back401 self.chain().last().unwrap()Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 402–419why, one step back402 }403404 /// Returns true if `E` is the type held by this error object.405 ///406 /// For errors with context, this method returns true if `E` matches the407 /// type of the context `C` **or** the type of the error on which the408 /// context has been attached. For details about the interaction between409 /// context and downcasting, [see here].410 ///411 /// [see here]: trait.Context.html#effect-on-downcasting412 pub fn is<E>(&self) -> bool413 where414 E: Display + Debug + Send + Sync + 'static,415 {416 self.downcast_ref::<E>().is_some()417 }418419 /// Attempt to downcast the error object to a concrete type.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 420why, one step back420 pub fn downcast<E>(mut self) -> Result<E, Self>Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 421–424why, one step back421 where422 E: Display + Debug + Send + Sync + 'static,423 {424 let target = TypeId::of::<E>();Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 425why, one step back425 let inner = self.inner.by_mut();Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 426why, one step back426 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 427–428why, one step back427 // Use vtable to find NonNull<()> which points to a value of type E428 // somewhere inside the data structure.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 429–438why, one step back429 #[cfg(not(anyhow_no_ptr_addr_of))]430 let addr = match (vtable(inner.ptr).object_downcast)(inner.by_ref(), target) {431 Some(addr) => addr.by_mut().extend(),432 None => return Err(self),433 };434 #[cfg(anyhow_no_ptr_addr_of)]435 let addr = match (vtable(inner.ptr).object_downcast_mut)(inner, target) {436 Some(addr) => addr.extend(),437 None => return Err(self),438 };Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 439–441why, one step back439440 // Prepare to read E out of the data structure. We'll drop the rest441 // of the data structure separately so that E is not dropped.Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 442why, one step back442 let outer = ManuallyDrop::new(self);Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 443–444why, one step back443444 // Read E from where the vtable found it.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 445why, one step back445 let error = addr.cast::<E>().read();Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 446–447why, one step back446447 // Drop rest of the data structure outside of E.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 448why, one step back448 (vtable(outer.inner.ptr).object_drop_rest)(outer.inner, target);Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 449why, one step back449Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 450–495why, one step back450 Ok(error)451 }452 }453454 /// Downcast this error object by reference.455 ///456 /// # Example457 ///458 /// ```459 /// # use anyhow::anyhow;460 /// # use std::fmt::{self, Display};461 /// # use std::task::Poll;462 /// #463 /// # #[derive(Debug)]464 /// # enum DataStoreError {465 /// # Censored(()),466 /// # }467 /// #468 /// # impl Display for DataStoreError {469 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {470 /// # unimplemented!()471 /// # }472 /// # }473 /// #474 /// # impl std::error::Error for DataStoreError {}475 /// #476 /// # const REDACTED_CONTENT: () = ();477 /// #478 /// # let error = anyhow!("...");479 /// # let root_cause = &error;480 /// #481 /// # let ret =482 /// // If the error was caused by redaction, then return a tombstone instead483 /// // of the content.484 /// match root_cause.downcast_ref::<DataStoreError>() {485 /// Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),486 /// None => Err(error),487 /// }488 /// # ;489 /// ```490 pub fn downcast_ref<E>(&self) -> Option<&E>491 where492 E: Display + Debug + Send + Sync + 'static,493 {494 let target = TypeId::of::<E>();495 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 496–497why, one step back496 // Use vtable to find NonNull<()> which points to a value of type E497 // somewhere inside the data structure.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 498–499why, one step back498 let addr = (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?;499 Some(addr.cast::<E>().deref())Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 500–509why, one step back500 }501 }502503 /// Downcast this error object by mutable reference.504 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>505 where506 E: Display + Debug + Send + Sync + 'static,507 {508 let target = TypeId::of::<E>();509 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 510–511why, one step back510 // Use vtable to find NonNull<()> which points to a value of type E511 // somewhere inside the data structure.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 512–517why, one step back512513 #[cfg(not(anyhow_no_ptr_addr_of))]514 let addr =515 (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?.by_mut();516517 #[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 518why, one step back518 let addr = (vtable(self.inner.ptr).object_downcast_mut)(self.inner.by_mut(), target)?;Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 519why, one step back519Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 520why, one step back520 Some(addr.cast::<E>().deref_mut())Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 521–524why, one step back521 }522 }523}524Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 525why, one step back525#[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 526why, one step back526#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 527–530why, one step back527impl<E> From<E> for Error528where529 E: StdError + Send + Sync + 'static,530{Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 531why, one step back531 #[cold]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 532–537why, one step back532 fn from(error: E) -> Self {533 let backtrace = backtrace_if_absent!(error);534 Error::from_std(error, backtrace)535 }536}537Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 538why, one step back538#[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 539why, one step back539#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 540–543why, one step back540impl Deref for Error {541 type Target = dyn StdError + Send + Sync + 'static;542543 fn deref(&self) -> &Self::Target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 544why, one step back544 unsafe { ErrorImpl::error(self.inner.by_ref()) }Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 545–547why, one step back545 }546}547Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 548why, one step back548#[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 549why, one step back549#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 550–551why, one step back550impl DerefMut for Error {551 fn deref_mut(&mut self) -> &mut Self::Target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 552why, one step back552 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 553–557why, one step back553 }554}555556impl Display for Error {557 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 558why, one step back558 unsafe { ErrorImpl::display(self.inner.by_ref(), formatter) }Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 559–563why, one step back559 }560}561562impl Debug for Error {563 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 564why, one step back564 unsafe { ErrorImpl::debug(self.inner.by_ref(), formatter) }Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 565–570why, one step back565 }566}567568impl Drop for Error {569 fn drop(&mut self) {570 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 571why, one step back571 // Invoke the vtable's drop behavior.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 572why, one step back572 (vtable(self.inner.ptr).object_drop)(self.inner);Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 573–577why, one step back573 }574 }575}576577struct ErrorVTable {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 578why, one step back578 object_drop: unsafe fn(Own<ErrorImpl>),Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 579–580why, one step back579 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,580 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 581–583why, one step back581 object_mut: unsafe fn(Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static),582 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,583 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 584why, one step back584 #[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 585–586why, one step back585 object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,586 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 587–588why, one step back587 #[cfg(all(not(backtrace), feature = "backtrace"))]588 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 589–590why, one step back589}590Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 591why, one step back591// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 592why, one step back592unsafe fn object_drop<E>(e: Own<ErrorImpl>) {Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 593–594why, one step back593 // Cast back to ErrorImpl<E> so that the allocator receives the correct594 // Layout to deallocate the Box's memory.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 595why, one step back595 let unerased = e.cast::<ErrorImpl<E>>().boxed();Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 596–598why, one step back596 drop(unerased);597}598Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 599why, one step back599// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 600why, one step back600unsafe fn object_drop_front<E>(e: Own<ErrorImpl>, target: TypeId) {Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 601–604why, one step back601 // Drop the fields of ErrorImpl other than E as well as the Box allocation,602 // without dropping E itself. This is used by downcast after doing a603 // ptr::read to take ownership of the E.604 let _ = target;Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 605why, one step back605 let unerased = e.cast::<ErrorImpl<ManuallyDrop<E>>>().boxed();Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 606–608why, one step back606 drop(unerased);607}608Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 609why, one step back609// Safety: requires layout of *e to match ErrorImpl<E>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 610why, one step back610unsafe fn object_ref<E>(e: Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 611–613why, one step back611where612 E: StdError + Send + Sync + 'static,613{Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 614why, one step back614 // Attach E's native StdError vtable onto a pointer to self._object.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 615–624why, one step back615616 let unerased = e.cast::<ErrorImpl<E>>();617618 #[cfg(not(anyhow_no_ptr_addr_of))]619 return Ref::from_raw(NonNull::new_unchecked(620 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,621 ));622623 #[cfg(anyhow_no_ptr_addr_of)]624 return Ref::new(&unerased.deref()._object);Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 625–626why, one step back625}626Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 627–628why, one step back627// Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived628// from a `&mut`Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 629why, one step back629#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 630why, one step back630unsafe fn object_mut<E>(e: Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static)Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 631–633why, one step back631where632 E: StdError + Send + Sync + 'static,633{Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 634why, one step back634 // Attach E's native StdError vtable onto a pointer to self._object.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 635why, one step back635 &mut e.cast::<ErrorImpl<E>>().deref_mut()._objectUse instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 636–637why, one step back636}637Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 638why, one step back638// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 639why, one step back639unsafe fn object_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 640–642why, one step back640where641 E: StdError + Send + Sync + 'static,642{Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 643why, one step back643 // Attach ErrorImpl<E>'s native StdError vtable. The StdError impl is below.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 644why, one step back644 e.cast::<ErrorImpl<E>>().boxed()Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 645–646why, one step back645}646Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 647why, one step back647// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 648why, one step back648unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 649–652why, one step back649where650 E: 'static,651{652 if TypeId::of::<E>() == target {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 653–654why, one step back653 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a654 // pointer to its E field.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 655–667why, one step back655656 let unerased = e.cast::<ErrorImpl<E>>();657658 #[cfg(not(anyhow_no_ptr_addr_of))]659 return Some(660 Ref::from_raw(NonNull::new_unchecked(661 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,662 ))663 .cast::<()>(),664 );665666 #[cfg(anyhow_no_ptr_addr_of)]667 return Some(Ref::new(&unerased.deref()._object).cast::<()>());Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 668–672why, one step back668 } else {669 None670 }671}672Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 673why, one step back673// Safety: requires layout of *e to match ErrorImpl<E>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 674why, one step back674#[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 675why, one step back675unsafe fn object_downcast_mut<E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 676–681why, one step back676where677 E: 'static,678{679 if TypeId::of::<E>() == target {680 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a681 // pointer to its E field.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 682–683why, one step back682 let unerased = e.cast::<ErrorImpl<E>>().deref_mut();683 Some(Mut::new(&mut unerased._object).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 684–688why, one step back684 } else {685 None686 }687}688Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 689–694why, one step back689#[cfg(all(not(backtrace), feature = "backtrace"))]690fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {691 let _ = e;692 None693}694Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 695why, one step back695// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 696why, one step back696#[cfg(feature = "std")]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 697why, one step back697unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 698–702why, one step back698where699 C: 'static,700 E: 'static,701{702 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 703–704why, one step back703 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();704 Some(Ref::new(&unerased._object.context).cast::<()>())Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 705why, one step back705 } else if TypeId::of::<E>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 706–707why, one step back706 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();707 Some(Ref::new(&unerased._object.error).cast::<()>())Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 708–712why, one step back708 } else {709 None710 }711}712Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 713why, one step back713// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 714why, one step back714#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 715why, one step back715unsafe fn context_downcast_mut<C, E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 716–719why, one step back716where717 C: 'static,718 E: 'static,719{Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 720why, one step back720 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 721–722why, one step back721 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();722 Some(Mut::new(&mut unerased._object.context).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 723why, one step back723 } else if TypeId::of::<E>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 724–725why, one step back724 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();725 Some(Mut::new(&mut unerased._object.error).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 726–732why, one step back726 } else {727 None728 }729}730731// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.732#[cfg(feature = "std")]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 733why, one step back733unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 734–737why, one step back734where735 C: 'static,736 E: 'static,737{Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 738–740why, one step back738 // Called after downcasting by value to either the C or the E and doing a739 // ptr::read to take ownership of that value.740 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 741–743why, one step back741 let unerased = e742 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, E>>>()743 .boxed();Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 744–745why, one step back744 drop(unerased);745 } else {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 746–748why, one step back746 let unerased = e747 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<E>>>>()748 .boxed();Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 749–752why, one step back749 drop(unerased);750 }751}752Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 753why, one step back753// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 754why, one step back754unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 755–757why, one step back755where756 C: 'static,757{Resolve branches_sharing_code clippy lintDavid Tolnaybefore adoptionaa6c83d
line 758why, one step back758 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 759why, one step back759 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 760why, one step back760 Some(Ref::new(&unerased._object.context).cast::<()>())Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 761why, one step back761 } else {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 762why, one step back762 // Recurse down the context chain per the inner error's vtable.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 763–764why, one step back763 let source = &unerased._object.error;764 (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target)Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 765–767why, one step back765 }766}767Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 768why, one step back768// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 769why, one step back769#[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 770why, one step back770unsafe fn context_chain_downcast_mut<C>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 771–773why, one step back771where772 C: 'static,773{Resolve branches_sharing_code clippy lintDavid Tolnaybefore adoptionaa6c83d
line 774why, one step back774 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 775why, one step back775 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 776why, one step back776 Some(Mut::new(&mut unerased._object.context).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 777–778why, one step back777 } else {778 // Recurse down the context chain per the inner error's vtable.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 779–780why, one step back779 let source = &mut unerased._object.error;780 (vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 781–784why, one step back781 }782}783784// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 785why, one step back785unsafe fn context_chain_drop_rest<C>(e: Own<ErrorImpl>, target: TypeId)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 786–788why, one step back786where787 C: 'static,788{Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 789–791why, one step back789 // Called after downcasting by value to either the C or one of the causes790 // and doing a ptr::read to take ownership of that value.791 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 792–794why, one step back792 let unerased = e793 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, Error>>>()794 .boxed();Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 795why, one step back795 // Drop the entire rest of the data structure rooted in the next Error.Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 796–797why, one step back796 drop(unerased);797 } else {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 798–801why, one step back798 let unerased = e799 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<Error>>>>()800 .boxed();801 // Read the Own<ErrorImpl> from the next error.Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 802why, one step back802 let inner = unerased._object.error.inner;Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 803why, one step back803 drop(unerased);Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 804why, one step back804 let vtable = vtable(inner.ptr);Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 805why, one step back805 // Recursively drop the next error using the same target typeid.Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 806why, one step back806 (vtable.object_drop_rest)(inner, target);Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 807–809why, one step back807 }808}809Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 810–821why, one step back810// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.811#[cfg(all(not(backtrace), feature = "backtrace"))]812#[allow(clippy::unnecessary_wraps)]813unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>814where815 C: 'static,816{817 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();818 let backtrace = ErrorImpl::backtrace(unerased._object.error.inner.by_ref());819 Some(backtrace)820}821Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 822–823why, one step back822// NOTE: If working with `ErrorImpl<()>`, references should be avoided in favor823// of raw pointers and `NonNull`.Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 824–825why, one step back824// repr C to ensure that E remains in the final position.825#[repr(C)]Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b
line 826why, one step back826pub(crate) struct ErrorImpl<E = ()> {Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 827–833why, one step back827 vtable: &'static ErrorVTable,828 backtrace: Option<Backtrace>,829 // NOTE: Don't use directly. Use only through vtable. Erased type may have830 // different alignment.831 _object: E,832}833Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 834–835why, one step back834// Reads the vtable out of `p`. This is the same as `p.as_ref().vtable`, but835// avoids converting `p` into a reference.Only need vtable function for a type erased ErrorImplDavid Tolnaybefore adoptiond1b0c9a
line 836why, one step back836unsafe fn vtable(p: NonNull<ErrorImpl>) -> &'static ErrorVTable {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 837why, one step back837 // NOTE: This assumes that `ErrorVTable` is the first field of ErrorImpl.Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd
line 838why, one step back838 *(p.as_ptr() as *const &'static ErrorVTable)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 839–840why, one step back839}840Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 841–849why, one step back841// repr C to ensure that ContextError<C, E> has the same layout as842// ContextError<ManuallyDrop<C>, E> and ContextError<C, ManuallyDrop<E>>.843#[repr(C)]844pub(crate) struct ContextError<C, E> {845 pub context: C,846 pub error: E,847}848849impl<E> ErrorImpl<E> {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 850why, one step back850 fn erase(&self) -> Ref<ErrorImpl> {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 851–852why, one step back851 // Erase the concrete type of E but preserve the vtable in self.vtable852 // for manipulating the resulting thin pointer. This is analogous to anFix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b
line 853why, one step back853 // unsize coercion.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 854why, one step back854 Ref::new(self).cast::<ErrorImpl>()Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 855–857why, one step back855 }856}857Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b
line 858why, one step back858impl ErrorImpl {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 859why, one step back859 pub(crate) unsafe fn error(this: Ref<Self>) -> &(dyn StdError + Send + Sync + 'static) {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 860–861why, one step back860 // Use vtable to attach E's native StdError vtable for the right861 // original type E.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 862why, one step back862 (vtable(this.ptr).object_ref)(this).deref()Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 863–864why, one step back863 }864Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 865why, one step back865 #[cfg(feature = "std")]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 866why, one step back866 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 867–868why, one step back867 // Use vtable to attach E's native StdError vtable for the right868 // original type E.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 869–876why, one step back869870 #[cfg(not(anyhow_no_ptr_addr_of))]871 return (vtable(this.ptr).object_ref)(this.by_ref())872 .by_mut()873 .deref_mut();874875 #[cfg(anyhow_no_ptr_addr_of)]876 return (vtable(this.ptr).object_mut)(this);Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 877–878why, one step back877 }878Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
line 879why, one step back879 #[cfg(any(backtrace, feature = "backtrace"))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 880why, one step back880 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 881–883why, one step back881 // This unwrap can only panic if the underlying error's backtrace method882 // is nondeterministic, which would only happen in maliciously883 // constructed code.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 884why, one step back884 this.deref()Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 885why, one step back885 .backtraceUse instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
line 886why, one step back886 .as_ref()Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 887–892why, one step back887 .or_else(|| {888 #[cfg(backtrace)]889 return Self::error(this).backtrace();890 #[cfg(all(not(backtrace), feature = "backtrace"))]891 return (vtable(this.ptr).object_backtrace)(this);892 })Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 893–895why, one step back893 .expect("backtrace capture failed")894 }895Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 896why, one step back896 #[cold]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 897why, one step back897 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 898why, one step back898 Chain::new(Self::error(this))Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 899–907why, one step back899 }900}901902impl<E> StdError for ErrorImpl<E>903where904 E: StdError,905{906 #[cfg(backtrace)]907 fn backtrace(&self) -> Option<&Backtrace> {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 908why, one step back908 Some(unsafe { ErrorImpl::backtrace(self.erase()) })Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 909–911why, one step back909 }910911 fn source(&self) -> Option<&(dyn StdError + 'static)> {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 912why, one step back912 unsafe { ErrorImpl::error(self.erase()).source() }Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 913–920why, one step back913 }914}915916impl<E> Debug for ErrorImpl<E>917where918 E: Debug,919{920 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 921why, one step back921 unsafe { ErrorImpl::debug(self.erase(), formatter) }Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 922–929why, one step back922 }923}924925impl<E> Display for ErrorImpl<E>926where927 E: Display,928{929 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 930why, one step back930 unsafe { Display::fmt(ErrorImpl::error(self.erase()), formatter) }Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 931–934why, one step back931 }932}933934impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {Merge pull request #166 from stepancheg/coldDavid Tolnaybefore adoption447eff8
line 935why, one step back935 #[cold]Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 936–938why, one step back936 fn from(error: Error) -> Self {937 let outer = ManuallyDrop::new(error);938 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 939–940why, one step back939 // Use vtable to attach ErrorImpl<E>'s native StdError vtable for940 // the right original type E.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 941why, one step back941 (vtable(outer.inner.ptr).object_boxed)(outer.inner)Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 942–945why, one step back942 }943 }944}945Merge pull request #104 from dtolnay/fromDavid Tolnaybefore adoption03a1916
lines 946–951why, one step back946impl From<Error> for Box<dyn StdError + Send + 'static> {947 fn from(error: Error) -> Self {948 Box::<dyn StdError + Send + Sync>::from(error)949 }950}951Use instrs.json as file path in examplesDavid Tolnaybefore adoption87e85ab
lines 952–956why, one step back952impl From<Error> for Box<dyn StdError + 'static> {953 fn from(error: Error) -> Self {954 Box::<dyn StdError + Send + Sync>::from(error)955 }956}Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164
line 957why, one step back957Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 958why, one step back958#[cfg(feature = "std")]Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164
lines 959–964why, one step back959impl AsRef<dyn StdError + Send + Sync> for Error {960 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {961 &**self962 }963}964Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 965why, one step back965#[cfg(feature = "std")]Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164
lines 966–970why, one step back966impl AsRef<dyn StdError> for Error {967 fn as_ref(&self) -> &(dyn StdError + 'static) {968 &**self969 }970}