Skip to main content

arena_alligator/
error.rs

1use std::fmt;
2
3/// Allocation failed.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum AllocError {
6    /// All slots are in use.
7    ArenaFull,
8}
9
10impl fmt::Display for AllocError {
11    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
12        match self {
13            AllocError::ArenaFull => write!(f, "arena is full"),
14        }
15    }
16}
17
18impl std::error::Error for AllocError {}
19
20/// Builder configuration error.
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum BuildError {
23    /// `slot_count * aligned_capacity` overflows `usize`.
24    SizeOverflow,
25    /// Alignment is not a power of 2.
26    InvalidAlignment,
27    /// Buddy arena geometry is invalid.
28    InvalidGeometry,
29}
30
31impl fmt::Display for BuildError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            BuildError::SizeOverflow => write!(f, "total arena size overflows usize"),
35            BuildError::InvalidAlignment => write!(f, "alignment must be a power of 2"),
36            BuildError::InvalidGeometry => {
37                write!(
38                    f,
39                    "buddy arena geometry must be a power-of-two multiple of min block size"
40                )
41            }
42        }
43    }
44}
45
46impl std::error::Error for BuildError {}
47
48/// Buffer capacity exceeded.
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct BufferFullError {
51    /// Bytes remaining in the buffer.
52    pub remaining: usize,
53    /// Bytes that were requested.
54    pub requested: usize,
55}
56
57impl fmt::Display for BufferFullError {
58    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59        write!(
60            f,
61            "buffer full: {} bytes requested, {} bytes remaining",
62            self.requested, self.remaining,
63        )
64    }
65}
66
67impl std::error::Error for BufferFullError {}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn alloc_error_display() {
75        let err = AllocError::ArenaFull;
76        assert_eq!(err.to_string(), "arena is full");
77    }
78
79    #[test]
80    fn alloc_error_is_std_error() {
81        let err: Box<dyn std::error::Error> = Box::new(AllocError::ArenaFull);
82        assert_eq!(err.to_string(), "arena is full");
83    }
84
85    #[test]
86    fn build_error_display_variants() {
87        assert_eq!(
88            BuildError::SizeOverflow.to_string(),
89            "total arena size overflows usize"
90        );
91        assert_eq!(
92            BuildError::InvalidAlignment.to_string(),
93            "alignment must be a power of 2"
94        );
95        assert_eq!(
96            BuildError::InvalidGeometry.to_string(),
97            "buddy arena geometry must be a power-of-two multiple of min block size"
98        );
99    }
100
101    #[test]
102    fn build_error_is_std_error() {
103        let err: Box<dyn std::error::Error> = Box::new(BuildError::SizeOverflow);
104        assert!(err.to_string().contains("overflows"));
105    }
106
107    #[test]
108    fn buffer_full_error_display() {
109        let err = BufferFullError {
110            remaining: 10,
111            requested: 50,
112        };
113        assert_eq!(
114            err.to_string(),
115            "buffer full: 50 bytes requested, 10 bytes remaining"
116        );
117    }
118}