rust_dsa

Struct BumpAlloc

Source
pub struct BumpAlloc<T> { /* private fields */ }
Expand description

A bump allocator for type T.

§Example

use rust_dsa::BumpAlloc;

// First, we create a new bump allocator.
let mut arena: BumpAlloc<i32> = BumpAlloc::new(3);

// We can allocate pointers.
let ptr1 = arena.alloc().unwrap();
let ptr2 = arena.alloc().unwrap();
let ptr3 = arena.alloc().unwrap();

// Eventually, we run out of room.
assert_eq!(arena.alloc(), None);
assert_eq!(arena.available_slots(), 0);

// We can free allocations to make more room.
arena.free(ptr1);
arena.free(ptr2);
arena.free(ptr3);
assert!(arena.can_alloc());
assert!(arena.alloc().is_some());
assert!(arena.alloc().is_some());

// We can iterate over existing allocations.
assert_eq!(arena.allocations().count(), 2);

// Finally, we can free everything.
arena.free_all();

Implementations§

Source§

impl<T> BumpAlloc<T>

Source

pub fn new(n: usize) -> BumpAlloc<T>

Creates a new allocator.

§Panics

Panics if the allocation fails or if size_of::<T>() is zero.

Source

pub fn alloc(&mut self) -> Option<NonNull<T>>

Allocates a slot, if one is available.

§Example
use rust_dsa::BumpAlloc;

let mut arena: BumpAlloc<char> = BumpAlloc::new(3);

assert!(arena.alloc().is_some());
assert!(arena.alloc().is_some());
assert!(arena.alloc().is_some());
assert!(arena.alloc().is_none());
Source

pub fn free(&mut self, ptr: NonNull<T>)

Frees a slot.

§Panics

Panics if the pointer was not allocated by this BumpAlloc struct.

§Example
use rust_dsa::BumpAlloc;

let mut arena: BumpAlloc<String> = BumpAlloc::new(10);

let ptr = arena.alloc().unwrap();
assert_eq!(arena.available_slots(), 9);

arena.free(ptr);

assert_eq!(arena.available_slots(), 10);
Source

pub fn allocations(&self) -> Allocations<'_, T>

Returns an interator over the currently allocated pointers.

§Example
use rust_dsa::BumpAlloc;

let mut arena: BumpAlloc<u8> = BumpAlloc::new(100);
arena.alloc();
arena.alloc();
arena.alloc();
assert_eq!(arena.available_slots(), 97);

let allocations: Vec<_> = arena.allocations().collect();
for ptr in allocations {
    arena.free(ptr);
}

assert_eq!(arena.available_slots(), 100);
Source

pub fn free_all(&mut self)

Frees all the currently allocated pointers.

§Example
use rust_dsa::BumpAlloc;

let mut arena: BumpAlloc<i32> = BumpAlloc::new(10);
arena.alloc();
arena.alloc();
arena.alloc();
assert_eq!(arena.available_slots(), 7);

arena.free_all();

assert_eq!(arena.available_slots(), 10);
Source

pub fn available_slots(&self) -> usize

Returns the number of available slots that can be allocated.

§Example
use rust_dsa::BumpAlloc;

let mut arena: BumpAlloc<i32> = BumpAlloc::new(10);
assert_eq!(arena.available_slots(), 10);

arena.alloc();
arena.alloc();
arena.alloc();
assert_eq!(arena.available_slots(), 7);
Source

pub fn can_alloc(&self) -> bool

Returns true if BumpAlloc::alloc will return Some.

§Example
use rust_dsa::BumpAlloc;

let mut arena: BumpAlloc<bool> = BumpAlloc::new(1);
assert!(arena.can_alloc());

arena.alloc();

assert!(!arena.can_alloc());

Trait Implementations§

Source§

impl<T> Drop for BumpAlloc<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BumpAlloc<T>

§

impl<T> RefUnwindSafe for BumpAlloc<T>
where T: RefUnwindSafe,

§

impl<T> !Send for BumpAlloc<T>

§

impl<T> !Sync for BumpAlloc<T>

§

impl<T> Unpin for BumpAlloc<T>

§

impl<T> UnwindSafe for BumpAlloc<T>
where T: RefUnwindSafe,

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.