stack_arena/error.rs
1/// Error type for allocation failures.
2///
3/// This error is returned when an allocation operation fails, typically
4/// due to insufficient memory or other resource constraints.
5///
6/// # Examples
7///
8/// ```
9/// use stack_arena::{BufferArena, Allocator, AllocError};
10/// use std::alloc::Layout;
11///
12/// // Create a BufferArena with a small fixed size
13/// let mut arena = BufferArena::new(16);
14///
15/// // First allocation succeeds
16/// let layout1 = Layout::from_size_align(16, 1).unwrap();
17/// let ptr1 = unsafe { arena.allocate(layout1) }.unwrap();
18///
19/// // Second allocation fails due to insufficient space
20/// let layout2 = Layout::from_size_align(1, 1).unwrap();
21/// let result = unsafe { arena.allocate(layout2) };
22/// assert!(result.is_err());
23/// assert!(matches!(result, Err(AllocError {})));
24/// ```
25///
26/// Note: Unlike `BufferArena`, the `StackArena` will automatically allocate new chunks
27/// when the current chunk is full, so it rarely returns allocation errors.
28///
29/// # Implementation
30///
31/// This error implements the standard `Error` and `Display` traits,
32/// allowing it to be used with error handling utilities like `Result`.
33#[derive(Debug, PartialEq, Eq)]
34pub struct AllocError {}
35
36impl std::fmt::Display for AllocError {
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 write!(f, "memory allocation error")
39 }
40}
41
42impl std::error::Error for AllocError {}