use core::alloc::LayoutError;
use core::convert::Infallible;
use core::fmt;
use crate::alloc::AllocError;
#[derive(Debug)]
pub enum CustomError<E> {
Custom(E),
Error(Error),
}
impl<E> From<Error> for CustomError<E> {
fn from(error: Error) -> Self {
CustomError::Error(error)
}
}
impl<E> From<AllocError> for CustomError<E> {
fn from(error: AllocError) -> Self {
CustomError::Error(Error::from(error))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
#[doc(hidden)]
CapacityOverflow,
#[doc(hidden)]
LayoutError,
#[doc(hidden)]
FormatError,
#[doc(hidden)]
AllocError {
error: AllocError,
},
}
impl From<AllocError> for Error {
#[inline]
fn from(error: AllocError) -> Self {
Error::AllocError { error }
}
}
impl From<Infallible> for Error {
#[inline(always)]
fn from(value: Infallible) -> Self {
match value {}
}
}
impl From<LayoutError> for Error {
#[inline]
fn from(_: LayoutError) -> Self {
Error::LayoutError
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::CapacityOverflow => write!(f, "Capacity overflow"),
Error::LayoutError => write!(f, "Layout error"),
Error::FormatError => write!(f, "Format error"),
Error::AllocError { error } => error.fmt(f),
}
}
}
impl core::error::Error for Error {}