-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add StdError impls for core and alloc errors
- Loading branch information
Showing
2 changed files
with
120 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use core::{ | ||
alloc::LayoutError, | ||
array::TryFromSliceError, | ||
cell::{BorrowError, BorrowMutError}, | ||
char::{CharTryFromError, DecodeUtf16Error, ParseCharError, TryFromCharError}, | ||
ffi::{ | ||
FromBytesUntilNulError, | ||
FromBytesWithNulError, | ||
}, | ||
fmt, | ||
num::{ParseFloatError, ParseIntError, TryFromIntError}, str::{ParseBoolError, Utf8Error}, | ||
time::TryFromFloatSecsError, | ||
}; | ||
#[cfg(feature = "alloc")] | ||
use crate::alloc::{ | ||
collections::TryReserveError, | ||
ffi::{ | ||
NulError, | ||
FromVecWithNulError, | ||
IntoStringError, | ||
}, | ||
string::{FromUtf16Error, FromUtf8Error}, | ||
}; | ||
|
||
/// An error that can be debugged and displayed. | ||
/// | ||
/// With the `std` feature enabled, this is a re-export of the [`Error`] trait | ||
/// instead. | ||
/// | ||
/// [`Error`]: std::error::Error | ||
pub trait StdError: fmt::Debug + fmt::Display { | ||
/// The lower-level source of this error, if any. | ||
fn source(&self) -> Option<&(dyn StdError + 'static)> { | ||
None | ||
} | ||
} | ||
|
||
// SAFETY: The metadata type of `dyn StdError` is `DynMetadata<dyn StdError>`. | ||
unsafe impl ptr_meta::Pointee for dyn StdError { | ||
type Metadata = ptr_meta::DynMetadata<dyn StdError>; | ||
} | ||
|
||
// SAFETY: The metadata type of `dyn StdError + Send` is | ||
// `DynMetadata<dyn StdError + Send>`. | ||
unsafe impl ptr_meta::Pointee for dyn StdError + Send { | ||
type Metadata = ptr_meta::DynMetadata<dyn StdError + Send>; | ||
} | ||
|
||
// SAFETY: The metadata type of `dyn StdError + Sync` is | ||
// `DynMetadata<dyn StdError + Sync>`. | ||
unsafe impl ptr_meta::Pointee for dyn StdError + Sync { | ||
type Metadata = ptr_meta::DynMetadata<dyn StdError + Sync>; | ||
} | ||
|
||
// SAFETY: The metadata type of `dyn StdError + Send + Sync` is | ||
// `DynMetadata<dyn StdError + Send + Sync>`. | ||
unsafe impl ptr_meta::Pointee for dyn StdError + Send + Sync { | ||
type Metadata = ptr_meta::DynMetadata<dyn StdError + Send + Sync>; | ||
} | ||
|
||
impl<T: StdError> StdError for &'_ T { | ||
fn source(&self) -> Option<&(dyn StdError + 'static)> { | ||
T::source(self) | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
impl<T: StdError> StdError for crate::alloc::boxed::Box<T> { | ||
fn source(&self) -> Option<&(dyn StdError + 'static)> { | ||
T::source(self) | ||
} | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
impl<T: StdError> StdError for crate::alloc::sync::Arc<T> { | ||
fn source(&self) -> Option<&(dyn StdError + 'static)> { | ||
T::source(self) | ||
} | ||
} | ||
|
||
macro_rules! impl_std_error { | ||
($($tys:ty),* $(,)?) => { | ||
$( | ||
impl StdError for $tys {} | ||
)* | ||
} | ||
} | ||
|
||
impl_std_error! { | ||
LayoutError, | ||
TryFromSliceError, | ||
BorrowError, | ||
BorrowMutError, | ||
CharTryFromError, | ||
DecodeUtf16Error, | ||
ParseCharError, | ||
TryFromCharError, | ||
FromBytesUntilNulError, | ||
FromBytesWithNulError, | ||
fmt::Error, | ||
ParseFloatError, | ||
ParseIntError, | ||
TryFromIntError, | ||
ParseBoolError, | ||
Utf8Error, | ||
FromUtf8Error, | ||
FromUtf16Error, | ||
TryFromFloatSecsError, | ||
} | ||
|
||
#[cfg(feature = "alloc")] | ||
impl_std_error! { | ||
TryReserveError, | ||
NulError, | ||
FromVecWithNulError, | ||
IntoStringError, | ||
} |