#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::boxed::Box;
use core::fmt;
#[derive(Debug)]
pub struct Error {
kind: ErrorKind,
_not_unwind_safe: core::marker::PhantomData<NotUnwindSafe>,
#[cfg(feature = "std")]
error: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
#[cfg(all(feature = "alloc", not(feature = "std")))]
error: Option<Box<dyn fmt::Debug + Send + Sync + 'static>>,
}
impl Error {
#[cfg(feature = "std")]
pub fn new<E>(kind: ErrorKind, error: E) -> Self
where
E: Into<Box<dyn std::error::Error + Send + Sync + 'static>>,
{
Self { kind, _not_unwind_safe: core::marker::PhantomData, error: Some(error.into()) }
}
#[cfg(all(feature = "alloc", not(feature = "std")))]
pub fn new<E: sealed::IntoBoxDynDebug>(kind: ErrorKind, error: E) -> Self {
Self { kind, _not_unwind_safe: core::marker::PhantomData, error: Some(error.into()) }
}
pub fn kind(&self) -> ErrorKind { self.kind }
#[cfg(feature = "std")]
pub fn get_ref(&self) -> Option<&(dyn std::error::Error + Send + Sync + 'static)> {
self.error.as_deref()
}
#[cfg(all(feature = "alloc", not(feature = "std")))]
pub fn get_ref(&self) -> Option<&(dyn fmt::Debug + Send + Sync + 'static)> {
self.error.as_deref()
}
}
impl From<ErrorKind> for Error {
fn from(kind: ErrorKind) -> Self {
Self {
kind,
_not_unwind_safe: core::marker::PhantomData,
#[cfg(any(feature = "std", feature = "alloc"))]
error: None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, fmt: &mut fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
fmt.write_fmt(format_args!("I/O Error: {}", self.kind.description()))?;
#[cfg(any(feature = "alloc", feature = "std"))]
if let Some(e) = &self.error {
fmt.write_fmt(format_args!(". {:?}", e))?;
}
Ok(())
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.error.as_ref().and_then(|e| e.as_ref().source())
}
}
#[cfg(feature = "std")]
impl From<std::io::Error> for Error {
fn from(o: std::io::Error) -> Self {
Self {
kind: ErrorKind::from_std(o.kind()),
_not_unwind_safe: core::marker::PhantomData,
error: o.into_inner(),
}
}
}
#[cfg(feature = "std")]
impl From<Error> for std::io::Error {
fn from(o: Error) -> Self {
if let Some(err) = o.error {
Self::new(o.kind.to_std(), err)
} else {
o.kind.to_std().into()
}
}
}
struct NotUnwindSafe {
_not_unwind_safe: core::marker::PhantomData<(&'static mut (), core::cell::UnsafeCell<()>)>,
}
unsafe impl Sync for NotUnwindSafe {}
macro_rules! define_errorkind {
($($(#[$($attr:tt)*])* $kind:ident),*) => {
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum ErrorKind {
$(
$(#[$($attr)*])*
$kind
),*
}
impl From<core::convert::Infallible> for ErrorKind {
fn from(never: core::convert::Infallible) -> Self { match never {} }
}
impl ErrorKind {
fn description(&self) -> &'static str {
match self {
$(Self::$kind => stringify!($kind)),*
}
}
#[cfg(feature = "std")]
fn to_std(self) -> std::io::ErrorKind {
match self {
$(Self::$kind => std::io::ErrorKind::$kind),*
}
}
#[cfg(feature = "std")]
fn from_std(o: std::io::ErrorKind) -> ErrorKind {
match o {
$(std::io::ErrorKind::$kind => ErrorKind::$kind),*,
_ => ErrorKind::Other
}
}
}
}
}
define_errorkind!(
NotFound,
PermissionDenied,
ConnectionRefused,
ConnectionReset,
ConnectionAborted,
NotConnected,
AddrInUse,
AddrNotAvailable,
BrokenPipe,
AlreadyExists,
WouldBlock,
InvalidInput,
InvalidData,
TimedOut,
WriteZero,
Interrupted,
UnexpectedEof,
Other
);
#[cfg(all(feature = "alloc", not(feature = "std")))]
mod sealed {
use alloc::boxed::Box;
use alloc::string::String;
use core::fmt;
pub trait IntoBoxDynDebug {
fn into(self) -> Box<dyn fmt::Debug + Send + Sync + 'static>;
}
impl IntoBoxDynDebug for &str {
fn into(self) -> Box<dyn fmt::Debug + Send + Sync + 'static> {
Box::new(String::from(self))
}
}
impl IntoBoxDynDebug for String {
fn into(self) -> Box<dyn fmt::Debug + Send + Sync + 'static> { Box::new(self) }
}
}