use core::fmt;
use musli::context::StdError;
#[cfg(feature = "alloc")]
use alloc::string::{String, ToString};
pub trait Error: Sized + 'static + Send + Sync + fmt::Display + fmt::Debug {
fn custom<T>(error: T) -> Self
where
T: 'static + Send + Sync + StdError;
fn message<T>(message: T) -> Self
where
T: fmt::Display;
}
#[cfg(all(feature = "std", feature = "alloc"))]
impl Error for std::io::Error {
fn custom<T>(message: T) -> Self
where
T: 'static + Send + Sync + fmt::Display + fmt::Debug,
{
std::io::Error::new(std::io::ErrorKind::Other, message.to_string())
}
fn message<T>(message: T) -> Self
where
T: fmt::Display,
{
std::io::Error::new(std::io::ErrorKind::Other, message.to_string())
}
}
#[cfg(feature = "alloc")]
impl Error for String {
#[inline]
fn custom<T>(message: T) -> Self
where
T: fmt::Display,
{
message.to_string()
}
#[inline]
fn message<T>(message: T) -> Self
where
T: fmt::Display,
{
message.to_string()
}
}