use std::fmt;
use std::io;
#[derive(Debug)]
pub enum Error {
Closed,
InvalidCapacity,
HeaderMismatch,
StorageTooSmall,
Eof,
Timeout,
Io(io::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Closed => write!(f, "shmring: ring buffer closed"),
Error::InvalidCapacity => {
write!(f, "shmring: capacity must be a positive power of two")
}
Error::HeaderMismatch => write!(
f,
"shmring: storage header does not match expected ring buffer format"
),
Error::StorageTooSmall => {
write!(
f,
"shmring: storage is too small for the requested capacity"
)
}
Error::Eof => write!(f, "shmring: end of stream"),
Error::Timeout => write!(f, "shmring: deadline exceeded"),
Error::Io(e) => write!(f, "shmring: {e}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
pub type Result<T> = std::result::Result<T, Error>;