lowlet 0.1.2

Low-latency IPC library using shared memory and lock-free structures
Documentation
use std::fmt;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Error {
    ShmCreate,
    ShmOpen,
    ShmResize,
    Mmap,
    Munmap,
    QueueFull,
    QueueEmpty,
    Alignment,
    OutOfMemory,
    InvalidSize,
    Interrupted,
    ChannelClosed,
}

impl Error {
    #[inline]
    pub const fn code(self) -> &'static str {
        match self {
            Self::ShmCreate => "E_SHM_CREATE",
            Self::ShmOpen => "E_SHM_OPEN",
            Self::ShmResize => "E_SHM_RESIZE",
            Self::Mmap => "E_MMAP",
            Self::Munmap => "E_MUNMAP",
            Self::QueueFull => "E_QUEUE_FULL",
            Self::QueueEmpty => "E_QUEUE_EMPTY",
            Self::Alignment => "E_ALIGN",
            Self::OutOfMemory => "E_OOM",
            Self::InvalidSize => "E_INVALID_SIZE",
            Self::Interrupted => "E_INTERRUPTED",
            Self::ChannelClosed => "E_CHANNEL_CLOSED",
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ShmCreate => write!(f, "failed to create shared memory segment"),
            Self::ShmOpen => write!(f, "failed to open shared memory segment"),
            Self::ShmResize => write!(f, "failed to resize shared memory segment"),
            Self::Mmap => write!(f, "memory mapping failed"),
            Self::Munmap => write!(f, "memory unmapping failed"),
            Self::QueueFull => write!(f, "queue is at capacity"),
            Self::QueueEmpty => write!(f, "queue is empty"),
            Self::Alignment => write!(f, "memory alignment requirement not met"),
            Self::OutOfMemory => write!(f, "allocator exhausted"),
            Self::InvalidSize => write!(f, "size must be a power of two"),
            Self::Interrupted => write!(f, "operation interrupted"),
            Self::ChannelClosed => write!(f, "channel is closed"),
        }
    }
}

impl std::error::Error for Error {}

pub type Result<T> = std::result::Result<T, Error>;