Struct Arena
struct Arena<T> { ... }
An arena of objects of type T.
Example
use Arena;
let monsters = new;
let vegeta = monsters.alloc;
assert!;
Implementations
impl Arena<u8>
fn alloc_str(self: &Self, s: &str) -> &mut strAllocates a string slice and returns a mutable reference to it.
This is on
Arena<u8>, because string slices use byte slices ([u8]) as their backing storage.Example
use Arena; let arena: = new; let hello = arena.alloc_str; assert_eq!;
impl<T> Arena<T>
fn new() -> Arena<T>Construct a new arena.
Example
use Arena; let arena = new; # arena.alloc;fn with_capacity(n: usize) -> Arena<T>Construct a new arena with capacity for
nvalues pre-allocated.Example
use Arena; let arena = with_capacity; # arena.alloc;fn len(self: &Self) -> usizeReturn the size of the arena
This is useful for using the size of previous typed arenas to build new typed arenas with large enough spaces.
Example
use Arena; let arena = with_capacity; let a = arena.alloc; let b = arena.alloc; assert_eq!;fn alloc(self: &Self, value: T) -> &mut TAllocates a value in the arena, and returns a mutable reference to that value.
Example
use Arena; let arena = new; let x = arena.alloc; assert_eq!;fn alloc_extend<I>(self: &Self, iterable: I) -> &mut [T] where I: IntoIterator<Item = T>Uses the contents of an iterator to allocate values in the arena. Returns a mutable slice that contains these values.
Example
use Arena; let arena = new; let abc = arena.alloc_extend; assert_eq!;unsafe fn alloc_uninitialized(self: &Self, num: usize) -> &mut [MaybeUninit<T>]Allocates space for a given number of values, but doesn't initialize it.
Safety
After calling this method, the arena considers the elements initialized. If you fail to initialize them (which includes because of panicking during the initialization), the arena will run destructors on the uninitialized memory. Therefore, you must initialize them.
Considering how easy it is to cause undefined behaviour using this, you're advised to prefer the other (safe) methods, like [
alloc_extend][Arena::alloc_extend].Example
use ; use ptr; use Arena; // Transmute from MaybeUninit slice to slice of initialized T. // It is a separate function to preserve the lifetime of the reference. unsafe let arena: = new; let slice: &mut ; unsafeAlternative allocation pattern
To avoid the problem of dropping assumed to be initialized elements on panic, it is also possible to combine the [
reserve_extend][Arena::reserve_extend] with [uninitialized_array][Arena::uninitialized_array], initialize the elements and confirm them by this method. In such case, when there's a panic during initialization, the already initialized elements would leak but it wouldn't cause UB.use ; use ptr; use Arena; unsafe const COUNT: usize = 2; let arena: = new; arena.reserve_extend; let slice: &mut ; unsafefn reserve_extend(self: &Self, num: usize)Makes sure there's enough continuous space for at least
numelements.This may save some work if called before [
alloc_extend][Arena::alloc_extend]. It also allows somewhat safer use pattern of [alloc_uninitialized][Arena::alloc_uninitialized]. On the other hand this might waste up ton - 1elements of space. In case new allocation is needed, the unused ones in current chunk are never used.fn uninitialized_array(self: &Self) -> *mut [MaybeUninit<T>]Returns unused space.
This unused space is still not considered "allocated". Therefore, it won't be dropped unless there are further calls to
alloc, [alloc_uninitialized][Arena::alloc_uninitialized], or [alloc_extend][Arena::alloc_extend] which is why the method is safe.It returns a raw pointer to avoid creating multiple mutable references to the same place. It is up to the caller not to dereference it after any of the
alloc_methods are called.fn into_vec(self: Self) -> Vec<T>Convert this
Arenainto aVec<T>.Items in the resulting
Vec<T>appear in the order that they were allocated in.Example
use Arena; let arena = new; arena.alloc; arena.alloc; arena.alloc; let easy_as_123 = arena.into_vec; assert_eq!;fn iter_mut(self: &mut Self) -> IterMut<'_, T>Returns an iterator that allows modifying each value.
Items are yielded in the order that they were allocated.
Example
use Arena; ; let mut arena = new; arena.alloc; arena.alloc; for point in arena.iter_mut let points = arena.into_vec; assert_eq!;Immutable Iteration
Note that there is no corresponding
itermethod. Access to the arena's contents requries mutable access to the arena itself.use typed_arena::Arena; let mut arena = Arena::new(); let x = arena.alloc(1); // borrow error! for i in arena.iter_mut() { println!("i: {}", i); } // borrow error! *x = 2;
impl<T> Any for Arena<T>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Arena<T>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Arena<T>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> Default for Arena<T>
fn default() -> Self
impl<T> Freeze for Arena<T>
impl<T> From for Arena<T>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> RefUnwindSafe for Arena<T>
impl<T> Send for Arena<T>
impl<T> Sync for Arena<T>
impl<T> Unpin for Arena<T>
impl<T> UnsafeUnpin for Arena<T>
impl<T> UnwindSafe for Arena<T>
impl<T, U> Into for Arena<T>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Arena<T>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Arena<T>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>