pub struct Bump { /* private fields */ }Expand description
Multi-chunk bump arena. See the module-level docs.
§Examples
use arena_lib::Bump;
let mut bump = Bump::with_capacity(64);
let a = bump.alloc(7_u32);
let b = bump.alloc(42_u32);
assert_eq!(*a, 7);
assert_eq!(*b, 42);
// Resetting clears the cursor; chunks are retained for reuse.
bump.reset();
assert_eq!(bump.allocated_bytes(), 0);
assert!(bump.chunk_capacity() >= 64);Implementations§
Source§impl Bump
impl Bump
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty bump arena. The first allocation triggers the allocation of an initial chunk (default size 4 KiB).
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a bump arena that pre-allocates an initial chunk of
capacity bytes.
Subsequent chunks (if needed) are sized to at least capacity
bytes, with a floor of 4 KiB.
Sourcepub fn chunk_capacity(&self) -> usize
pub fn chunk_capacity(&self) -> usize
Total bytes reserved across every chunk currently held by the arena.
Sourcepub fn chunk_count(&self) -> usize
pub fn chunk_count(&self) -> usize
Number of chunks currently held.
Starts at 0 for a Bump::new arena, and grows by one each time
an allocation forces a new chunk to be requested from the global
allocator. Bump::reset does not reduce this count.
Sourcepub fn allocated_bytes(&self) -> usize
pub fn allocated_bytes(&self) -> usize
Bytes consumed since the most recent Bump::reset (or since
construction).
Counts the fully-used capacity of every chunk before
current_chunk plus the cursor within current_chunk. Alignment
padding is included.
Sourcepub fn alloc<T>(&self, value: T) -> &mut T
pub fn alloc<T>(&self, value: T) -> &mut T
Allocates value and returns a unique reference to it.
Allocates a new chunk if the current chunk does not have enough
space. Panics only if the global allocator itself fails (same
failure model as Vec::push / Box::new).
§Examples
use arena_lib::Bump;
let bump = Bump::with_capacity(16);
let n = bump.alloc(123_u32);
assert_eq!(*n, 123);Sourcepub fn try_alloc<T>(&self, value: T) -> Result<&mut T>
pub fn try_alloc<T>(&self, value: T) -> Result<&mut T>
Allocates value, returning Ok(&mut T) on success or
Error::CapacityExceeded only if a new chunk could not be
allocated (effectively never, on systems with a working global
allocator).
The returned &mut T borrows from &self because the Bump
owns the underlying chunks and hands out non-overlapping regions
per call — the same pattern used by bumpalo::Bump::alloc.
§Examples
use arena_lib::Bump;
let bump = Bump::with_capacity(16);
let r = bump.try_alloc(0xfeed_u32).expect("global allocator should be live");
assert_eq!(*r, 0xfeed);Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the arena, marking every prior allocation as discarded.
Every chunk is retained — subsequent allocations refill chunk 0
first, then chunk 1, etc., before any new chunk is requested from
the global allocator. Destructors are not run (see the
module-level docs for the Drop policy). Taking &mut self
ensures no outstanding references survive across the call.
§Examples
use arena_lib::Bump;
let mut bump = Bump::with_capacity(64);
let _ = bump.alloc([0_u8; 32]);
let chunks_before = bump.chunk_count();
bump.reset();
assert_eq!(bump.allocated_bytes(), 0);
assert_eq!(bump.chunk_count(), chunks_before, "reset retains chunks");