Skip to main content

santh_bufpool/
error.rs

1use std::fmt;
2
3/// Maximum bytes that can be safely requested.
4pub const MAX_REQUEST_BYTES: usize = isize::MAX as usize;
5
6/// Result type used by `bufpool`.
7pub type Result<T> = std::result::Result<T, Error>;
8
9/// Errors returned by the buffer pool.
10#[derive(Debug)]
11pub enum Error {
12    /// The requested logical slice length is too large to expose safely.
13    RequestedLengthTooLarge {
14        /// The requested byte count.
15        requested: usize,
16    },
17    /// The requested alignment is not a power of two.
18    InvalidAlignment {
19        /// The invalid alignment value.
20        alignment: usize,
21    },
22}
23
24impl fmt::Display for Error {
25    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
26        match self {
27            Self::RequestedLengthTooLarge { requested } => write!(
28                formatter,
29                "requested buffer length {requested} exceeds the safe slice limit. Fix: request at most {MAX_REQUEST_BYTES} bytes."
30            ),
31            Self::InvalidAlignment { alignment } => write!(
32                formatter,
33                "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, ...)."
34            ),
35        }
36    }
37}
38
39impl std::error::Error for Error {}