pub struct Arena { /* private fields */ }Expand description
A bump-allocated region of memory.
Values allocated in an Arena are tied to its lifetime and must not
outlive it. The public API enforces this with borrow checker lifetimes.
§Type safety note
Arena does not run destructors. Only store Copy or otherwise
non-owning values until drop support is added.
§Example
use ocas_core::arena::Arena;
let arena = Arena::new();
let value = arena.allocate_with(|| 42);
assert_eq!(*value, 42);Implementations§
Source§impl Arena
impl Arena
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new arena with the default block size.
§Example
use ocas_core::arena::Arena;
let arena = Arena::new();
let n = arena.allocate_with(|| 7);
assert_eq!(*n, 7);Sourcepub fn with_capacity(block_size: usize) -> Self
pub fn with_capacity(block_size: usize) -> Self
Create a new arena with a custom initial block size.
Sourcepub fn allocate_with<T>(&self, init: impl FnOnce() -> T) -> &mut T
pub fn allocate_with<T>(&self, init: impl FnOnce() -> T) -> &mut T
Allocate a value in the arena, constructing it inside init, and return
a mutable reference tied to self.
The closure form avoids any ambiguity about when mutation of the arena
occurs. The returned &mut T is unique because alloc_raw advances the
arena offset for each allocation via interior mutability.
§Panics
Panics if the requested layout has size zero.
§Example
use ocas_core::arena::Arena;
let arena = Arena::new();
let value = arena.allocate_with(|| "hello");
assert_eq!(*value, "hello");Sourcepub fn allocate_slice<T: Copy>(&self, values: &[T]) -> &[T]
pub fn allocate_slice<T: Copy>(&self, values: &[T]) -> &[T]
Allocate a contiguous slice of T values in the arena.
The returned slice is tied to the arena lifetime. Because the arena does
not run destructors, T must be Copy so that dropping the arena does
not leak resources owned by the slice elements.
§Panics
Panics if T has zero size or if the total allocation size overflows.
§Example
use ocas_core::arena::Arena;
let arena = Arena::new();
let slice = arena.allocate_slice(&[1, 2, 3, 4, 5]);
assert_eq!(slice, &[1, 2, 3, 4, 5]);Sourcepub fn reset(&self)
pub fn reset(&self)
Reset the arena, invalidating all previously allocated values.
The first chunk is kept and reused; additional chunks are released.
This makes repeated build–reset cycles allocation-free in the steady
state, which is the basis of the workspace pool in ocas-atom.
§Safety contract (enforced by convention)
Any reference returned by allocate_with or
allocate_slice before the reset must not
be used afterwards — the memory may be handed out again for different
values. Callers must treat reset as the end of a generation.
§Example
use ocas_core::arena::Arena;
let arena = Arena::new();
let _ = arena.allocate_with(|| 1);
arena.reset();
let value = arena.allocate_with(|| 2);
assert_eq!(*value, 2);Sourcepub fn chunk_count(&self) -> usize
pub fn chunk_count(&self) -> usize
Return the number of chunks currently held by the arena.
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Arena
impl !RefUnwindSafe for Arena
impl !Send for Arena
impl !Sync for Arena
impl Unpin for Arena
impl UnsafeUnpin for Arena
impl UnwindSafe for Arena
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more