pub struct Bump { /* private fields */ }Expand description
Single-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 offset; capacity is retained.
bump.reset();
assert_eq!(bump.allocated_bytes(), 0);Implementations§
Source§impl Bump
impl Bump
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty bump arena that can satisfy only zero-sized allocations.
Use Bump::with_capacity for any non-trivial use.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a bump arena backed by a fixed-size chunk of capacity bytes.
capacity is the upper bound on the byte footprint of every
allocation made against this arena before Bump::reset is called.
Account for alignment padding in addition to raw value size.
Sourcepub fn chunk_capacity(&self) -> usize
pub fn chunk_capacity(&self) -> usize
Total capacity of the underlying chunk, in bytes.
Sourcepub fn allocated_bytes(&self) -> usize
pub fn allocated_bytes(&self) -> usize
Bytes consumed since the most recent Bump::reset (or since
construction).
Includes alignment padding.
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.
Panics if the chunk does not have room for value (after alignment
padding). Use Bump::try_alloc for an explicit fallible variant.
§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 if the chunk is full.
The returned &mut T borrows from &self because the Bump
owns the underlying chunk and hands out non-overlapping regions
per call — the same pattern used by bumpalo::Bump::alloc.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the arena, marking every prior allocation as discarded.
Capacity is retained. Destructors of previously allocated values are
not run (see the module-level docs for the Drop policy).
Taking &mut self ensures no outstanding references survive across
the call.