use std::error::Error;
use std::{fmt, io};
pub enum CastError<S> {
Mismatch(S),
Io(io::Error, S),
}
impl<S> CastError<S> {
#[inline]
pub fn into_slice(self) -> S {
match self {
CastError::Mismatch(s) | CastError::Io(_, s) => s,
}
}
#[inline]
pub fn io(&self) -> Option<&io::Error> {
match self {
CastError::Io(e, _) => Some(e),
CastError::Mismatch(_) => None,
}
}
}
impl<S> fmt::Debug for CastError<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CastError::Mismatch(_) => f.write_str("CastError::Mismatch(..)"),
CastError::Io(e, _) => f.debug_tuple("CastError::Io").field(e).finish(),
}
}
}
impl<S> fmt::Display for CastError<S> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
CastError::Mismatch(_) => {
f.write_str("bstack_cast!: block tag/size is not the target type")
}
CastError::Io(e, _) => fmt::Display::fmt(e, f),
}
}
}
impl<S> Error for CastError<S> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
CastError::Io(e, _) => Some(e),
CastError::Mismatch(_) => None,
}
}
}