use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::string;
use alloc::string::String;
use core::any::TypeId;
use core::array;
use core::cell;
use core::char;
use core::fmt::{self, Debug, Display};
use core::mem::transmute;
use core::num;
use core::str;
pub trait Error: Debug + Display {
fn description(&self) -> &str {
"description() is deprecated; use Display"
}
#[deprecated(note = "replaced by Error::source, which can support downcasting")]
fn cause(&self) -> Option<&dyn Error> {
self.source()
}
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}
impl<'a, E: Error + 'a> From<E> for Box<dyn Error + 'a> {
fn from(err: E) -> Box<dyn Error + 'a> {
Box::new(err)
}
}
impl<'a, E: Error + Send + Sync + 'a> From<E> for Box<dyn Error + Send + Sync + 'a> {
fn from(err: E) -> Box<dyn Error + Send + Sync + 'a> {
Box::new(err)
}
}
impl From<String> for Box<dyn Error + Send + Sync> {
fn from(err: String) -> Box<dyn Error + Send + Sync> {
#[derive(Debug)]
struct StringError(String);
impl Error for StringError {
fn description(&self) -> &str {
&self.0
}
}
impl Display for StringError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Display::fmt(&self.0, f)
}
}
Box::new(StringError(err))
}
}
impl From<String> for Box<dyn Error> {
fn from(str_err: String) -> Box<dyn Error> {
let err1: Box<dyn Error + Send + Sync> = From::from(str_err);
let err2: Box<dyn Error> = err1;
err2
}
}
impl<'a> From<&str> for Box<dyn Error + Send + Sync + 'a> {
fn from(err: &str) -> Box<dyn Error + Send + Sync + 'a> {
From::from(String::from(err))
}
}
impl From<&str> for Box<dyn Error> {
fn from(err: &str) -> Box<dyn Error> {
From::from(String::from(err))
}
}
impl<'a, 'b> From<Cow<'b, str>> for Box<dyn Error + Send + Sync + 'a> {
fn from(err: Cow<'b, str>) -> Box<dyn Error + Send + Sync + 'a> {
From::from(String::from(err))
}
}
impl<'a> From<Cow<'a, str>> for Box<dyn Error> {
fn from(err: Cow<'a, str>) -> Box<dyn Error> {
From::from(String::from(err))
}
}
impl Error for str::ParseBoolError {
fn description(&self) -> &str {
"failed to parse bool"
}
}
impl Error for str::Utf8Error {
fn description(&self) -> &str {
"invalid utf-8: corrupt contents"
}
}
impl Error for num::ParseIntError {
fn description(&self) -> &str {
"Parse int error"
}
}
impl Error for num::TryFromIntError {
fn description(&self) -> &str {
"Try from int error"
}
}
impl Error for array::TryFromSliceError {
fn description(&self) -> &str {
"Try from slice error"
}
}
impl Error for num::ParseFloatError {
fn description(&self) -> &str {
"Parse float error"
}
}
impl Error for string::FromUtf8Error {
fn description(&self) -> &str {
"invalid utf-8"
}
}
impl Error for string::FromUtf16Error {
fn description(&self) -> &str {
"invalid utf-16"
}
}
impl Error for string::ParseError {
fn description(&self) -> &str {
match *self {}
}
}
impl Error for char::DecodeUtf16Error {
fn description(&self) -> &str {
"unpaired surrogate found"
}
}
impl<T: Error> Error for Box<T> {
fn description(&self) -> &str {
Error::description(&**self)
}
#[allow(deprecated)]
fn cause(&self) -> Option<&dyn Error> {
Error::cause(&**self)
}
}
impl Error for fmt::Error {
fn description(&self) -> &str {
"an error occurred when formatting an argument"
}
}
impl Error for cell::BorrowError {
fn description(&self) -> &str {
"already mutably borrowed"
}
}
impl Error for cell::BorrowMutError {
fn description(&self) -> &str {
"already borrowed"
}
}
impl Error for char::CharTryFromError {
fn description(&self) -> &str {
"converted integer out of range for `char`"
}
}
impl Error for char::ParseCharError {
fn description(&self) -> &str {
"Parse char error"
}
}
use core::any::Any;
impl dyn Error + 'static {
#[inline]
pub fn is<T: Error + 'static>(&self) -> bool {
let t = TypeId::of::<T>();
let boxed = self.type_id();
t == boxed
}
#[inline]
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
if self.is::<T>() {
unsafe { Some(&*(self as *const dyn Error as *const T)) }
} else {
None
}
}
#[inline]
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
if self.is::<T>() {
unsafe { Some(&mut *(self as *mut dyn Error as *mut T)) }
} else {
None
}
}
}
impl dyn Error + 'static + Send {
#[inline]
pub fn is<T: Error + 'static>(&self) -> bool {
<dyn Error + 'static>::is::<T>(self)
}
#[inline]
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
<dyn Error + 'static>::downcast_ref::<T>(self)
}
#[inline]
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
<dyn Error + 'static>::downcast_mut::<T>(self)
}
}
impl dyn Error + 'static + Send + Sync {
#[inline]
pub fn is<T: Error + 'static>(&self) -> bool {
<dyn Error + 'static>::is::<T>(self)
}
#[inline]
pub fn downcast_ref<T: Error + 'static>(&self) -> Option<&T> {
<dyn Error + 'static>::downcast_ref::<T>(self)
}
#[inline]
pub fn downcast_mut<T: Error + 'static>(&mut self) -> Option<&mut T> {
<dyn Error + 'static>::downcast_mut::<T>(self)
}
}
impl dyn Error {
#[inline]
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error>> {
if self.is::<T>() {
unsafe {
let raw: *mut dyn Error = Box::into_raw(self);
Ok(Box::from_raw(raw as *mut T))
}
} else {
Err(self)
}
}
}
impl dyn Error + Send {
#[inline]
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<dyn Error + Send>> {
let err: Box<dyn Error> = self;
<dyn Error>::downcast(err).map_err(|s| unsafe {
transmute::<Box<dyn Error>, Box<dyn Error + Send>>(s)
})
}
}
impl dyn Error + Send + Sync {
#[inline]
pub fn downcast<T: Error + 'static>(self: Box<Self>) -> Result<Box<T>, Box<Self>> {
let err: Box<dyn Error> = self;
<dyn Error>::downcast(err).map_err(|s| unsafe {
transmute::<Box<dyn Error>, Box<dyn Error + Send + Sync>>(s)
})
}
}