|
1 | 1 | use crate::boxed::Box; |
| 2 | +use crate::fmt::{self, Debug, Display}; |
| 3 | +use crate::string::String; |
2 | 4 |
|
3 | | -#[cfg(feature = "std")] |
4 | | -pub use std::error::Error; |
5 | | - |
6 | | -#[cfg(not(feature = "std"))] |
7 | 5 | pub trait Error: core::fmt::Debug + core::fmt::Display { |
8 | 6 | fn source(&self) -> Option<&(dyn Error + 'static)> { |
9 | 7 | None |
10 | 8 | } |
11 | 9 | } |
12 | 10 |
|
13 | | -#[cfg(not(feature = "std"))] |
14 | 11 | impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> { |
15 | 12 | fn from(err: E) -> Self { |
16 | 13 | Box::new(err) |
17 | 14 | } |
18 | 15 | } |
19 | 16 |
|
20 | | -#[cfg(not(feature = "std"))] |
| 17 | +impl<'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 | + |
21 | 23 | impl<T: Error> Error for Box<T> {} |
22 | 24 |
|
23 | | -#[cfg(not(feature = "std"))] |
24 | | -impl Error for crate::string::String {} |
| 25 | +impl 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 | + } |
25 | 37 |
|
26 | | -#[cfg(not(feature = "std"))] |
27 | | -impl Error for crate::io::Error {} |
| 38 | + // Purposefully skip printing "StringError(..)" |
| 39 | + 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 | + |
| 49 | +impl<'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 | +} |
0 commit comments