santh-bufpool 0.1.0

Typed buffer recycling with fixed size classes and lock-free checkout/return
Documentation
use std::fmt;

/// Maximum bytes that can be safely requested.
pub const MAX_REQUEST_BYTES: usize = isize::MAX as usize;

/// Result type used by `bufpool`.
pub type Result<T> = std::result::Result<T, Error>;

/// Errors returned by the buffer pool.
#[derive(Debug)]
pub enum Error {
    /// The requested logical slice length is too large to expose safely.
    RequestedLengthTooLarge {
        /// The requested byte count.
        requested: usize,
    },
    /// The requested alignment is not a power of two.
    InvalidAlignment {
        /// The invalid alignment value.
        alignment: usize,
    },
}

impl fmt::Display for Error {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::RequestedLengthTooLarge { requested } => write!(
                formatter,
                "requested buffer length {requested} exceeds the safe slice limit. Fix: request at most {MAX_REQUEST_BYTES} bytes."
            ),
            Self::InvalidAlignment { alignment } => write!(
                formatter,
                "alignment {alignment} is not a power of two. Fix: use an alignment that is a power of two (1, 2, 4, 8, 16, 32, 64, ...)."
            ),
        }
    }
}

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