1use crate::boxed::Box;
2use crate::fmt::{self, Debug, Display};
3use crate::string::String;
4
5pub trait Error: core::fmt::Debug + core::fmt::Display {
6 fn source(&self) -> Option<&(dyn Error + 'static)> {
7 None
8 }
9}
10
11impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
12 fn from(err: E) -> Self {
13 Box::new(err)
14 }
15}
16
17impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
18 fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
19 Box::new(err)
20 }
21}
22
23impl<T: Error> Error for Box<T> {}
24
25impl From<String> for Box<dyn Error + Send + Sync> {
26 #[inline]
27 fn from(err: String) -> Box<dyn Error + Send + Sync> {
28 struct StringError(String);
29
30 impl Error for StringError {}
31
32 impl Display for StringError {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 Display::fmt(&self.0, f)
35 }
36 }
37
38 impl Debug for StringError {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 Debug::fmt(&self.0, f)
42 }
43 }
44
45 Box::new(StringError(err))
46 }
47}
48
49impl<'a> From<&'a str> for Box<dyn Error + Send + Sync> {
50 #[inline]
51 fn from(err: &'a str) -> Box<dyn Error + Send + Sync> {
52 From::from(String::from(err))
53 }
54}