Skip to content
Myr

Merge pull request #415 from dtolnay/intodyn

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Add 2 different conversions to `Box<dyn Error + Send + Sync + 'static>`

authored by David Tolnay · committed by GitHub · parents 29f2edd, e1a2017 · Browse files · 1 file+122 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+122 added−8 removed

  • other:+122 added−8 removed

1 file+122 added−8 removed

modified:src/error.rs+122 added-8 removed
157 unmodified lines
158 #[cfg(anyhow_no_ptr_addr_of)]159 object_mut: object_mut::<E>,160 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]161 object_boxed: object_boxed::<E>,162 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]163 object_reallocate_boxed: object_reallocate_boxed::<E>,164 object_downcast: object_downcast::<E>,165 #[cfg(anyhow_no_ptr_addr_of)]166 object_downcast_mut: object_downcast_mut::<E>,167 object_drop_rest: object_drop_front::<E>,
21 unmodified lines
189 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]190 object_mut: object_mut::<MessageError<M>>,191 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]192 object_boxed: object_boxed::<MessageError<M>>,193 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]194 object_reallocate_boxed: object_reallocate_boxed::<MessageError<M>>,195 object_downcast: object_downcast::<M>,196 #[cfg(anyhow_no_ptr_addr_of)]197 object_downcast_mut: object_downcast_mut::<M>,198 object_drop_rest: object_drop_front::<M>,
22 unmodified lines
221 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]222 object_mut: object_mut::<DisplayError<M>>,223 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]224 object_boxed: object_boxed::<DisplayError<M>>,225 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]226 object_reallocate_boxed: object_reallocate_boxed::<DisplayError<M>>,227 object_downcast: object_downcast::<M>,228 #[cfg(anyhow_no_ptr_addr_of)]229 object_downcast_mut: object_downcast_mut::<M>,230 object_drop_rest: object_drop_front::<M>,
28 unmodified lines
259 #[cfg(anyhow_no_ptr_addr_of)]260 object_mut: object_mut::<ContextError<C, E>>,261 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]262 object_boxed: object_boxed::<ContextError<C, E>>,263 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]264 object_reallocate_boxed: object_reallocate_boxed::<ContextError<C, E>>,265 object_downcast: context_downcast::<C, E>,266 #[cfg(anyhow_no_ptr_addr_of)]267 object_downcast_mut: context_downcast_mut::<C, E>,268 object_drop_rest: context_drop_rest::<C, E>,
22 unmodified lines
291 #[cfg(anyhow_no_ptr_addr_of)]292 object_mut: object_mut::<BoxedError>,293 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]294 object_boxed: object_boxed::<BoxedError>,295 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]296 object_reallocate_boxed: object_reallocate_boxed::<BoxedError>,297 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,298 #[cfg(anyhow_no_ptr_addr_of)]299 object_downcast_mut: object_downcast_mut::<Box<dyn StdError + Send + Sync>>,300 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,
109 unmodified lines
410 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]411 object_mut: object_mut::<ContextError<C, Error>>,412 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]413 object_boxed: object_boxed::<ContextError<C, Error>>,414 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]415 object_reallocate_boxed: object_reallocate_boxed::<ContextError<C, Error>>,416 object_downcast: context_chain_downcast::<C>,417 #[cfg(anyhow_no_ptr_addr_of)]418 object_downcast_mut: context_chain_downcast_mut::<C>,419 object_drop_rest: context_chain_drop_rest::<C>,
200 unmodified lines
620 Some(addr.cast::<E>().deref_mut())621 }622 }623624 /// Convert to a standard library error trait object.625 ///626 /// This is implemented as a cheap pointer cast that does not allocate or627 /// deallocate memory. Like [`anyhow::Error::from_boxed`], it's useful for628 /// interop with other error libraries.629 ///630 /// The same conversion is also available as631 /// <code style="display:inline;white-space:normal;">impl From&lt;anyhow::Error&gt;632 /// for Box&lt;dyn Error + Send + Sync + &apos;static&gt;</code>.633 ///634 /// If a backtrace was collected during construction of the `anyhow::Error`,635 /// that backtrace remains accessible using the standard library `Error`636 /// trait's provider API, but as a consequence, the resulting boxed error637 /// can no longer be downcast to its original underlying type.638 ///639 /// ```640 #[cfg_attr(not(error_generic_member_access), doc = "# _ = stringify! {")]641 /// #![feature(error_generic_member_access)]642 ///643 /// use anyhow::anyhow;644 /// use std::backtrace::Backtrace;645 /// use thiserror::Error;646 ///647 /// #[derive(Error, Debug)]648 /// #[error("...")]649 /// struct MyError;650 ///651 /// let anyhow_error = anyhow!(MyError);652 /// println!("{}", anyhow_error.backtrace()); // has Backtrace653 /// assert!(anyhow_error.downcast_ref::<MyError>().is_some()); // can downcast654 ///655 /// let boxed_dyn_error = anyhow_error.into_boxed_dyn_error();656 /// assert!(std::error::request_ref::<Backtrace>(&*boxed_dyn_error).is_some()); // has Backtrace657 /// assert!(boxed_dyn_error.downcast_ref::<MyError>().is_none()); // can no longer downcast658 #[cfg_attr(not(error_generic_member_access), doc = "# };")]659 /// ```660 ///661 /// [`anyhow::Error::from_boxed`]: Self::from_boxed662 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]663 #[must_use]664 pub fn into_boxed_dyn_error(self) -> Box<dyn StdError + Send + Sync + 'static> {665 let outer = ManuallyDrop::new(self);666 unsafe {667 // Use vtable to attach ErrorImpl<E>'s native StdError vtable for668 // the right original type E.669 (vtable(outer.inner.ptr).object_boxed)(outer.inner)670 }671 }672673 /// Convert to a standard library error trait object.674 ///675 /// Unlike `self.into_boxed_dyn_error()`, this method relocates the676 /// underlying error into a new allocation in order to make it downcastable677 /// to `&E` or `Box<E>` for its original underlying error type. Any678 /// backtrace collected during construction of the `anyhow::Error` is679 /// discarded.680 ///681 /// ```682 #[cfg_attr(not(error_generic_member_access), doc = "# _ = stringify!{")]683 /// #![feature(error_generic_member_access)]684 ///685 /// use anyhow::anyhow;686 /// use std::backtrace::Backtrace;687 /// use thiserror::Error;688 ///689 /// #[derive(Error, Debug)]690 /// #[error("...")]691 /// struct MyError;692 ///693 /// let anyhow_error = anyhow!(MyError);694 /// println!("{}", anyhow_error.backtrace()); // has Backtrace695 /// assert!(anyhow_error.downcast_ref::<MyError>().is_some()); // can downcast696 ///697 /// let boxed_dyn_error = anyhow_error.reallocate_into_boxed_dyn_error_without_backtrace();698 /// assert!(std::error::request_ref::<Backtrace>(&*boxed_dyn_error).is_none()); // Backtrace lost699 /// assert!(boxed_dyn_error.downcast_ref::<MyError>().is_some()); // can downcast to &MyError700 /// assert!(boxed_dyn_error.downcast::<MyError>().is_ok()); // can downcast to Box<MyError>701 #[cfg_attr(not(error_generic_member_access), doc = "# };")]702 /// ```703 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]704 #[must_use]705 pub fn reallocate_into_boxed_dyn_error_without_backtrace(706 self,707 ) -> Box<dyn StdError + Send + Sync + 'static> {708 let outer = ManuallyDrop::new(self);709 unsafe {710 // Use vtable to attach E's native StdError vtable for the right711 // original type E.712 (vtable(outer.inner.ptr).object_reallocate_boxed)(outer.inner)713 }714 }715716 #[cfg(error_generic_member_access)]717 pub(crate) fn provide<'a>(&'a self, request: &mut Request<'a>) {718 unsafe { ErrorImpl::provide(self.inner.by_ref(), request) }719 }
65 unmodified lines
785 #[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]786 object_mut: unsafe fn(Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static),787 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]788 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,789 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]790 object_reallocate_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,791 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,792 #[cfg(anyhow_no_ptr_addr_of)]793 object_downcast_mut: unsafe fn(Mut<ErrorImpl>, TypeId) -> Option<Mut<()>>,794 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),
63 unmodified lines
858 unsafe { unerased_own.boxed() }859}860861// Safety: requires layout of *e to match ErrorImpl<E>.862#[cfg(any(feature = "std", not(anyhow_no_core_error)))]863unsafe fn object_reallocate_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>864where865 E: StdError + Send + Sync + 'static,866{867 // Attach E's native StdError vtable.868 let unerased_own = e.cast::<ErrorImpl<E>>();869 Box::new(unsafe { unerased_own.boxed() }._object)870}871872// Safety: requires layout of *e to match ErrorImpl<E>.873unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>874where875 E: 'static,876{
299 unmodified lines
1176#[cfg(any(feature = "std", not(anyhow_no_core_error)))]1177impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {1178 #[cold]1179 fn from(error: Error) -> Self {1063 let outer = ManuallyDrop::new(error);1064 unsafe {1065 // Use vtable to attach ErrorImpl<E>'s native StdError vtable for1066 // the right original type E.1067 (vtable(outer.inner.ptr).object_boxed)(outer.inner)1068 }1180 error.into_boxed_dyn_error()1181 }1182}11831184#[cfg(any(feature = "std", not(anyhow_no_core_error)))]1185impl From<Error> for Box<dyn StdError + Send + 'static> {1186 #[cold]1187 fn from(error: Error) -> Self {1075 Box::<dyn StdError + Send + Sync>::from(error)1188 error.into_boxed_dyn_error()1189 }1190}11911192#[cfg(any(feature = "std", not(anyhow_no_core_error)))]1193impl From<Error> for Box<dyn StdError + 'static> {1194 #[cold]1195 fn from(error: Error) -> Self {1082 Box::<dyn StdError + Send + Sync>::from(error)1196 error.into_boxed_dyn_error()1197 }1198}11991200#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
18 unmodified lines