1use crate::backtrace::Backtrace;
2use crate::chain::Chain;
3#[cfg(error_generic_member_access)]
4use crate::nightly::{self, Request};
5#[cfg(any(feature = "std", not(anyhow_no_core_error), anyhow_no_ptr_addr_of))]
6use crate::ptr::Mut;
7use crate::ptr::{Own, Ref};
8use crate::{Error, StdError};
9use alloc::boxed::Box;
10use core::any::TypeId;
11use core::fmt::{self, Debug, Display};
12use core::mem::ManuallyDrop;
13#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
14use core::ops::{Deref, DerefMut};
15#[cfg(not(anyhow_no_core_unwind_safe))]
16use core::panic::{RefUnwindSafe, UnwindSafe};
17#[cfg(not(anyhow_no_ptr_addr_of))]
18use core::ptr;
19use core::ptr::NonNull;
20#[cfg(all(feature = "std", anyhow_no_core_unwind_safe))]
21use std::panic::{RefUnwindSafe, UnwindSafe};
22
23impl Error {
24 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
32 #[cold]
33 #[must_use]
34 pub fn new<E>(error: E) -> Self
35 where
36 E: StdError + Send + Sync + 'static,
37 {
38 let backtrace = backtrace_if_absent!(&error);
39 Error::construct_from_std(error, backtrace)
40 }
41
42 #[cold]
80 #[must_use]
81 pub fn msg<M>(message: M) -> Self
82 where
83 M: Display + Debug + Send + Sync + 'static,
84 {
85 Error::construct_from_adhoc(message, backtrace!())
86 }
87
88 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
142 #[cold]
143 #[must_use]
144 pub fn from_boxed(boxed_error: Box<dyn StdError + Send + Sync + 'static>) -> Self {
145 let backtrace = backtrace_if_absent!(&*boxed_error);
146 Error::construct_from_boxed(boxed_error, backtrace)
147 }
148
149 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
150 #[cold]
151 pub(crate) fn construct_from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self
152 where
153 E: StdError + Send + Sync + 'static,
154 {
155 let vtable = &ErrorVTable {
156 object_drop: object_drop::<E>,
157 object_ref: object_ref::<E>,
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>,
168 #[cfg(all(
169 not(error_generic_member_access),
170 any(std_backtrace, feature = "backtrace")
171 ))]
172 object_backtrace: no_backtrace,
173 };
174
175 unsafe { Error::construct(error, vtable, backtrace) }
177 }
178
179 #[cold]
180 pub(crate) fn construct_from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self
181 where
182 M: Display + Debug + Send + Sync + 'static,
183 {
184 use crate::wrapper::MessageError;
185 let error: MessageError<M> = MessageError(message);
186 let vtable = &ErrorVTable {
187 object_drop: object_drop::<MessageError<M>>,
188 object_ref: object_ref::<MessageError<M>>,
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>,
199 #[cfg(all(
200 not(error_generic_member_access),
201 any(std_backtrace, feature = "backtrace")
202 ))]
203 object_backtrace: no_backtrace,
204 };
205
206 unsafe { Error::construct(error, vtable, backtrace) }
209 }
210
211 #[cold]
212 pub(crate) fn construct_from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self
213 where
214 M: Display + Send + Sync + 'static,
215 {
216 use crate::wrapper::DisplayError;
217 let error: DisplayError<M> = DisplayError(message);
218 let vtable = &ErrorVTable {
219 object_drop: object_drop::<DisplayError<M>>,
220 object_ref: object_ref::<DisplayError<M>>,
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>,
231 #[cfg(all(
232 not(error_generic_member_access),
233 any(std_backtrace, feature = "backtrace")
234 ))]
235 object_backtrace: no_backtrace,
236 };
237
238 unsafe { Error::construct(error, vtable, backtrace) }
241 }
242
243 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
244 #[cold]
245 pub(crate) fn construct_from_context<C, E>(
246 context: C,
247 error: E,
248 backtrace: Option<Backtrace>,
249 ) -> Self
250 where
251 C: Display + Send + Sync + 'static,
252 E: StdError + Send + Sync + 'static,
253 {
254 let error: ContextError<C, E> = ContextError { context, error };
255
256 let vtable = &ErrorVTable {
257 object_drop: object_drop::<ContextError<C, E>>,
258 object_ref: object_ref::<ContextError<C, E>>,
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>,
269 #[cfg(all(
270 not(error_generic_member_access),
271 any(std_backtrace, feature = "backtrace")
272 ))]
273 object_backtrace: no_backtrace,
274 };
275
276 unsafe { Error::construct(error, vtable, backtrace) }
278 }
279
280 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
281 #[cold]
282 pub(crate) fn construct_from_boxed(
283 error: Box<dyn StdError + Send + Sync>,
284 backtrace: Option<Backtrace>,
285 ) -> Self {
286 use crate::wrapper::BoxedError;
287 let error = BoxedError(error);
288 let vtable = &ErrorVTable {
289 object_drop: object_drop::<BoxedError>,
290 object_ref: object_ref::<BoxedError>,
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>>,
301 #[cfg(all(
302 not(error_generic_member_access),
303 any(std_backtrace, feature = "backtrace")
304 ))]
305 object_backtrace: no_backtrace,
306 };
307
308 unsafe { Error::construct(error, vtable, backtrace) }
311 }
312
313 #[cold]
319 unsafe fn construct<E>(
320 error: E,
321 vtable: &'static ErrorVTable,
322 backtrace: Option<Backtrace>,
323 ) -> Self
324 where
325 E: StdError + Send + Sync + 'static,
326 {
327 let inner: Box<ErrorImpl<E>> = Box::new(ErrorImpl {
328 vtable,
329 backtrace,
330 _object: error,
331 });
332 let inner = Own::new(inner).cast::<ErrorImpl>();
339 Error { inner }
340 }
341
342 #[cold]
397 #[must_use]
398 pub fn context<C>(self, context: C) -> Self
399 where
400 C: Display + Send + Sync + 'static,
401 {
402 let error: ContextError<C, Error> = ContextError {
403 context,
404 error: self,
405 };
406
407 let vtable = &ErrorVTable {
408 object_drop: object_drop::<ContextError<C, Error>>,
409 object_ref: object_ref::<ContextError<C, Error>>,
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>,
420 #[cfg(all(
421 not(error_generic_member_access),
422 any(std_backtrace, feature = "backtrace")
423 ))]
424 object_backtrace: context_backtrace::<C>,
425 };
426
427 let backtrace = None;
429
430 unsafe { Error::construct(error, vtable, backtrace) }
432 }
433
434 #[cfg(any(std_backtrace, feature = "backtrace"))]
462 pub fn backtrace(&self) -> &impl_backtrace!() {
463 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }
464 }
465
466 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
488 #[cold]
489 pub fn chain(&self) -> Chain {
490 unsafe { ErrorImpl::chain(self.inner.by_ref()) }
491 }
492
493 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
499 #[allow(clippy::double_ended_iterator_last)]
500 pub fn root_cause(&self) -> &(dyn StdError + 'static) {
501 self.chain().last().unwrap()
502 }
503
504 pub fn is<E>(&self) -> bool
513 where
514 E: Display + Debug + Send + Sync + 'static,
515 {
516 self.downcast_ref::<E>().is_some()
517 }
518
519 pub fn downcast<E>(mut self) -> Result<E, Self>
521 where
522 E: Display + Debug + Send + Sync + 'static,
523 {
524 let target = TypeId::of::<E>();
525 let inner = self.inner.by_mut();
526 unsafe {
527 #[cfg(not(anyhow_no_ptr_addr_of))]
530 let addr = match (vtable(inner.ptr).object_downcast)(inner.by_ref(), target) {
531 Some(addr) => addr.by_mut().extend(),
532 None => return Err(self),
533 };
534 #[cfg(anyhow_no_ptr_addr_of)]
535 let addr = match (vtable(inner.ptr).object_downcast_mut)(inner, target) {
536 Some(addr) => addr.extend(),
537 None => return Err(self),
538 };
539
540 let outer = ManuallyDrop::new(self);
543
544 let error = addr.cast::<E>().read();
546
547 (vtable(outer.inner.ptr).object_drop_rest)(outer.inner, target);
549
550 Ok(error)
551 }
552 }
553
554 pub fn downcast_ref<E>(&self) -> Option<&E>
591 where
592 E: Display + Debug + Send + Sync + 'static,
593 {
594 let target = TypeId::of::<E>();
595 unsafe {
596 let addr = (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?;
599 Some(addr.cast::<E>().deref())
600 }
601 }
602
603 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>
605 where
606 E: Display + Debug + Send + Sync + 'static,
607 {
608 let target = TypeId::of::<E>();
609 unsafe {
610 #[cfg(not(anyhow_no_ptr_addr_of))]
614 let addr =
615 (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?.by_mut();
616
617 #[cfg(anyhow_no_ptr_addr_of)]
618 let addr = (vtable(self.inner.ptr).object_downcast_mut)(self.inner.by_mut(), target)?;
619
620 Some(addr.cast::<E>().deref_mut())
621 }
622 }
623
624 #[cfg_attr(not(error_generic_member_access), doc = "# _ = stringify! {")]
641 #[cfg_attr(not(error_generic_member_access), doc = "# };")]
659 #[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 (vtable(outer.inner.ptr).object_boxed)(outer.inner)
670 }
671 }
672
673 #[cfg_attr(not(error_generic_member_access), doc = "# _ = stringify!{")]
683 #[cfg_attr(not(error_generic_member_access), doc = "# };")]
702 #[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 (vtable(outer.inner.ptr).object_reallocate_boxed)(outer.inner)
713 }
714 }
715
716 #[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 }
720
721 #[cfg(error_generic_member_access)]
727 #[doc(hidden)]
728 pub fn thiserror_provide<'a>(&'a self, request: &mut Request<'a>) {
729 Self::provide(self, request);
730 }
731}
732
733#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
734impl<E> From<E> for Error
735where
736 E: StdError + Send + Sync + 'static,
737{
738 #[cold]
739 fn from(error: E) -> Self {
740 let backtrace = backtrace_if_absent!(&error);
741 Error::construct_from_std(error, backtrace)
742 }
743}
744
745#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
746impl Deref for Error {
747 type Target = dyn StdError + Send + Sync + 'static;
748
749 fn deref(&self) -> &Self::Target {
750 unsafe { ErrorImpl::error(self.inner.by_ref()) }
751 }
752}
753
754#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
755impl DerefMut for Error {
756 fn deref_mut(&mut self) -> &mut Self::Target {
757 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }
758 }
759}
760
761impl Display for Error {
762 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
763 unsafe { ErrorImpl::display(self.inner.by_ref(), formatter) }
764 }
765}
766
767impl Debug for Error {
768 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
769 unsafe { ErrorImpl::debug(self.inner.by_ref(), formatter) }
770 }
771}
772
773impl Drop for Error {
774 fn drop(&mut self) {
775 unsafe {
776 (vtable(self.inner.ptr).object_drop)(self.inner);
778 }
779 }
780}
781
782struct ErrorVTable {
783 object_drop: unsafe fn(Own<ErrorImpl>),
784 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,
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),
795 #[cfg(all(
796 not(error_generic_member_access),
797 any(std_backtrace, feature = "backtrace")
798 ))]
799 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,
800}
801
802unsafe fn object_drop<E>(e: Own<ErrorImpl>) {
804 let unerased_own = e.cast::<ErrorImpl<E>>();
807 drop(unsafe { unerased_own.boxed() });
808}
809
810unsafe fn object_drop_front<E>(e: Own<ErrorImpl>, target: TypeId) {
812 let _ = target;
816 let unerased_own = e.cast::<ErrorImpl<ManuallyDrop<E>>>();
817 drop(unsafe { unerased_own.boxed() });
818}
819
820unsafe fn object_ref<E>(e: Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>
822where
823 E: StdError + Send + Sync + 'static,
824{
825 let unerased_ref = e.cast::<ErrorImpl<E>>();
828
829 #[cfg(not(anyhow_no_ptr_addr_of))]
830 return Ref::from_raw(unsafe {
831 NonNull::new_unchecked(ptr::addr_of!((*unerased_ref.as_ptr())._object) as *mut E)
832 });
833
834 #[cfg(anyhow_no_ptr_addr_of)]
835 return Ref::new(unsafe { &unerased_ref.deref()._object });
836}
837
838#[cfg(all(any(feature = "std", not(anyhow_no_core_error)), anyhow_no_ptr_addr_of))]
841unsafe fn object_mut<E>(e: Mut<ErrorImpl>) -> &mut (dyn StdError + Send + Sync + 'static)
842where
843 E: StdError + Send + Sync + 'static,
844{
845 let unerased_mut = e.cast::<ErrorImpl<E>>();
847 unsafe { &mut unerased_mut.deref_mut()._object }
848}
849
850#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
852unsafe fn object_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>
853where
854 E: StdError + Send + Sync + 'static,
855{
856 let unerased_own = e.cast::<ErrorImpl<E>>();
858 unsafe { unerased_own.boxed() }
859}
860
861#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
863unsafe fn object_reallocate_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>
864where
865 E: StdError + Send + Sync + 'static,
866{
867 let unerased_own = e.cast::<ErrorImpl<E>>();
869 Box::new(unsafe { unerased_own.boxed() }._object)
870}
871
872unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
874where
875 E: 'static,
876{
877 if TypeId::of::<E>() == target {
878 let unerased_ref = e.cast::<ErrorImpl<E>>();
882
883 #[cfg(not(anyhow_no_ptr_addr_of))]
884 return Some(
885 Ref::from_raw(unsafe {
886 NonNull::new_unchecked(ptr::addr_of!((*unerased_ref.as_ptr())._object) as *mut E)
887 })
888 .cast::<()>(),
889 );
890
891 #[cfg(anyhow_no_ptr_addr_of)]
892 return Some(Ref::new(unsafe { &unerased_ref.deref()._object }).cast::<()>());
893 } else {
894 None
895 }
896}
897
898#[cfg(anyhow_no_ptr_addr_of)]
900unsafe fn object_downcast_mut<E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>
901where
902 E: 'static,
903{
904 if TypeId::of::<E>() == target {
905 let unerased_mut = e.cast::<ErrorImpl<E>>();
908 let unerased = unsafe { unerased_mut.deref_mut() };
909 Some(Mut::new(&mut unerased._object).cast::<()>())
910 } else {
911 None
912 }
913}
914
915#[cfg(all(
916 not(error_generic_member_access),
917 any(std_backtrace, feature = "backtrace")
918))]
919fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {
920 let _ = e;
921 None
922}
923
924#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
926unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
927where
928 C: 'static,
929 E: 'static,
930{
931 if TypeId::of::<C>() == target {
932 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();
933 let unerased = unsafe { unerased_ref.deref() };
934 Some(Ref::new(&unerased._object.context).cast::<()>())
935 } else if TypeId::of::<E>() == target {
936 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();
937 let unerased = unsafe { unerased_ref.deref() };
938 Some(Ref::new(&unerased._object.error).cast::<()>())
939 } else {
940 None
941 }
942}
943
944#[cfg(all(feature = "std", anyhow_no_ptr_addr_of))]
946unsafe fn context_downcast_mut<C, E>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>
947where
948 C: 'static,
949 E: 'static,
950{
951 if TypeId::of::<C>() == target {
952 let unerased_mut = e.cast::<ErrorImpl<ContextError<C, E>>>();
953 let unerased = unsafe { unerased_mut.deref_mut() };
954 Some(Mut::new(&mut unerased._object.context).cast::<()>())
955 } else if TypeId::of::<E>() == target {
956 let unerased_mut = e.cast::<ErrorImpl<ContextError<C, E>>>();
957 let unerased = unsafe { unerased_mut.deref_mut() };
958 Some(Mut::new(&mut unerased._object.error).cast::<()>())
959 } else {
960 None
961 }
962}
963
964#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
966unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)
967where
968 C: 'static,
969 E: 'static,
970{
971 if TypeId::of::<C>() == target {
974 let unerased_own = e.cast::<ErrorImpl<ContextError<ManuallyDrop<C>, E>>>();
975 drop(unsafe { unerased_own.boxed() });
976 } else {
977 let unerased_own = e.cast::<ErrorImpl<ContextError<C, ManuallyDrop<E>>>>();
978 drop(unsafe { unerased_own.boxed() });
979 }
980}
981
982unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
984where
985 C: 'static,
986{
987 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();
988 let unerased = unsafe { unerased_ref.deref() };
989 if TypeId::of::<C>() == target {
990 Some(Ref::new(&unerased._object.context).cast::<()>())
991 } else {
992 let source = &unerased._object.error;
994 unsafe { (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target) }
995 }
996}
997
998#[cfg(anyhow_no_ptr_addr_of)]
1000unsafe fn context_chain_downcast_mut<C>(e: Mut<ErrorImpl>, target: TypeId) -> Option<Mut<()>>
1001where
1002 C: 'static,
1003{
1004 let unerased_mut = e.cast::<ErrorImpl<ContextError<C, Error>>>();
1005 let unerased = unsafe { unerased_mut.deref_mut() };
1006 if TypeId::of::<C>() == target {
1007 Some(Mut::new(&mut unerased._object.context).cast::<()>())
1008 } else {
1009 let source = &mut unerased._object.error;
1011 unsafe { (vtable(source.inner.ptr).object_downcast_mut)(source.inner.by_mut(), target) }
1012 }
1013}
1014
1015unsafe fn context_chain_drop_rest<C>(e: Own<ErrorImpl>, target: TypeId)
1017where
1018 C: 'static,
1019{
1020 if TypeId::of::<C>() == target {
1023 let unerased_own = e.cast::<ErrorImpl<ContextError<ManuallyDrop<C>, Error>>>();
1024 drop(unsafe { unerased_own.boxed() });
1026 } else {
1027 let unerased_own = e.cast::<ErrorImpl<ContextError<C, ManuallyDrop<Error>>>>();
1028 let unerased = unsafe { unerased_own.boxed() };
1029 let inner = unerased._object.error.inner;
1031 drop(unerased);
1032 let vtable = unsafe { vtable(inner.ptr) };
1033 unsafe { (vtable.object_drop_rest)(inner, target) };
1035 }
1036}
1037
1038#[cfg(all(
1040 not(error_generic_member_access),
1041 any(std_backtrace, feature = "backtrace")
1042))]
1043#[allow(clippy::unnecessary_wraps)]
1044unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>
1045where
1046 C: 'static,
1047{
1048 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();
1049 let unerased = unsafe { unerased_ref.deref() };
1050 let backtrace = unsafe { ErrorImpl::backtrace(unerased._object.error.inner.by_ref()) };
1051 Some(backtrace)
1052}
1053
1054#[repr(C)]
1058pub(crate) struct ErrorImpl<E = ()> {
1059 vtable: &'static ErrorVTable,
1060 backtrace: Option<Backtrace>,
1061 _object: E,
1064}
1065
1066unsafe fn vtable(p: NonNull<ErrorImpl>) -> &'static ErrorVTable {
1069 unsafe { *(p.as_ptr() as *const &'static ErrorVTable) }
1071}
1072
1073#[repr(C)]
1076pub(crate) struct ContextError<C, E> {
1077 pub context: C,
1078 pub error: E,
1079}
1080
1081impl<E> ErrorImpl<E> {
1082 fn erase(&self) -> Ref<ErrorImpl> {
1083 Ref::new(self).cast::<ErrorImpl>()
1087 }
1088}
1089
1090impl ErrorImpl {
1091 pub(crate) unsafe fn error(this: Ref<Self>) -> &(dyn StdError + Send + Sync + 'static) {
1092 unsafe { (vtable(this.ptr).object_ref)(this).deref() }
1095 }
1096
1097 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1098 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {
1099 #[cfg(not(anyhow_no_ptr_addr_of))]
1103 return unsafe {
1104 (vtable(this.ptr).object_ref)(this.by_ref())
1105 .by_mut()
1106 .deref_mut()
1107 };
1108
1109 #[cfg(anyhow_no_ptr_addr_of)]
1110 return unsafe { (vtable(this.ptr).object_mut)(this) };
1111 }
1112
1113 #[cfg(any(std_backtrace, feature = "backtrace"))]
1114 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {
1115 unsafe { this.deref() }
1119 .backtrace
1120 .as_ref()
1121 .or_else(|| {
1122 #[cfg(error_generic_member_access)]
1123 return nightly::request_ref_backtrace(unsafe { Self::error(this) });
1124 #[cfg(not(error_generic_member_access))]
1125 return unsafe { (vtable(this.ptr).object_backtrace)(this) };
1126 })
1127 .expect("backtrace capture failed")
1128 }
1129
1130 #[cfg(error_generic_member_access)]
1131 unsafe fn provide<'a>(this: Ref<'a, Self>, request: &mut Request<'a>) {
1132 if let Some(backtrace) = unsafe { &this.deref().backtrace } {
1133 nightly::provide_ref_backtrace(request, backtrace);
1134 }
1135 nightly::provide(unsafe { Self::error(this) }, request);
1136 }
1137
1138 #[cold]
1139 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {
1140 Chain::new(unsafe { Self::error(this) })
1141 }
1142}
1143
1144impl<E> StdError for ErrorImpl<E>
1145where
1146 E: StdError,
1147{
1148 fn source(&self) -> Option<&(dyn StdError + 'static)> {
1149 unsafe { ErrorImpl::error(self.erase()).source() }
1150 }
1151
1152 #[cfg(error_generic_member_access)]
1153 fn provide<'a>(&'a self, request: &mut Request<'a>) {
1154 unsafe { ErrorImpl::provide(self.erase(), request) }
1155 }
1156}
1157
1158impl<E> Debug for ErrorImpl<E>
1159where
1160 E: Debug,
1161{
1162 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1163 unsafe { ErrorImpl::debug(self.erase(), formatter) }
1164 }
1165}
1166
1167impl<E> Display for ErrorImpl<E>
1168where
1169 E: Display,
1170{
1171 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1172 unsafe { Display::fmt(ErrorImpl::error(self.erase()), formatter) }
1173 }
1174}
1175
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 {
1180 error.into_boxed_dyn_error()
1181 }
1182}
1183
1184#[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 {
1188 error.into_boxed_dyn_error()
1189 }
1190}
1191
1192#[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 {
1196 error.into_boxed_dyn_error()
1197 }
1198}
1199
1200#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1201impl AsRef<dyn StdError + Send + Sync> for Error {
1202 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {
1203 &**self
1204 }
1205}
1206
1207#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1208impl AsRef<dyn StdError> for Error {
1209 fn as_ref(&self) -> &(dyn StdError + 'static) {
1210 &**self
1211 }
1212}
1213
1214#[cfg(any(feature = "std", not(anyhow_no_core_unwind_safe)))]
1215impl UnwindSafe for Error {}
1216
1217#[cfg(any(feature = "std", not(anyhow_no_core_unwind_safe)))]
1218impl RefUnwindSafe for Error {}