use core::{fmt, num::NonZeroUsize};
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
#[non_exhaustive]
pub struct LenError(Option<NonZeroUsize>);
#[cfg(feature = "unstable-error")]
impl core::error::Error for LenError {}
impl fmt::Display for LenError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("source ran over the end of the buffer")?;
if let Some(remaining) = self.0 {
write!(f, ", {remaining} bytes remaining")?;
}
Ok(())
}
}
impl LenError {
pub const fn new() -> Self {
Self(None)
}
pub const fn from_remaining(remaining: usize) -> Self {
Self(NonZeroUsize::new(remaining))
}
pub const fn remaining(&self) -> Option<NonZeroUsize> {
self.0
}
}