Skip to main content

Bump

Struct Bump 

Source
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

Source

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.

Source

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.

Source

pub fn chunk_capacity(&self) -> usize

Total capacity of the underlying chunk, in bytes.

Source

pub fn allocated_bytes(&self) -> usize

Bytes consumed since the most recent Bump::reset (or since construction).

Includes alignment padding.

Source

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

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.

Source

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.

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.