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>
impl<T> BumpAlloc<T>
Sourcepub fn alloc(&mut self) -> Option<NonNull<T>>
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());Sourcepub fn free(&mut self, ptr: NonNull<T>)
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);Sourcepub fn allocations(&self) -> Allocations<'_, T>
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);Sourcepub fn free_all(&mut self)
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);Sourcepub fn available_slots(&self) -> usize
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);Sourcepub fn can_alloc(&self) -> bool
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§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more