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)))]
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};
15use core::panic::{RefUnwindSafe, UnwindSafe};
16use core::ptr;
17use core::ptr::NonNull;
18
19impl Error {
20 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
28 #[cold]
29 #[must_use]
30 pub fn new<E>(error: E) -> Self
31 where
32 E: StdError + Send + Sync + 'static,
33 {
34 let backtrace = match crate::nightly::request_ref_backtrace(&error as &dyn core::error::Error)
{
Some(_) => None,
None => Some(std::backtrace::Backtrace::capture()),
}backtrace_if_absent!(&error);
35 Error::construct_from_std(error, backtrace)
36 }
37
38 #[cold]
76 #[must_use]
77 pub fn msg<M>(message: M) -> Self
78 where
79 M: Display + Debug + Send + Sync + 'static,
80 {
81 Error::construct_from_adhoc(message, Some(std::backtrace::Backtrace::capture())backtrace!())
82 }
83
84 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
138 #[cold]
139 #[must_use]
140 pub fn from_boxed(boxed_error: Box<dyn StdError + Send + Sync + 'static>) -> Self {
141 let backtrace = match crate::nightly::request_ref_backtrace(&*boxed_error as
&dyn core::error::Error) {
Some(_) => None,
None => Some(std::backtrace::Backtrace::capture()),
}backtrace_if_absent!(&*boxed_error);
142 Error::construct_from_boxed(boxed_error, backtrace)
143 }
144
145 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
146 #[cold]
147 pub(crate) fn construct_from_std<E>(error: E, backtrace: Option<Backtrace>) -> Self
148 where
149 E: StdError + Send + Sync + 'static,
150 {
151 let vtable = &ErrorVTable {
152 object_drop: object_drop::<E>,
153 object_ref: object_ref::<E>,
154 object_boxed: object_boxed::<E>,
155 object_reallocate_boxed: object_reallocate_boxed::<E>,
156 object_downcast: object_downcast::<E>,
157 object_drop_rest: object_drop_front::<E>,
158 #[cfg(all(not(error_generic_member_access), feature = "std"))]
159 object_backtrace: no_backtrace,
160 };
161
162 unsafe { Error::construct(error, vtable, backtrace) }
164 }
165
166 #[cold]
167 pub(crate) fn construct_from_adhoc<M>(message: M, backtrace: Option<Backtrace>) -> Self
168 where
169 M: Display + Debug + Send + Sync + 'static,
170 {
171 use crate::wrapper::MessageError;
172 let error: MessageError<M> = MessageError(message);
173 let vtable = &ErrorVTable {
174 object_drop: object_drop::<MessageError<M>>,
175 object_ref: object_ref::<MessageError<M>>,
176 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
177 object_boxed: object_boxed::<MessageError<M>>,
178 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
179 object_reallocate_boxed: object_reallocate_boxed::<MessageError<M>>,
180 object_downcast: object_downcast::<M>,
181 object_drop_rest: object_drop_front::<M>,
182 #[cfg(all(not(error_generic_member_access), feature = "std"))]
183 object_backtrace: no_backtrace,
184 };
185
186 unsafe { Error::construct(error, vtable, backtrace) }
189 }
190
191 #[cold]
192 pub(crate) fn construct_from_display<M>(message: M, backtrace: Option<Backtrace>) -> Self
193 where
194 M: Display + Send + Sync + 'static,
195 {
196 use crate::wrapper::DisplayError;
197 let error: DisplayError<M> = DisplayError(message);
198 let vtable = &ErrorVTable {
199 object_drop: object_drop::<DisplayError<M>>,
200 object_ref: object_ref::<DisplayError<M>>,
201 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
202 object_boxed: object_boxed::<DisplayError<M>>,
203 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
204 object_reallocate_boxed: object_reallocate_boxed::<DisplayError<M>>,
205 object_downcast: object_downcast::<M>,
206 object_drop_rest: object_drop_front::<M>,
207 #[cfg(all(not(error_generic_member_access), feature = "std"))]
208 object_backtrace: no_backtrace,
209 };
210
211 unsafe { Error::construct(error, vtable, backtrace) }
214 }
215
216 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
217 #[cold]
218 pub(crate) fn construct_from_context<C, E>(
219 context: C,
220 error: E,
221 backtrace: Option<Backtrace>,
222 ) -> Self
223 where
224 C: Display + Send + Sync + 'static,
225 E: StdError + Send + Sync + 'static,
226 {
227 let error: ContextError<C, E> = ContextError { context, error };
228
229 let vtable = &ErrorVTable {
230 object_drop: object_drop::<ContextError<C, E>>,
231 object_ref: object_ref::<ContextError<C, E>>,
232 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
233 object_boxed: object_boxed::<ContextError<C, E>>,
234 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
235 object_reallocate_boxed: object_reallocate_boxed::<ContextError<C, E>>,
236 object_downcast: context_downcast::<C, E>,
237 object_drop_rest: context_drop_rest::<C, E>,
238 #[cfg(all(not(error_generic_member_access), feature = "std"))]
239 object_backtrace: no_backtrace,
240 };
241
242 unsafe { Error::construct(error, vtable, backtrace) }
244 }
245
246 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
247 #[cold]
248 pub(crate) fn construct_from_boxed(
249 error: Box<dyn StdError + Send + Sync>,
250 backtrace: Option<Backtrace>,
251 ) -> Self {
252 use crate::wrapper::BoxedError;
253 let error = BoxedError(error);
254 let vtable = &ErrorVTable {
255 object_drop: object_drop::<BoxedError>,
256 object_ref: object_ref::<BoxedError>,
257 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
258 object_boxed: object_boxed::<BoxedError>,
259 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
260 object_reallocate_boxed: boxed_reallocate_boxed,
261 object_downcast: object_downcast::<Box<dyn StdError + Send + Sync>>,
262 object_drop_rest: object_drop_front::<Box<dyn StdError + Send + Sync>>,
263 #[cfg(all(not(error_generic_member_access), feature = "std"))]
264 object_backtrace: no_backtrace,
265 };
266
267 unsafe { Error::construct(error, vtable, backtrace) }
270 }
271
272 #[cold]
278 unsafe fn construct<E>(
279 error: E,
280 vtable: &'static ErrorVTable,
281 backtrace: Option<Backtrace>,
282 ) -> Self
283 where
284 E: StdError + Send + Sync + 'static,
285 {
286 let inner: Box<ErrorImpl<E>> = Box::new(ErrorImpl {
287 vtable,
288 backtrace,
289 _object: error,
290 });
291 let inner = Own::new(inner).cast::<ErrorImpl>();
298 Error { inner }
299 }
300
301 #[cold]
356 #[must_use]
357 pub fn context<C>(self, context: C) -> Self
358 where
359 C: Display + Send + Sync + 'static,
360 {
361 let error: ContextError<C, Error> = ContextError {
362 context,
363 error: self,
364 };
365
366 let vtable = &ErrorVTable {
367 object_drop: object_drop::<ContextError<C, Error>>,
368 object_ref: object_ref::<ContextError<C, Error>>,
369 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
370 object_boxed: object_boxed::<ContextError<C, Error>>,
371 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
372 object_reallocate_boxed: object_reallocate_boxed::<ContextError<C, Error>>,
373 object_downcast: context_chain_downcast::<C>,
374 object_drop_rest: context_chain_drop_rest::<C>,
375 #[cfg(all(not(error_generic_member_access), feature = "std"))]
376 object_backtrace: context_backtrace::<C>,
377 };
378
379 let backtrace = None;
381
382 unsafe { Error::construct(error, vtable, backtrace) }
384 }
385
386 #[cfg(feature = "std")]
414 pub fn backtrace(&self) -> &Backtrace {
415 unsafe { ErrorImpl::backtrace(self.inner.by_ref()) }
416 }
417
418 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
440 #[cold]
441 pub fn chain(&self) -> Chain {
442 unsafe { ErrorImpl::chain(self.inner.by_ref()) }
443 }
444
445 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
451 #[allow(clippy::double_ended_iterator_last)]
452 pub fn root_cause(&self) -> &(dyn StdError + 'static) {
453 self.chain().last().unwrap()
454 }
455
456 pub fn is<E>(&self) -> bool
465 where
466 E: Display + Debug + Send + Sync + 'static,
467 {
468 self.downcast_ref::<E>().is_some()
469 }
470
471 pub fn downcast<E>(mut self) -> Result<E, Self>
473 where
474 E: Display + Debug + Send + Sync + 'static,
475 {
476 let target = TypeId::of::<E>();
477 let inner = self.inner.by_mut();
478 unsafe {
479 let addr = match (vtable(inner.ptr).object_downcast)(inner.by_ref(), target) {
482 Some(addr) => addr.by_mut().extend(),
483 None => return Err(self),
484 };
485
486 let outer = ManuallyDrop::new(self);
489
490 let error = addr.cast::<E>().read();
492
493 (vtable(outer.inner.ptr).object_drop_rest)(outer.inner, target);
495
496 Ok(error)
497 }
498 }
499
500 pub fn downcast_ref<E>(&self) -> Option<&E>
537 where
538 E: Display + Debug + Send + Sync + 'static,
539 {
540 let target = TypeId::of::<E>();
541 unsafe {
542 let addr = (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?;
545 Some(addr.cast::<E>().deref())
546 }
547 }
548
549 pub fn downcast_mut<E>(&mut self) -> Option<&mut E>
551 where
552 E: Display + Debug + Send + Sync + 'static,
553 {
554 let target = TypeId::of::<E>();
555 unsafe {
556 let addr =
559 (vtable(self.inner.ptr).object_downcast)(self.inner.by_ref(), target)?.by_mut();
560 Some(addr.cast::<E>().deref_mut())
561 }
562 }
563
564 #[cfg_attr(not(error_generic_member_access), doc = "# _ = stringify! {")]
581 #[cfg_attr(not(error_generic_member_access), doc = "# };")]
599 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
603 #[must_use]
604 pub fn into_boxed_dyn_error(self) -> Box<dyn StdError + Send + Sync + 'static> {
605 let outer = ManuallyDrop::new(self);
606 unsafe {
607 (vtable(outer.inner.ptr).object_boxed)(outer.inner)
610 }
611 }
612
613 #[cfg_attr(not(error_generic_member_access), doc = "# _ = stringify!{")]
623 #[cfg_attr(not(error_generic_member_access), doc = "# };")]
642 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
644 #[must_use]
645 pub fn reallocate_into_boxed_dyn_error_without_backtrace(
646 self,
647 ) -> Box<dyn StdError + Send + Sync + 'static> {
648 let outer = ManuallyDrop::new(self);
649 unsafe {
650 (vtable(outer.inner.ptr).object_reallocate_boxed)(outer.inner)
653 }
654 }
655
656 #[cfg(error_generic_member_access)]
657 pub(crate) fn provide<'a>(&'a self, request: &mut Request<'a>) {
658 unsafe { ErrorImpl::provide(self.inner.by_ref(), request) }
659 }
660
661 #[cfg(error_generic_member_access)]
667 #[doc(hidden)]
668 pub fn thiserror_provide<'a>(&'a self, request: &mut Request<'a>) {
669 Self::provide(self, request);
670 }
671}
672
673#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
674impl<E> From<E> for Error
675where
676 E: StdError + Send + Sync + 'static,
677{
678 #[cold]
679 fn from(error: E) -> Self {
680 let backtrace = match crate::nightly::request_ref_backtrace(&error as &dyn core::error::Error)
{
Some(_) => None,
None => Some(std::backtrace::Backtrace::capture()),
}backtrace_if_absent!(&error);
681 Error::construct_from_std(error, backtrace)
682 }
683}
684
685#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
686impl Deref for Error {
687 type Target = dyn StdError + Send + Sync + 'static;
688
689 fn deref(&self) -> &Self::Target {
690 unsafe { ErrorImpl::error(self.inner.by_ref()) }
691 }
692}
693
694#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
695impl DerefMut for Error {
696 fn deref_mut(&mut self) -> &mut Self::Target {
697 unsafe { ErrorImpl::error_mut(self.inner.by_mut()) }
698 }
699}
700
701impl Display for Error {
702 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
703 unsafe { ErrorImpl::display(self.inner.by_ref(), formatter) }
704 }
705}
706
707impl Debug for Error {
708 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
709 unsafe { ErrorImpl::debug(self.inner.by_ref(), formatter) }
710 }
711}
712
713impl Drop for Error {
714 fn drop(&mut self) {
715 unsafe {
716 (vtable(self.inner.ptr).object_drop)(self.inner);
718 }
719 }
720}
721
722struct ErrorVTable {
723 object_drop: unsafe fn(Own<ErrorImpl>),
724 object_ref: unsafe fn(Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>,
725 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
726 object_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,
727 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
728 object_reallocate_boxed: unsafe fn(Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>,
729 object_downcast: unsafe fn(Ref<ErrorImpl>, TypeId) -> Option<Ref<()>>,
730 object_drop_rest: unsafe fn(Own<ErrorImpl>, TypeId),
731 #[cfg(all(not(error_generic_member_access), feature = "std"))]
732 object_backtrace: unsafe fn(Ref<ErrorImpl>) -> Option<&Backtrace>,
733}
734
735unsafe fn object_drop<E>(e: Own<ErrorImpl>) {
737 let unerased_own = e.cast::<ErrorImpl<E>>();
740 drop(unsafe { unerased_own.boxed() });
741}
742
743unsafe fn object_drop_front<E>(e: Own<ErrorImpl>, target: TypeId) {
745 let _ = target;
749 let unerased_own = e.cast::<ErrorImpl<ManuallyDrop<E>>>();
750 drop(unsafe { unerased_own.boxed() });
751}
752
753unsafe fn object_ref<E>(e: Ref<ErrorImpl>) -> Ref<dyn StdError + Send + Sync + 'static>
755where
756 E: StdError + Send + Sync + 'static,
757{
758 let unerased_ref = e.cast::<ErrorImpl<E>>();
760 Ref::from_raw(unsafe {
761 NonNull::new_unchecked(&raw const (*unerased_ref.as_ptr())._objectptr::addr_of!((*unerased_ref.as_ptr())._object).cast_mut())
762 })
763}
764
765#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
767unsafe fn object_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>
768where
769 E: StdError + Send + Sync + 'static,
770{
771 let unerased_own = e.cast::<ErrorImpl<E>>();
773 unsafe { unerased_own.boxed() }
774}
775
776#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
778unsafe fn object_reallocate_boxed<E>(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static>
779where
780 E: StdError + Send + Sync + 'static,
781{
782 let unerased_own = e.cast::<ErrorImpl<E>>();
784 Box::new(unsafe { unerased_own.boxed() }._object)
785}
786
787#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
789unsafe fn boxed_reallocate_boxed(e: Own<ErrorImpl>) -> Box<dyn StdError + Send + Sync + 'static> {
790 let unerased_own = e.cast::<ErrorImpl<crate::wrapper::BoxedError>>();
793 unsafe { unerased_own.boxed() }._object.0
794}
795
796unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
798where
799 E: 'static,
800{
801 if TypeId::of::<E>() == target {
802 let unerased_ref = e.cast::<ErrorImpl<E>>();
805 Some(
806 Ref::from_raw(unsafe {
807 NonNull::new_unchecked(&raw const (*unerased_ref.as_ptr())._objectptr::addr_of!((*unerased_ref.as_ptr())._object).cast_mut())
808 })
809 .cast::<()>(),
810 )
811 } else {
812 None
813 }
814}
815
816#[cfg(all(not(error_generic_member_access), feature = "std"))]
817fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {
818 let _ = e;
819 None
820}
821
822#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
824unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
825where
826 C: 'static,
827 E: 'static,
828{
829 if TypeId::of::<C>() == target {
830 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();
831 let inner = unsafe { &raw const (*unerased_ref.as_ptr())._object.contextptr::addr_of!((*unerased_ref.as_ptr())._object.context) };
832 Some(Ref::from_raw(unsafe { NonNull::new_unchecked(inner.cast_mut()) }).cast::<()>())
833 } else if TypeId::of::<E>() == target {
834 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();
835 let inner = unsafe { &raw const (*unerased_ref.as_ptr())._object.errorptr::addr_of!((*unerased_ref.as_ptr())._object.error) };
836 Some(Ref::from_raw(unsafe { NonNull::new_unchecked(inner.cast_mut()) }).cast::<()>())
837 } else {
838 None
839 }
840}
841
842#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
844unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)
845where
846 C: 'static,
847 E: 'static,
848{
849 if TypeId::of::<C>() == target {
852 let unerased_own = e.cast::<ErrorImpl<ContextError<ManuallyDrop<C>, E>>>();
853 drop(unsafe { unerased_own.boxed() });
854 } else {
855 let unerased_own = e.cast::<ErrorImpl<ContextError<C, ManuallyDrop<E>>>>();
856 drop(unsafe { unerased_own.boxed() });
857 }
858}
859
860unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
862where
863 C: 'static,
864{
865 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();
866 if TypeId::of::<C>() == target {
867 let inner = unsafe { &raw const (*unerased_ref.as_ptr())._object.contextptr::addr_of!((*unerased_ref.as_ptr())._object.context) };
868 Some(Ref::from_raw(unsafe { NonNull::new_unchecked(inner.cast_mut()) }).cast::<()>())
869 } else {
870 let unerased = unsafe { unerased_ref.deref() };
872 let source = &unerased._object.error;
873 unsafe { (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target) }
874 }
875}
876
877unsafe fn context_chain_drop_rest<C>(e: Own<ErrorImpl>, target: TypeId)
879where
880 C: 'static,
881{
882 if TypeId::of::<C>() == target {
885 let unerased_own = e.cast::<ErrorImpl<ContextError<ManuallyDrop<C>, Error>>>();
886 drop(unsafe { unerased_own.boxed() });
888 } else {
889 let unerased_own = e.cast::<ErrorImpl<ContextError<C, ManuallyDrop<Error>>>>();
890 let unerased = unsafe { unerased_own.boxed() };
891 let inner = unerased._object.error.inner;
893 drop(unerased);
894 let vtable = unsafe { vtable(inner.ptr) };
895 unsafe { (vtable.object_drop_rest)(inner, target) };
897 }
898}
899
900#[cfg(all(not(error_generic_member_access), feature = "std"))]
902#[allow(clippy::unnecessary_wraps)]
903unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>
904where
905 C: 'static,
906{
907 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();
908 let unerased = unsafe { unerased_ref.deref() };
909 let backtrace = unsafe { ErrorImpl::backtrace(unerased._object.error.inner.by_ref()) };
910 Some(backtrace)
911}
912
913#[repr(C)]
917pub(crate) struct ErrorImpl<E = ()> {
918 vtable: &'static ErrorVTable,
919 backtrace: Option<Backtrace>,
920 _object: E,
923}
924
925unsafe fn vtable(p: NonNull<ErrorImpl>) -> &'static ErrorVTable {
928 unsafe { *(p.as_ptr() as *const &'static ErrorVTable) }
930}
931
932#[repr(C)]
935pub(crate) struct ContextError<C, E> {
936 pub context: C,
937 pub error: E,
938}
939
940impl<E> ErrorImpl<E> {
941 fn erase(&self) -> Ref<ErrorImpl> {
942 Ref::new(self).cast::<ErrorImpl>()
946 }
947}
948
949impl ErrorImpl {
950 pub(crate) unsafe fn error(this: Ref<Self>) -> &(dyn StdError + Send + Sync + 'static) {
951 unsafe { (vtable(this.ptr).object_ref)(this).deref() }
954 }
955
956 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
957 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {
958 unsafe {
961 (vtable(this.ptr).object_ref)(this.by_ref())
962 .by_mut()
963 .deref_mut()
964 }
965 }
966
967 #[cfg(feature = "std")]
968 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {
969 unsafe { this.deref() }
973 .backtrace
974 .as_ref()
975 .or_else(|| {
976 #[cfg(error_generic_member_access)]
977 return nightly::request_ref_backtrace(unsafe { Self::error(this) });
978 #[cfg(not(error_generic_member_access))]
979 return unsafe { (vtable(this.ptr).object_backtrace)(this) };
980 })
981 .expect("backtrace capture failed")
982 }
983
984 #[cfg(error_generic_member_access)]
985 unsafe fn provide<'a>(this: Ref<'a, Self>, request: &mut Request<'a>) {
986 if let Some(backtrace) = unsafe { &this.deref().backtrace } {
987 nightly::provide_ref_backtrace(request, backtrace);
988 }
989 nightly::provide(unsafe { Self::error(this) }, request);
990 }
991
992 #[cold]
993 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {
994 Chain::new(unsafe { Self::error(this) })
995 }
996}
997
998impl<E> StdError for ErrorImpl<E>
999where
1000 E: StdError,
1001{
1002 fn source(&self) -> Option<&(dyn StdError + 'static)> {
1003 unsafe { ErrorImpl::error(self.erase()).source() }
1004 }
1005
1006 #[cfg(error_generic_member_access)]
1007 fn provide<'a>(&'a self, request: &mut Request<'a>) {
1008 unsafe { ErrorImpl::provide(self.erase(), request) }
1009 }
1010}
1011
1012impl<E> Debug for ErrorImpl<E>
1013where
1014 E: Debug,
1015{
1016 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1017 unsafe { ErrorImpl::debug(self.erase(), formatter) }
1018 }
1019}
1020
1021impl<E> Display for ErrorImpl<E>
1022where
1023 E: Display,
1024{
1025 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1026 unsafe { Display::fmt(ErrorImpl::error(self.erase()), formatter) }
1027 }
1028}
1029
1030#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1031impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {
1032 #[cold]
1033 fn from(error: Error) -> Self {
1034 error.into_boxed_dyn_error()
1035 }
1036}
1037
1038#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1039impl From<Error> for Box<dyn StdError + Send + 'static> {
1040 #[cold]
1041 fn from(error: Error) -> Self {
1042 error.into_boxed_dyn_error()
1043 }
1044}
1045
1046#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1047impl From<Error> for Box<dyn StdError + 'static> {
1048 #[cold]
1049 fn from(error: Error) -> Self {
1050 error.into_boxed_dyn_error()
1051 }
1052}
1053
1054#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1055impl AsRef<dyn StdError + Send + Sync> for Error {
1056 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {
1057 &**self
1058 }
1059}
1060
1061#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1062impl AsRef<dyn StdError> for Error {
1063 fn as_ref(&self) -> &(dyn StdError + 'static) {
1064 &**self
1065 }
1066}
1067
1068impl UnwindSafe for Error {}
1069impl RefUnwindSafe for Error {}