Skip to main content

sprite_core/
arena.rs

1use bumpalo::Bump;
2
3pub struct Arena {
4    bump: Bump,
5}
6
7impl Arena {
8    pub fn new() -> Self { Self { bump: Bump::new() } }
9    pub fn with_capacity(capacity: usize) -> Self { Self { bump: Bump::with_capacity(capacity) } }
10    /// Reset the arena in ~1-2ns. All allocations are invalidated.
11    pub fn reset(&mut self) { self.bump.reset(); }
12    pub fn alloc<T>(&self, val: T) -> &mut T { self.bump.alloc(val) }
13    pub fn alloc_slice_copy<T: Copy>(&self, slice: &[T]) -> &mut [T] { self.bump.alloc_slice_copy(slice) }
14    pub fn allocated_bytes(&self) -> usize { self.bump.allocated_bytes() }
15}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    #[test]
21    fn alloc_and_reset() {
22        let mut arena = Arena::with_capacity(1024);
23        let _ = arena.alloc(42i64);
24        let before = arena.allocated_bytes();
25        assert!(before > 0);
26        arena.reset();
27        let _ = arena.alloc(99i64);
28        assert!(arena.allocated_bytes() >= before);
29    }
30}