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: object_reallocate_boxed::<BoxedError>,
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
787unsafe fn object_downcast<E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
789where
790 E: 'static,
791{
792 if TypeId::of::<E>() == target {
793 let unerased_ref = e.cast::<ErrorImpl<E>>();
796 Some(
797 Ref::from_raw(unsafe {
798 NonNull::new_unchecked(&raw const (*unerased_ref.as_ptr())._objectptr::addr_of!((*unerased_ref.as_ptr())._object).cast_mut())
799 })
800 .cast::<()>(),
801 )
802 } else {
803 None
804 }
805}
806
807#[cfg(all(not(error_generic_member_access), feature = "std"))]
808fn no_backtrace(e: Ref<ErrorImpl>) -> Option<&Backtrace> {
809 let _ = e;
810 None
811}
812
813#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
815unsafe fn context_downcast<C, E>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
816where
817 C: 'static,
818 E: 'static,
819{
820 if TypeId::of::<C>() == target {
821 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();
822 let unerased = unsafe { unerased_ref.deref() };
823 Some(Ref::new(&unerased._object.context).cast::<()>())
824 } else if TypeId::of::<E>() == target {
825 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, E>>>();
826 let unerased = unsafe { unerased_ref.deref() };
827 Some(Ref::new(&unerased._object.error).cast::<()>())
828 } else {
829 None
830 }
831}
832
833#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
835unsafe fn context_drop_rest<C, E>(e: Own<ErrorImpl>, target: TypeId)
836where
837 C: 'static,
838 E: 'static,
839{
840 if TypeId::of::<C>() == target {
843 let unerased_own = e.cast::<ErrorImpl<ContextError<ManuallyDrop<C>, E>>>();
844 drop(unsafe { unerased_own.boxed() });
845 } else {
846 let unerased_own = e.cast::<ErrorImpl<ContextError<C, ManuallyDrop<E>>>>();
847 drop(unsafe { unerased_own.boxed() });
848 }
849}
850
851unsafe fn context_chain_downcast<C>(e: Ref<ErrorImpl>, target: TypeId) -> Option<Ref<()>>
853where
854 C: 'static,
855{
856 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();
857 let unerased = unsafe { unerased_ref.deref() };
858 if TypeId::of::<C>() == target {
859 Some(Ref::new(&unerased._object.context).cast::<()>())
860 } else {
861 let source = &unerased._object.error;
863 unsafe { (vtable(source.inner.ptr).object_downcast)(source.inner.by_ref(), target) }
864 }
865}
866
867unsafe fn context_chain_drop_rest<C>(e: Own<ErrorImpl>, target: TypeId)
869where
870 C: 'static,
871{
872 if TypeId::of::<C>() == target {
875 let unerased_own = e.cast::<ErrorImpl<ContextError<ManuallyDrop<C>, Error>>>();
876 drop(unsafe { unerased_own.boxed() });
878 } else {
879 let unerased_own = e.cast::<ErrorImpl<ContextError<C, ManuallyDrop<Error>>>>();
880 let unerased = unsafe { unerased_own.boxed() };
881 let inner = unerased._object.error.inner;
883 drop(unerased);
884 let vtable = unsafe { vtable(inner.ptr) };
885 unsafe { (vtable.object_drop_rest)(inner, target) };
887 }
888}
889
890#[cfg(all(not(error_generic_member_access), feature = "std"))]
892#[allow(clippy::unnecessary_wraps)]
893unsafe fn context_backtrace<C>(e: Ref<ErrorImpl>) -> Option<&Backtrace>
894where
895 C: 'static,
896{
897 let unerased_ref = e.cast::<ErrorImpl<ContextError<C, Error>>>();
898 let unerased = unsafe { unerased_ref.deref() };
899 let backtrace = unsafe { ErrorImpl::backtrace(unerased._object.error.inner.by_ref()) };
900 Some(backtrace)
901}
902
903#[repr(C)]
907pub(crate) struct ErrorImpl<E = ()> {
908 vtable: &'static ErrorVTable,
909 backtrace: Option<Backtrace>,
910 _object: E,
913}
914
915unsafe fn vtable(p: NonNull<ErrorImpl>) -> &'static ErrorVTable {
918 unsafe { *(p.as_ptr() as *const &'static ErrorVTable) }
920}
921
922#[repr(C)]
925pub(crate) struct ContextError<C, E> {
926 pub context: C,
927 pub error: E,
928}
929
930impl<E> ErrorImpl<E> {
931 fn erase(&self) -> Ref<ErrorImpl> {
932 Ref::new(self).cast::<ErrorImpl>()
936 }
937}
938
939impl ErrorImpl {
940 pub(crate) unsafe fn error(this: Ref<Self>) -> &(dyn StdError + Send + Sync + 'static) {
941 unsafe { (vtable(this.ptr).object_ref)(this).deref() }
944 }
945
946 #[cfg(any(feature = "std", not(anyhow_no_core_error)))]
947 pub(crate) unsafe fn error_mut(this: Mut<Self>) -> &mut (dyn StdError + Send + Sync + 'static) {
948 unsafe {
951 (vtable(this.ptr).object_ref)(this.by_ref())
952 .by_mut()
953 .deref_mut()
954 }
955 }
956
957 #[cfg(feature = "std")]
958 pub(crate) unsafe fn backtrace(this: Ref<Self>) -> &Backtrace {
959 unsafe { this.deref() }
963 .backtrace
964 .as_ref()
965 .or_else(|| {
966 #[cfg(error_generic_member_access)]
967 return nightly::request_ref_backtrace(unsafe { Self::error(this) });
968 #[cfg(not(error_generic_member_access))]
969 return unsafe { (vtable(this.ptr).object_backtrace)(this) };
970 })
971 .expect("backtrace capture failed")
972 }
973
974 #[cfg(error_generic_member_access)]
975 unsafe fn provide<'a>(this: Ref<'a, Self>, request: &mut Request<'a>) {
976 if let Some(backtrace) = unsafe { &this.deref().backtrace } {
977 nightly::provide_ref_backtrace(request, backtrace);
978 }
979 nightly::provide(unsafe { Self::error(this) }, request);
980 }
981
982 #[cold]
983 pub(crate) unsafe fn chain(this: Ref<Self>) -> Chain {
984 Chain::new(unsafe { Self::error(this) })
985 }
986}
987
988impl<E> StdError for ErrorImpl<E>
989where
990 E: StdError,
991{
992 fn source(&self) -> Option<&(dyn StdError + 'static)> {
993 unsafe { ErrorImpl::error(self.erase()).source() }
994 }
995
996 #[cfg(error_generic_member_access)]
997 fn provide<'a>(&'a self, request: &mut Request<'a>) {
998 unsafe { ErrorImpl::provide(self.erase(), request) }
999 }
1000}
1001
1002impl<E> Debug for ErrorImpl<E>
1003where
1004 E: Debug,
1005{
1006 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1007 unsafe { ErrorImpl::debug(self.erase(), formatter) }
1008 }
1009}
1010
1011impl<E> Display for ErrorImpl<E>
1012where
1013 E: Display,
1014{
1015 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
1016 unsafe { Display::fmt(ErrorImpl::error(self.erase()), formatter) }
1017 }
1018}
1019
1020#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1021impl From<Error> for Box<dyn StdError + Send + Sync + 'static> {
1022 #[cold]
1023 fn from(error: Error) -> Self {
1024 error.into_boxed_dyn_error()
1025 }
1026}
1027
1028#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1029impl From<Error> for Box<dyn StdError + Send + 'static> {
1030 #[cold]
1031 fn from(error: Error) -> Self {
1032 error.into_boxed_dyn_error()
1033 }
1034}
1035
1036#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1037impl From<Error> for Box<dyn StdError + 'static> {
1038 #[cold]
1039 fn from(error: Error) -> Self {
1040 error.into_boxed_dyn_error()
1041 }
1042}
1043
1044#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1045impl AsRef<dyn StdError + Send + Sync> for Error {
1046 fn as_ref(&self) -> &(dyn StdError + Send + Sync + 'static) {
1047 &**self
1048 }
1049}
1050
1051#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
1052impl AsRef<dyn StdError> for Error {
1053 fn as_ref(&self) -> &(dyn StdError + 'static) {
1054 &**self
1055 }
1056}
1057
1058impl UnwindSafe for Error {}
1059impl RefUnwindSafe for Error {}