error.rs
This file
Path
src/error.rs
956 lines · 32.9 KiB · Rust
Last changed by
Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
History of this fileTake it
The walk stopped after 200 commits: lines older than dcb6b79 point at it.
Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 1why, one step back1use crate::alloc::Box;Move backtrace capture to backtrace module and macroDavid Tolnaybefore adoptionebd9101
line 2why, one step back2use crate::backtrace::Backtrace;Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 3why, one step back3use crate::chain::Chain;Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 4–6why, one step back4#[cfg(any(feature = "std", anyhow_no_ptr_addr_of))]5use crate::ptr::Mut;6use crate::ptr::{Own, Ref};Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
lines 7–9why, one step back7use crate::{Error, StdError};8use 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;Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
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};17Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
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 #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 28–31why, one step back28 pub fn new<E>(error: E) -> Self29 where30 E: StdError + Send + Sync + 'static,31 {Move Error::new backtrace capture to backtrace_if_absent macroDavid Tolnaybefore adoptioneaa819d
line 32why, one step back32 let backtrace = backtrace_if_absent!(error);Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9
lines 33–34why, one step back33 Error::from_std(error, backtrace)34 }Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298
line 35why, one step back35Add Error::msg constructor to simplify map_err combinatorsDavid Tolnaybefore adoption04b11c5
lines 36–79why, one step back36 /// Create a new error object from a printable error message.37 ///38 /// If the argument implements std::error::Error, prefer `Error::new`39 /// instead which preserves the underlying error's cause chain and40 /// backtrace. If the argument may or may not implement std::error::Error41 /// now or in the future, use `anyhow!(err)` which handles either way42 /// correctly.43 ///44 /// `Error::msg("...")` is equivalent to `anyhow!("...")` but occasionally45 /// convenient in places where a function is preferable over a macro, such46 /// as iterator or stream combinators:47 ///48 /// ```49 /// # mod ffi {50 /// # pub struct Input;51 /// # pub struct Output;52 /// # pub async fn do_some_work(_: Input) -> Result<Output, &'static str> {53 /// # unimplemented!()54 /// # }55 /// # }56 /// #57 /// # use ffi::{Input, Output};58 /// #59 /// use anyhow::{Error, Result};60 /// use futures::stream::{Stream, StreamExt, TryStreamExt};61 ///62 /// async fn demo<S>(stream: S) -> Result<Vec<Output>>63 /// where64 /// S: Stream<Item = Input>,65 /// {66 /// stream67 /// .then(ffi::do_some_work) // returns Result<Output, &str>68 /// .map_err(Error::msg)69 /// .try_collect()70 /// .await71 /// }72 /// ```73 pub fn msg<M>(message: M) -> Self74 where75 M: Display + Debug + Send + Sync + 'static,76 {77 Error::from_adhoc(message, backtrace!())78 }79Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 80why, one step back80 #[cfg(feature = "std")]Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9
lines 81–84why, one step back81 pub(crate) fn from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self82 where83 E: StdError + Send + Sync + 'static,84 {Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 85–87why, one step back85 let vtable = &ErrorVTable {86 object_drop: object_drop::<E>,87 object_ref: object_ref::<E>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 88why, one step back88 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 89–90why, one step back89 object_mut: object_mut::<E>,90 object_boxed: object_boxed::<E>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 91why, one step back91 object_downcast: object_downcast::<E>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 92why, one step back92 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 93why, one step back93 object_downcast_mut: object_downcast_mut::<E>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 94why, one step back94 object_drop_rest: object_drop_front::<E>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 95–96why, one step back95 #[cfg(all(not(backtrace), feature = "backtrace"))]96 object_backtrace: no_backtrace,Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
line 97why, one step back97 };Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9
line 98why, one step back98Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 99–100why, one step back99 // Safety: passing vtable that operates on the right type E.100 unsafe { Error::construct(error, vtable, backtrace) }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 101–102why, one step back101 }102Introduce safe from_std and matching from_adhocDavid Tolnaybefore adoption310dec9
line 103why, one step back103 pub(crate) fn from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> SelfMerge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 104–106why, one step back104 where105 M: Display + Debug + Send + Sync + 'static,106 {Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 107why, one step back107 use crate::wrapper::MessageError;Specify types where the manual vtable relies on themDavid Tolnaybefore adoptionb261a64
line 108why, one step back108 let error: MessageError<M> = MessageError(message);Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 109–111why, one step back109 let vtable = &ErrorVTable {110 object_drop: object_drop::<MessageError<M>>,111 object_ref: object_ref::<MessageError<M>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 112why, one step back112 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 113–114why, one step back113 object_mut: object_mut::<MessageError<M>>,114 object_boxed: object_boxed::<MessageError<M>>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 115why, one step back115 object_downcast: object_downcast::<M>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 116why, one step back116 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 117why, one step back117 object_downcast_mut: object_downcast_mut::<M>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 118why, one step back118 object_drop_rest: object_drop_front::<M>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 119–120why, one step back119 #[cfg(all(not(backtrace), feature = "backtrace"))]120 object_backtrace: no_backtrace,Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
line 121why, one step back121 };Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298
line 122why, one step back122Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 123–125why, one step back123 // Safety: MessageError is repr(transparent) so it is okay for the124 // vtable to allow casting the MessageError<M> to M.125 unsafe { Error::construct(error, vtable, backtrace) }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 126–127why, one step back126 }127Merge pull request #8 from dtolnay/optionDavid Tolnaybefore adoption240cc83
lines 128–131why, one step back128 pub(crate) fn from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self129 where130 M: Display + Send + Sync + 'static,131 {Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 132why, one step back132 use crate::wrapper::DisplayError;Specify types where the manual vtable relies on themDavid Tolnaybefore adoptionb261a64
line 133why, one step back133 let error: DisplayError<M> = DisplayError(message);Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 134–136why, one step back134 let vtable = &ErrorVTable {135 object_drop: object_drop::<DisplayError<M>>,136 object_ref: object_ref::<DisplayError<M>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 137why, one step back137 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 138–139why, one step back138 object_mut: object_mut::<DisplayError<M>>,139 object_boxed: object_boxed::<DisplayError<M>>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 140why, one step back140 object_downcast: object_downcast::<M>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 141why, one step back141 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 142why, one step back142 object_downcast_mut: object_downcast_mut::<M>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 143why, one step back143 object_drop_rest: object_drop_front::<M>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 144–145why, one step back144 #[cfg(all(not(backtrace), feature = "backtrace"))]145 object_backtrace: no_backtrace,Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
line 146why, one step back146 };Merge pull request #8 from dtolnay/optionDavid Tolnaybefore adoption240cc83
line 147why, one step back147Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 148–150why, one step back148 // Safety: DisplayError is repr(transparent) so it is okay for the149 // vtable to allow casting the DisplayError<M> to M.150 unsafe { Error::construct(error, vtable, backtrace) }Merge pull request #8 from dtolnay/optionDavid Tolnaybefore adoption240cc83
lines 151–152why, one step back151 }152Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 153why, one step back153 #[cfg(feature = "std")]Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6
line 154why, one step back154 pub(crate) fn from_context<C, E>(context: C, error: E, backtrace: Option<Backtrace>) -> SelfMove ContextError to error.rs in preparation for refactorDavid Tolnaybefore adoption30c2eab
lines 155–158why, one step back155 where156 C: Display + Send + Sync + 'static,157 E: StdError + Send + Sync + 'static,158 {Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6
lines 159–163why, one step back159 let error: ContextError<C, E> = ContextError { context, error };160161 let vtable = &ErrorVTable {162 object_drop: object_drop::<ContextError<C, E>>,163 object_ref: object_ref::<ContextError<C, E>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 164why, one step back164 #[cfg(anyhow_no_ptr_addr_of)]Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6
lines 165–166why, one step back165 object_mut: object_mut::<ContextError<C, E>>,166 object_boxed: object_boxed::<ContextError<C, E>>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 167why, one step back167 object_downcast: context_downcast::<C, E>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 168why, one step back168 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 169why, one step back169 object_downcast_mut: context_downcast_mut::<C, E>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 170why, one step back170 object_drop_rest: context_drop_rest::<C, E>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 171–172why, one step back171 #[cfg(all(not(backtrace), feature = "backtrace"))]172 object_backtrace: no_backtrace,Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6
lines 173–175why, one step back173 };174175 // Safety: passing vtable that operates on the right type.Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 176–178why, one step back176 unsafe { Error::construct(error, vtable, backtrace) }177 }178Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 179why, one step back179 #[cfg(feature = "std")]Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 180–183why, one step back180 pub(crate) fn from_boxed(181 error: Box<dyn StdError + Send + Sync>,182 backtrace: Option<Backtrace>,183 ) -> Self {Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 184why, one step back184 use crate::wrapper::BoxedError;Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 185–188why, one step back185 let error = BoxedError(error);186 let vtable = &ErrorVTable {187 object_drop: object_drop::<BoxedError>,188 object_ref: object_ref::<BoxedError>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 189why, one step back189 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 190–192why, one step back190 object_mut: object_mut::<BoxedError>,191 object_boxed: object_boxed::<BoxedError>,192 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 193why, one step back193 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 194why, one step back194 object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
line 195why, one step back195 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 196–197why, one step back196 #[cfg(all(not(backtrace), feature = "backtrace"))]197 object_backtrace: no_backtrace,Merge pull request #50 from dtolnay/boxedDavid Tolnaybefore adoption74fbea6
lines 198–201why, one step back198 };199200 // Safety: BoxedError is repr(transparent) so it is okay for the vtable201 // to allow casting to Box<dyn StdError + Send + Sync>.Expand ContextError vtable to prepare for extending downcast supportDavid Tolnaybefore adoption45e6fc6
line 202why, one step back202 unsafe { Error::construct(error, vtable, backtrace) }Move ContextError to error.rs in preparation for refactorDavid Tolnaybefore adoption30c2eab
lines 203–204why, one step back203 }204Move Error::new backtrace capture to backtrace_if_absent macroDavid Tolnaybefore adoptioneaa819d
lines 205–206why, one step back205 // Takes backtrace as argument rather than capturing it here so that the206 // user sees one fewer layer of wrapping noise in the backtrace.Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298
line 207why, one step back207 //Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 208–214why, one step back208 // Unsafe because the given vtable must have sensible behavior on the error209 // value of type E.210 unsafe fn construct<E>(211 error: E,212 vtable: &'static ErrorVTable,213 backtrace: Option<Backtrace>,214 ) -> SelfMerge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 215–217why, one step back215 where216 E: StdError + Send + Sync + 'static,217 {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 218why, one step back218 let inner: Box<ErrorImpl<E>> = Box::new(ErrorImpl {Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298
lines 219–220why, one step back219 vtable,220 backtrace,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 221why, one step back221 _object: error,Change Error::construct to unsafe fn because TypeId must be correctDavid Tolnaybefore adoptiondb2a298
line 222why, one step back222 });Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 223why, one step back223 // Erase the concrete type of E from the compile-time type system. ThisFix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b
line 224why, one step back224 // is equivalent to the safe unsize coercion from Box<ErrorImpl<E>> toFill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 225–228why, one step back225 // Box<ErrorImpl<dyn StdError + Send + Sync + 'static>> except that the226 // result is a thin pointer. The necessary behavior for manipulating the227 // underlying ErrorImpl<E> is preserved in the vtable provided by the228 // caller rather than a builtin fat pointer vtable.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 229why, one step back229 let inner = Own::new(inner).cast::<ErrorImpl>();Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f
line 230why, one step back230 Error { inner }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 231–259why, one step back231 }232233 /// Wrap the error value with additional context.234 ///235 /// For attaching context to a `Result` as it is propagated, the236 /// [`Context`][crate::Context] extension trait may be more convenient than237 /// this function.238 ///239 /// The primary reason to use `error.context(...)` instead of240 /// `result.context(...)` via the `Context` trait would be if the context241 /// needs to depend on some data held by the underlying error:242 ///243 /// ```244 /// # use std::fmt::{self, Debug, Display};245 /// #246 /// # type T = ();247 /// #248 /// # impl std::error::Error for ParseError {}249 /// # impl Debug for ParseError {250 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {251 /// # unimplemented!()252 /// # }253 /// # }254 /// # impl Display for ParseError {255 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {256 /// # unimplemented!()257 /// # }258 /// # }259 /// #Clean up Error::context example using anyhow ResultDavid Tolnaybefore adoptionf8457fb
line 260why, one step back260 /// use anyhow::Result;Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 261–275why, one step back261 /// use std::fs::File;262 /// use std::path::Path;263 ///264 /// struct ParseError {265 /// line: usize,266 /// column: usize,267 /// }268 ///269 /// fn parse_impl(file: File) -> Result<T, ParseError> {270 /// # const IGNORE: &str = stringify! {271 /// ...272 /// # };273 /// # unimplemented!()274 /// }275 ///Clean up Error::context example using anyhow ResultDavid Tolnaybefore adoptionf8457fb
line 276why, one step back276 /// pub fn parse(path: impl AsRef<Path>) -> Result<T> {Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 277–290why, one step back277 /// let file = File::open(&path)?;278 /// parse_impl(file).map_err(|error| {279 /// let context = format!(280 /// "only the first {} lines of {} are valid",281 /// error.line, path.as_ref().display(),282 /// );283 /// anyhow::Error::new(error).context(context)284 /// })285 /// }286 /// ```287 pub fn context<C>(self, context: C) -> Self288 where289 C: Display + Send + Sync + 'static,290 {Specify types where the manual vtable relies on themDavid Tolnaybefore adoptionb261a64
line 291why, one step back291 let error: ContextError<C, Error> = ContextError {Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
line 292why, one step back292 context,Swap ContextError type parameters to match type nameDavid Tolnaybefore adoption9f38495
line 293why, one step back293 error: self,Eliminate backtrace_if_absent in Error::contextDavid Tolnaybefore adoptiona2846fd
lines 294–298why, one step back294 };295296 let vtable = &ErrorVTable {297 object_drop: object_drop::<ContextError<C, Error>>,298 object_ref: object_ref::<ContextError<C, Error>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 299why, one step back299 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Eliminate backtrace_if_absent in Error::contextDavid Tolnaybefore adoptiona2846fd
lines 300–301why, one step back300 object_mut: object_mut::<ContextError<C, Error>>,301 object_boxed: object_boxed::<ContextError<C, Error>>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 302why, one step back302 object_downcast: context_chain_downcast::<C>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 303why, one step back303 #[cfg(anyhow_no_ptr_addr_of)]Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 304why, one step back304 object_downcast_mut: context_chain_downcast_mut::<C>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 305why, one step back305 object_drop_rest: context_chain_drop_rest::<C>,Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 306–307why, one step back306 #[cfg(all(not(backtrace), feature = "backtrace"))]307 object_backtrace: context_backtrace::<C>,Eliminate backtrace_if_absent in Error::contextDavid Tolnaybefore adoptiona2846fd
lines 308–314why, one step back308 };309310 // As the cause is anyhow::Error, we already have a backtrace for it.311 let backtrace = None;312313 // Safety: passing vtable that operates on the right type.314 unsafe { Error::construct(error, vtable, backtrace) }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 315–318why, one step back315 }316317 /// Get the backtrace for this Error.318 ///Merge pull request #70 from dtolnay/envDavid Tolnaybefore adoption8d2acea
lines 319–321why, one step back319 /// In order for the backtrace to be meaningful, one of the two environment320 /// variables `RUST_LIB_BACKTRACE=1` or `RUST_BACKTRACE=1` must be defined321 /// and `RUST_LIB_BACKTRACE` must not be `0`. Backtraces are somewhatMerge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 322–323why, one step back322 /// expensive to capture in Rust, so we don't necessarily want to be323 /// capturing them all over the place all the time.Merge pull request #70 from dtolnay/envDavid Tolnaybefore adoption8d2acea
lines 324–330why, one step back324 ///325 /// - If you want panics and errors to both have backtraces, set326 /// `RUST_BACKTRACE=1`;327 /// - If you want only errors to have backtraces, set328 /// `RUST_LIB_BACKTRACE=1`;329 /// - If you want only panics to have backtraces, set `RUST_BACKTRACE=1` and330 /// `RUST_LIB_BACKTRACE=0`.Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
line 331why, one step back331 ///Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 332–345why, one step back332 /// # Stability333 ///334 /// Standard library backtraces are only available on the nightly channel.335 /// Tracking issue: [rust-lang/rust#53487][tracking].336 ///337 /// On stable compilers, this function is only available if the crate's338 /// "backtrace" feature is enabled, and will use the `backtrace` crate as339 /// the underlying backtrace implementation.340 ///341 /// ```toml342 /// [dependencies]343 /// anyhow = { version = "1.0", features = ["backtrace"] }344 /// ```345 ///Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
line 346why, one step back346 /// [tracking]: https://github.com/rust-lang/rust/issues/53487Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 347–349why, one step back347 #[cfg(any(backtrace, feature = "backtrace"))]348 #[cfg_attr(doc_cfg, doc(cfg(any(nightly, feature = "backtrace"))))]349 pub fn backtrace(&self) -> &impl_backtrace!() {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 350why, one step back350 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 351–373why, one step back351 }352353 /// An iterator of the chain of source errors contained by this Error.354 ///355 /// This iterator will visit every error in the cause chain of this error356 /// object, beginning with the error that this error object was created357 /// from.358 ///359 /// # Example360 ///361 /// ```362 /// use anyhow::Error;363 /// use std::io;364 ///365 /// pub fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {366 /// for cause in error.chain() {367 /// if let Some(io_error) = cause.downcast_ref::<io::Error>() {368 /// return Some(io_error.kind());369 /// }370 /// }371 /// None372 /// }373 /// ```Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 374why, one step back374 #[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 375why, one step back375 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
line 376why, one step back376 pub fn chain(&self) -> Chain {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 377why, one step back377 unsafe { ErrorImpl::chain(self.inner.by_ref()) }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 378–384why, one step back378 }379380 /// The lowest level cause of this error — this error's cause's381 /// cause's cause etc.382 ///383 /// The root cause is the last error in the iterator produced by384 /// [`chain()`][Error::chain].Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 385why, one step back385 #[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 386why, one step back386 #[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
line 387why, one step back387 pub fn root_cause(&self) -> &(dyn StdError + 'static) {Merge pull request #108 from hbina/patch-1David Tolnaybefore adoption7954754
line 388why, one step back388 self.chain().last().unwrap()Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 389–390why, one step back389 }390Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 391–398why, one step back391 /// Returns true if `E` is the type held by this error object.392 ///393 /// For errors with context, this method returns true if `E` matches the394 /// type of the context `C` **or** the type of the error on which the395 /// context has been attached. For details about the interaction between396 /// context and downcasting, [see here].397 ///398 /// [see here]: trait.Context.html#effect-on-downcastingMerge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 399–402why, one step back399 pub fn is<E>(&self) -> bool400 where401 E: Display + Debug + Send + Sync + 'static,402 {Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 403why, one step back403 self.downcast_ref::<E>().is_some()Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 404–406why, one step back404 }405406 /// Attempt to downcast the error object to a concrete type.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 407why, one step back407 pub fn downcast<E>(mut self) -> Result<E, Self>Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 408–410why, one step back408 where409 E: Display + Debug + Send + Sync + 'static,410 {Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 411why, one step back411 let target = TypeId::of::<E>();Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 412why, one step back412 let inner = self.inner.by_mut();Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 413why, one step back413 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 414–415why, one step back414 // Use vtable to find NonNull<()> which points to a value of type E415 // somewhere inside the data structure.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 416–425why, one step back416 #[cfg(not(anyhow_no_ptr_addr_of))]417 let addr = match (vtable(inner.ptr).object_downcast)(inner.by_ref(), target) {418 Some(addr) => addr.by_mut().extend(),419 None => return Err(self),420 };421 #[cfg(anyhow_no_ptr_addr_of)]422 let addr = match (vtable(inner.ptr).object_downcast_mut)(inner, target) {423 Some(addr) => addr.extend(),424 None => return Err(self),425 };Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 426–428why, one step back426427 // Prepare to read E out of the data structure. We'll drop the rest428 // of the data structure separately so that E is not dropped.Deallocate with the right Layout during downcastDavid Tolnaybefore adoption22c2608
line 429why, one step back429 let outer = ManuallyDrop::new(self);Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 430–431why, one step back430431 // Read E from where the vtable found it.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 432why, one step back432 let error = addr.cast::<E>().read();Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 433–434why, one step back433434 // Drop rest of the data structure outside of E.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 435why, one step back435 (vtable(outer.inner.ptr).object_drop_rest)(outer.inner, target);Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 436why, one step back436Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 437why, one step back437 Ok(error)Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 438–480why, one step back438 }439 }440441 /// Downcast this error object by reference.442 ///443 /// # Example444 ///445 /// ```446 /// # use anyhow::anyhow;447 /// # use std::fmt::{self, Display};448 /// # use std::task::Poll;449 /// #450 /// # #[derive(Debug)]451 /// # enum DataStoreError {452 /// # Censored(()),453 /// # }454 /// #455 /// # impl Display for DataStoreError {456 /// # fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {457 /// # unimplemented!()458 /// # }459 /// # }460 /// #461 /// # impl std::error::Error for DataStoreError {}462 /// #463 /// # const REDACTED_CONTENT: () = ();464 /// #465 /// # let error = anyhow!("...");466 /// # let root_cause = &error;467 /// #468 /// # let ret =469 /// // If the error was caused by redaction, then return a tombstone instead470 /// // of the content.471 /// match root_cause.downcast_ref::<DataStoreError>() {472 /// Some(DataStoreError::Censored(_)) => Ok(Poll::Ready(REDACTED_CONTENT)),473 /// None => Err(error),474 /// }475 /// # ;476 /// ```477 pub fn downcast_ref<E>(&self) -> Option<&E>478 where479 E: Display + Debug + Send + Sync + 'static,480 {Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 481–482why, one step back481 let target = TypeId::of::<E>();482 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 483–484why, one step back483 // Use vtable to find NonNull<()> which points to a value of type E484 // somewhere inside the data structure.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 485–486why, one step back485 let addr = (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?;486 Some(addr.cast::<E>().deref())Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 487–494why, one step back487 }488 }489490 /// Downcast this error object by mutable reference.491 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>492 where493 E: Display + Debug + Send + Sync + 'static,494 {Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 495–496why, one step back495 let target = TypeId::of::<E>();496 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 497–498why, one step back497 // Use vtable to find NonNull<()> which points to a value of type E498 // somewhere inside the data structure.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 499–504why, one step back499500 #[cfg(not(anyhow_no_ptr_addr_of))]501 let addr =502 (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?.by_mut();503504 #[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 505why, one step back505 let addr = (vtable(self.inner.ptr).object_downcast_mut)(self.inner.by_mut(), target)?;Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 506why, one step back506Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 507why, one step back507 Some(addr.cast::<E>().deref_mut())Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 508–511why, one step back508 }509 }510}511Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 512why, one step back512#[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 513why, one step back513#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 514–518why, one step back514impl<E> From<E> for Error515where516 E: StdError + Send + Sync + 'static,517{518 fn from(error: E) -> Self {Capture backtraces earlierDavid Tolnaybefore adoptionc06155e
lines 519–520why, one step back519 let backtrace = backtrace_if_absent!(error);520 Error::from_std(error, backtrace)Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 521–523why, one step back521 }522}523Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 524why, one step back524#[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 525why, one step back525#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 526–529why, one step back526impl Deref for Error {527 type Target = dyn StdError + Send + Sync + 'static;528529 fn deref(&self) -> &Self::Target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 530why, one step back530 unsafe { ErrorImpl::error(self.inner.by_ref()) }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 531–533why, one step back531 }532}533Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 534why, one step back534#[cfg(feature = "std")]Merge pull request #128 from dtolnay/doc-cfgDavid Tolnaybefore adoption0c0e41f
line 535why, one step back535#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 536–537why, one step back536impl DerefMut for Error {537 fn deref_mut(&mut self) -> &mut Self::Target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 538why, one step back538 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 539–541why, one step back539 }540}541Merge pull request #42 from dtolnay/fmtDavid Tolnaybefore adoption74954bd
line 542why, one step back542impl Display for Error {Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
line 543why, one step back543 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 544why, one step back544 unsafe { ErrorImpl::display(self.inner.by_ref(), formatter) }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 545–547why, one step back545 }546}547Merge pull request #42 from dtolnay/fmtDavid Tolnaybefore adoption74954bd
lines 548–549why, one step back548impl Debug for Error {549 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 550why, one step back550 unsafe { ErrorImpl::debug(self.inner.by_ref(), formatter) }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 551–555why, one step back551 }552}553554impl Drop for Error {555 fn drop(&mut self) {Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f
line 556why, one step back556 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 557why, one step back557 // Invoke the vtable's drop behavior.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 558why, one step back558 (vtable(self.inner.ptr).object_drop)(self.inner);Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f
line 559why, one step back559 }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 560–562why, one step back560 }561}562Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a
line 563why, one step back563struct ErrorVTable {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 564why, one step back564 object_drop: unsafe fn(Own<ErrorImpl>),Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 565–566why, one step back565 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,566 #[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 567–569why, one step back567 object_mut: unsafe fn(Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static),568 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,569 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 570why, one step back570 #[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 571–572why, one step back571 object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,572 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 573–574why, one step back573 #[cfg(all(not(backtrace), feature = "backtrace"))]574 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f
lines 575–576why, one step back575}576Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 577why, one step back577// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 578why, one step back578unsafe fn object_drop<E>(e: Own<ErrorImpl>) {Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f
lines 579–580why, one step back579 // Cast back to ErrorImpl<E> so that the allocator receives the correct580 // Layout to deallocate the Box's memory.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 581why, one step back581 let unerased = e.cast::<ErrorImpl<E>>().boxed();Deallocate with the right Layout during downcastDavid Tolnaybefore adoption22c2608
lines 582–584why, one step back582 drop(unerased);583}584Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 585why, one step back585// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 586why, one step back586unsafe fn object_drop_front<E>(e: Own<ErrorImpl>, target: TypeId) {Deallocate with the right Layout during downcastDavid Tolnaybefore adoption22c2608
lines 587–589why, one step back587 // Drop the fields of ErrorImpl other than E as well as the Box allocation,588 // without dropping E itself. This is used by downcast after doing a589 // ptr::read to take ownership of the E.Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 590why, one step back590 let _ = target;Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 591why, one step back591 let unerased = e.cast::<ErrorImpl<ManuallyDrop<E>>>().boxed();Deallocate ErrorImpl with the right LayoutDavid Tolnaybefore adoption6b25b2f
line 592why, one step back592 drop(unerased);Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a
lines 593–594why, one step back593}594Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 595why, one step back595// Safety: requires layout of *e to match ErrorImpl<E>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 596why, one step back596unsafe fn object_ref<E>(e: Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a
lines 597–599why, one step back597where598 E: StdError + Send + Sync + 'static,599{Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 600why, one step back600 // Attach E's native StdError vtable onto a pointer to self._object.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 601–610why, one step back601602 let unerased = e.cast::<ErrorImpl<E>>();603604 #[cfg(not(anyhow_no_ptr_addr_of))]605 return Ref::from_raw(NonNull::new_unchecked(606 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,607 ));608609 #[cfg(anyhow_no_ptr_addr_of)]610 return Ref::new(&unerased.deref()._object);Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a
lines 611–612why, one step back611}612Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 613–614why, one step back613// Safety: requires layout of *e to match ErrorImpl<E>, and for `e` to be derived614// from a `&mut`Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 615why, one step back615#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 616why, one step back616unsafe fn object_mut<E>(e: Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static)Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a
lines 617–619why, one step back617where618 E: StdError + Send + Sync + 'static,619{Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 620why, one step back620 // Attach E's native StdError vtable onto a pointer to self._object.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 621why, one step back621 &mut e.cast::<ErrorImpl<E>>().deref_mut()._objectMerge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a
lines 622–623why, one step back622}623Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 624why, one step back624// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 625why, one step back625unsafe fn object_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
lines 626–628why, one step back626where627 E: StdError + Send + Sync + 'static,628{Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 629why, one step back629 // Attach ErrorImpl<E>'s native StdError vtable. The StdError impl is below.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 630why, one step back630 e.cast::<ErrorImpl<E>>().boxed()Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
lines 631–632why, one step back631}632Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 633why, one step back633// Safety: requires layout of *e to match ErrorImpl<E>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 634why, one step back634unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
line 635why, one step back635whereMerge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 636why, one step back636 E: 'static,Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
line 637why, one step back637{Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 638why, one step back638 if TypeId::of::<E>() == target {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 639–640why, one step back639 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a640 // pointer to its E field.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 641–653why, one step back641642 let unerased = e.cast::<ErrorImpl<E>>();643644 #[cfg(not(anyhow_no_ptr_addr_of))]645 return Some(646 Ref::from_raw(NonNull::new_unchecked(647 ptr::addr_of!((*unerased.as_ptr())._object) as *mut E,648 ))649 .cast::<()>(),650 );651652 #[cfg(anyhow_no_ptr_addr_of)]653 return Some(Ref::new(&unerased.deref()._object).cast::<()>());Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 654–656why, one step back654 } else {655 None656 }Merge pull request #33 from dtolnay/typeidDavid Tolnaybefore adoption8885e93
lines 657–658why, one step back657}658Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 659why, one step back659// Safety: requires layout of *e to match ErrorImpl<E>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 660why, one step back660#[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 661why, one step back661unsafe fn object_downcast_mut<E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 662–667why, one step back662where663 E: 'static,664{665 if TypeId::of::<E>() == target {666 // Caller is looking for an E pointer and e is ErrorImpl<E>, take a667 // pointer to its E field.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 668–669why, one step back668 let unerased = e.cast::<ErrorImpl<E>>().deref_mut();669 Some(Mut::new(&mut unerased._object).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 670–674why, one step back670 } else {671 None672 }673}674Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 675–680why, one step back675#[cfg(all(not(backtrace), feature = "backtrace"))]676fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {677 let _ = e;678 None679}680Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 681why, one step back681// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 682why, one step back682#[cfg(feature = "std")]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 683why, one step back683unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 684–688why, one step back684where685 C: 'static,686 E: 'static,687{688 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 689–690why, one step back689 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();690 Some(Ref::new(&unerased._object.context).cast::<()>())Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 691why, one step back691 } else if TypeId::of::<E>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 692–693why, one step back692 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref();693 Some(Ref::new(&unerased._object.error).cast::<()>())Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 694–698why, one step back694 } else {695 None696 }697}698Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 699why, one step back699// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 700why, one step back700#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 701why, one step back701unsafe fn context_downcast_mut<C, E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 702–705why, one step back702where703 C: 'static,704 E: 'static,705{Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 706why, one step back706 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 707–708why, one step back707 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();708 Some(Mut::new(&mut unerased._object.context).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 709why, one step back709 } else if TypeId::of::<E>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 710–711why, one step back710 let unerased = e.cast::<ErrorImpl<ContextError<C, E>>>().deref_mut();711 Some(Mut::new(&mut unerased._object.error).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 712–718why, one step back712 } else {713 None714 }715}716717// Safety: requires layout of *e to match ErrorImpl<ContextError<C, E>>.718#[cfg(feature = "std")]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 719why, one step back719unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 720–723why, one step back720where721 C: 'static,722 E: 'static,723{Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 724–726why, one step back724 // Called after downcasting by value to either the C or the E and doing a725 // ptr::read to take ownership of that value.726 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 727–729why, one step back727 let unerased = e728 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, E>>>()729 .boxed();Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 730–731why, one step back730 drop(unerased);731 } else {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 732–734why, one step back732 let unerased = e733 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<E>>>>()734 .boxed();Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 735–738why, one step back735 drop(unerased);736 }737}738Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 739why, one step back739// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 740why, one step back740unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 741–744why, one step back741where742 C: 'static,743{744 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 745–746why, one step back745 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();746 Some(Ref::new(&unerased._object.context).cast::<()>())Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 747why, one step back747 } else {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 748why, one step back748 // Recurse down the context chain per the inner error's vtable.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 749–751why, one step back749 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();750 let source = &unerased._object.error;751 (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target)Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 752–754why, one step back752 }753}754Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 755why, one step back755// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 756why, one step back756#[cfg(anyhow_no_ptr_addr_of)]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 757why, one step back757unsafe fn context_chain_downcast_mut<C>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 758–760why, one step back758where759 C: 'static,760{Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 761why, one step back761 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 762–763why, one step back762 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();763 Some(Mut::new(&mut unerased._object.context).cast::<()>())Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 764–765why, one step back764 } else {765 // Recurse down the context chain per the inner error's vtable.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 766–768why, one step back766 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref_mut();767 let source = &mut unerased._object.error;768 (vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 769–772why, one step back769 }770}771772// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 773why, one step back773unsafe fn context_chain_drop_rest<C>(e: Own<ErrorImpl>, target: TypeId)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 774–776why, one step back774where775 C: 'static,776{Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 777–779why, one step back777 // Called after downcasting by value to either the C or one of the causes778 // and doing a ptr::read to take ownership of that value.779 if TypeId::of::<C>() == target {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 780–782why, one step back780 let unerased = e781 .cast::<ErrorImpl<ContextError<ManuallyDrop<C>, Error>>>()782 .boxed();Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 783why, one step back783 // Drop the entire rest of the data structure rooted in the next Error.Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 784–785why, one step back784 drop(unerased);785 } else {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
lines 786–789why, one step back786 let unerased = e787 .cast::<ErrorImpl<ContextError<C, ManuallyDrop<Error>>>>()788 .boxed();789 // Read the Own<ErrorImpl> from the next error.Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 790why, one step back790 let inner = unerased._object.error.inner;Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 791why, one step back791 drop(unerased);Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 792why, one step back792 let vtable = vtable(inner.ptr);Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
line 793why, one step back793 // Recursively drop the next error using the same target typeid.Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 794why, one step back794 (vtable.object_drop_rest)(inner, target);Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 795–797why, one step back795 }796}797Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 798–809why, one step back798// Safety: requires layout of *e to match ErrorImpl<ContextError<C, Error>>.799#[cfg(all(not(backtrace), feature = "backtrace"))]800#[allow(clippy::unnecessary_wraps)]801unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>802where803 C: 'static,804{805 let unerased = e.cast::<ErrorImpl<ContextError<C, Error>>>().deref();806 let backtrace = ErrorImpl::backtrace(unerased._object.error.inner.by_ref());807 Some(backtrace)808}809Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 810–811why, one step back810// NOTE: If working with `ErrorImpl<()>`, references should be avoided in favor811// of raw pointers and `NonNull`.Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
line 812why, one step back812// repr C to ensure that E remains in the final position.Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
line 813why, one step back813#[repr(C)]Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b
line 814why, one step back814pub(crate) struct ErrorImpl<E = ()> {Merge pull request #5 from johnschug/masterDavid Tolnaybefore adoption8220e0a
line 815why, one step back815 vtable: &'static ErrorVTable,Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
line 816why, one step back816 backtrace: Option<Backtrace>,Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 817–819why, one step back817 // NOTE: Don't use directly. Use only through vtable. Erased type may have818 // different alignment.819 _object: E,Move ContextError to error.rs in preparation for refactorDavid Tolnaybefore adoption30c2eab
lines 820–821why, one step back820}821Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 822–823why, one step back822// Reads the vtable out of `p`. This is the same as `p.as_ref().vtable`, but823// avoids converting `p` into a reference.Only need vtable function for a type erased ErrorImplDavid Tolnaybefore adoptiond1b0c9a
line 824why, one step back824unsafe fn vtable(p: NonNull<ErrorImpl>) -> &'static ErrorVTable {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 825why, one step back825 // NOTE: This assumes that `ErrorVTable` is the first field of ErrorImpl.Restore compatibility with rustc pre-1.38David Tolnaybefore adoptionf3fe8bd
line 826why, one step back826 *(p.as_ptr() as *const &'static ErrorVTable)Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
lines 827–828why, one step back827}828Merge pull request #34 from dtolnay/contextDavid Tolnaybefore adoption8fcd8d0
lines 829–831why, one step back829// repr C to ensure that ContextError<C, E> has the same layout as830// ContextError<ManuallyDrop<C>, E> and ContextError<C, ManuallyDrop<E>>.831#[repr(C)]Move ContextError to error.rs in preparation for refactorDavid Tolnaybefore adoption30c2eab
lines 832–834why, one step back832pub(crate) struct ContextError<C, E> {833 pub context: C,834 pub error: E,Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 835–836why, one step back835}836Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452
line 837why, one step back837impl<E> ErrorImpl<E> {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 838why, one step back838 fn erase(&self) -> Ref<ErrorImpl> {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 839–840why, one step back839 // Erase the concrete type of E but preserve the vtable in self.vtable840 // for manipulating the resulting thin pointer. This is analogous to anFix typo coersion -> coercionDavid Tolnaybefore adoptionfd0268b
line 841why, one step back841 // unsize coercion.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 842why, one step back842 Ref::new(self).cast::<ErrorImpl>()Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452
lines 843–845why, one step back843 }844}845Elide ErrorImpl type parameter if erasedDavid Tolnaybefore adoptionbd5e39b
line 846why, one step back846impl ErrorImpl {Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 847why, one step back847 pub(crate) unsafe fn error(this: Ref<Self>) -> &(dyn StdError + Send + Sync + 'static) {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 848–849why, one step back848 // Use vtable to attach E's native StdError vtable for the right849 // original type E.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
line 850why, one step back850 (vtable(this.ptr).object_ref)(this).deref()Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 851–852why, one step back851 }852Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 853why, one step back853 #[cfg(feature = "std")]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 854why, one step back854 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 855–856why, one step back855 // Use vtable to attach E's native StdError vtable for the right856 // original type E.Merge pull request #145 from dtolnay/addrofDavid Tolnaybefore adoption64ac0c0
lines 857–864why, one step back857858 #[cfg(not(anyhow_no_ptr_addr_of))]859 return (vtable(this.ptr).object_ref)(this.by_ref())860 .by_mut()861 .deref_mut();862863 #[cfg(anyhow_no_ptr_addr_of)]864 return (vtable(this.ptr).object_mut)(this);Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
lines 865–866why, one step back865 }866Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
line 867why, one step back867 #[cfg(any(backtrace, feature = "backtrace"))]Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 868why, one step back868 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
lines 869–871why, one step back869 // This unwrap can only panic if the underlying error's backtrace method870 // is nondeterministic, which would only happen in maliciously871 // constructed code.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 872why, one step back872 this.deref()Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 873why, one step back873 .backtraceMerge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
line 874why, one step back874 .as_ref()Merge pull request #143 from dtolnay/backtraceDavid Tolnaybefore adoptiond65af36
lines 875–880why, one step back875 .or_else(|| {876 #[cfg(backtrace)]877 return Self::error(this).backtrace();878 #[cfg(all(not(backtrace), feature = "backtrace"))]879 return (vtable(this.ptr).object_backtrace)(this);880 })Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
lines 881–883why, one step back881 .expect("backtrace capture failed")882 }883Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 884why, one step back884 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 885why, one step back885 Chain::new(Self::error(this))Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
lines 886–888why, one step back886 }887}888Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452
lines 889–894why, one step back889impl<E> StdError for ErrorImpl<E>890where891 E: StdError,892{893 #[cfg(backtrace)]894 fn backtrace(&self) -> Option<&Backtrace> {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 895why, one step back895 Some(unsafe { ErrorImpl::backtrace(self.erase()) })Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452
line 896why, one step back896 }Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
line 897why, one step back897Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452
line 898why, one step back898 fn source(&self) -> Option<&(dyn StdError + 'static)> {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 899why, one step back899 unsafe { ErrorImpl::error(self.erase()).source() }Fill in StdError methods for ErrorImplDavid Tolnaybefore adoptionabd2452
lines 900–902why, one step back900 }901}902Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
lines 903–907why, one step back903impl<E> Debug for ErrorImpl<E>904where905 E: Debug,906{907 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 908why, one step back908 unsafe { ErrorImpl::debug(self.erase(), formatter) }Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
lines 909–916why, one step back909 }910}911912impl<E> Display for ErrorImpl<E>913where914 E: Display,915{916 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {Merge pull request 134 from thomcc/mirimiriDavid Tolnaybefore adoptionae372df
line 917why, one step back917 unsafe { Display::fmt(ErrorImpl::error(self.erase()), formatter) }Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
lines 918–924why, one step back918 }919}920921impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {922 fn from(error: Error) -> Self {923 let outer = ManuallyDrop::new(error);924 unsafe {Fill in some explanations of unsafe codeDavid Tolnaybefore adoption6088b60
lines 925–926why, one step back925 // Use vtable to attach ErrorImpl<E>'s native StdError vtable for926 // the right original type E.Adopt some NonNull wrappersDavid Tolnaybefore adoption62673e2
line 927why, one step back927 (vtable(outer.inner.ptr).object_boxed)(outer.inner)Merge pull request #12 from dtolnay/boxedDavid Tolnaybefore adoptionbc446d8
line 928why, one step back928 }Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 929–931why, one step back929 }930}931Merge pull request #104 from dtolnay/fromDavid Tolnaybefore adoption03a1916
lines 932–937why, one step back932impl From<Error> for Box<dyn StdError + Send + 'static> {933 fn from(error: Error) -> Self {934 Box::<dyn StdError + Send + Sync>::from(error)935 }936}937Merge pull request #28 from dtolnay/fromDavid Tolnaybefore adoption68225d2
lines 938–940why, one step back938impl From<Error> for Box<dyn StdError + 'static> {939 fn from(error: Error) -> Self {940 Box::<dyn StdError + Send + Sync>::from(error)Merge pull request #3 from dtolnay/literalDavid Tolnaybefore adoptiondcb6b79
lines 941–942why, one step back941 }942}Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164
line 943why, one step back943Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 944why, one step back944#[cfg(feature = "std")]Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164
lines 945–950why, one step back945impl AsRef<dyn StdError + Send + Sync> for Error {946 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {947 &**self948 }949}950Merge pull request #53 from dtolnay/nostdDavid Tolnaybefore adoption55343b9
line 951why, one step back951#[cfg(feature = "std")]Add AsRef impls for ErrorDavid Tolnaybefore adoption2c88164
lines 952–956why, one step back952impl AsRef<dyn StdError> for Error {953 fn as_ref(&self) -> &(dyn StdError + 'static) {954 &**self955 }956}