1use crate::Error;
48use core::fmt::{Debug, Display};
49
50#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
51use crate::StdError;
52#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
53use alloc::boxed::Box;
54
55pub struct Adhoc;
56
57#[doc(hidden)]
58pub trait AdhocKind: Sized {
59 #[inline]
60 fn anyhow_kind(&self) -> Adhoc {
61 Adhoc
62 }
63}
64
65impl<T> AdhocKind for &T where T: ?Sized + Display + Debug + Send + Sync + 'static {}
66
67impl Adhoc {
68 #[cold]
69 pub fn new<M>(self, message: M) -> Error
70 where
71 M: Display + Debug + Send + Sync + 'static,
72 {
73 Error::construct_from_adhoc(message, Some(crate::backtrace::Backtrace::capture())backtrace!())
74 }
75}
76
77pub struct Trait;
78
79#[doc(hidden)]
80pub trait TraitKind: Sized {
81 #[inline]
82 fn anyhow_kind(&self) -> Trait {
83 Trait
84 }
85}
86
87impl<E> TraitKind for E where E: Into<Error> {}
88
89impl Trait {
90 #[cold]
91 pub fn new<E>(self, error: E) -> Error
92 where
93 E: Into<Error>,
94 {
95 error.into()
96 }
97}
98
99#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
100pub struct Boxed;
101
102#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
103#[doc(hidden)]
104pub trait BoxedKind: Sized {
105 #[inline]
106 fn anyhow_kind(&self) -> Boxed {
107 Boxed
108 }
109}
110
111#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
112impl BoxedKind for Box<dyn StdError + Send + Sync> {}
113
114#[cfg(any(feature = "std", not(anyhow_no_core_error)))]
115impl Boxed {
116 #[cold]
117 pub fn new(self, error: Box<dyn StdError + Send + Sync>) -> Error {
118 let backtrace = match crate::nightly::request_ref_backtrace(&*error as
&dyn core::error::Error) {
Some(_) => None,
None => Some(crate::backtrace::Backtrace::capture()),
}backtrace_if_absent!(&*error);
119 Error::construct_from_boxed(error, backtrace)
120 }
121}