Skip to main content

Bump

Struct Bump 

Source
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

Source

pub fn new() -> Self

Creates an empty bump arena. The first allocation triggers the allocation of an initial chunk (default size 4 KiB).

Source

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.

Source

pub fn chunk_capacity(&self) -> usize

Total bytes reserved across every chunk currently held by the arena.

Source

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.

Source

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.

Source

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);
Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for Bump

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Bump

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for Bump

§

impl !RefUnwindSafe for Bump

§

impl Send for Bump

§

impl !Sync for Bump

§

impl Unpin for Bump

§

impl UnsafeUnpin for Bump

§

impl UnwindSafe for Bump

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.