pub struct Arena<T> { /* private fields */ }
Expand description
Container that can have elements inserted into it and removed from it.
Indices use the Index
type, created by inserting values with Arena::insert
.
Implementations§
Source§impl<T> Arena<T>
impl<T> Arena<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Construct an empty arena with space to hold exactly capacity
elements
without reallocating.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Return the number of elements the arena can hold without allocating, including the elements currently in the arena.
Sourcepub fn insert(&mut self, value: T) -> Index
pub fn insert(&mut self, value: T) -> Index
Insert a new value into the arena, returning an index that can be used to later retrieve the value.
Sourcepub fn insert_at(&mut self, index: Index, value: T) -> Option<T>
pub fn insert_at(&mut self, index: Index, value: T) -> Option<T>
Insert a new value at a given index, returning the old value if present. The entry’s generation is set to the given index’s generation.
§Caveats
This method is capable of “resurrecting” an old Index
. This is unavoidable; if we already
have an occupied entry (or had) at this index of some generation M, and then insert_at
that same slot but with a generation N < M, eventually after some number of insertions and
removals it is possible we could end up with an index matching that old index. There are few
cases where this is likely to be a problem, but it is still possible.
Sourcepub fn insert_at_slot(&mut self, slot: u32, value: T) -> (Index, Option<T>)
pub fn insert_at_slot(&mut self, slot: u32, value: T) -> (Index, Option<T>)
Insert a new value at a given slot, returning the old value if present. If the slot is already occupied, this will increment the generation of the slot, and invalidate any previous indices pointing to it.
Sourcepub fn contains(&self, index: Index) -> bool
pub fn contains(&self, index: Index) -> bool
Returns true if the given index is valid for the arena.
Sourcepub fn contains_slot(&self, slot: u32) -> Option<Index>
pub fn contains_slot(&self, slot: u32) -> Option<Index>
Checks to see whether a slot is occupied in the arena, and if it is,
returns Some
with the true Index
of that slot (slot plus generation.)
Otherwise, returns None
.
Sourcepub fn get(&self, index: Index) -> Option<&T>
pub fn get(&self, index: Index) -> Option<&T>
Get an immutable reference to a value inside the arena by
Index
, returning None
if the index is not contained in the arena.
Sourcepub fn get_mut(&mut self, index: Index) -> Option<&mut T>
pub fn get_mut(&mut self, index: Index) -> Option<&mut T>
Get a mutable reference to a value inside the arena by Index
,
returning None
if the index is not contained in the arena.
Sourcepub fn get2_mut(
&mut self,
index1: Index,
index2: Index,
) -> (Option<&mut T>, Option<&mut T>)
pub fn get2_mut( &mut self, index1: Index, index2: Index, ) -> (Option<&mut T>, Option<&mut T>)
Sourcepub fn remove(&mut self, index: Index) -> Option<T>
pub fn remove(&mut self, index: Index) -> Option<T>
Remove the value contained at the given index from the arena, returning it if it was present.
Sourcepub fn invalidate(&mut self, index: Index) -> Option<Index>
pub fn invalidate(&mut self, index: Index) -> Option<Index>
Invalidate the given index and return a new index to the same value. This
is roughly equivalent to remove
followed by insert
, but much faster.
If the old index is already invalid, this method returns None
.
Sourcepub fn get_by_slot(&self, slot: u32) -> Option<(Index, &T)>
pub fn get_by_slot(&self, slot: u32) -> Option<(Index, &T)>
Attempt to look up the given slot in the arena, disregarding any generational
information, and retrieve an immutable reference to it. Returns None
if the
slot is empty.
Sourcepub fn get_by_slot_mut(&mut self, slot: u32) -> Option<(Index, &mut T)>
pub fn get_by_slot_mut(&mut self, slot: u32) -> Option<(Index, &mut T)>
Attempt to look up the given slot in the arena, disregarding any generational
information, and retrieve a mutable reference to it. Returns None
if the
slot is empty.
Sourcepub fn remove_by_slot(&mut self, slot: u32) -> Option<(Index, T)>
pub fn remove_by_slot(&mut self, slot: u32) -> Option<(Index, T)>
Remove an entry in the arena by its slot, disregarding any generational info.
Returns None
if the slot was already empty.
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Iterate over all of the indexes and values contained in the arena.
Iteration order is not defined.
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
Iterate over all of the indexes and values contained in the arena, with mutable access to each value.
Iteration order is not defined.
Sourcepub fn drain(&mut self) -> Drain<'_, T> ⓘ
pub fn drain(&mut self) -> Drain<'_, T> ⓘ
Returns an iterator that removes each element from the arena.
Iteration order is not defined.
If the iterator is dropped before it is fully consumed, any uniterated items will be dropped from the arena, and the arena will be empty. The arena’s capacity will not be changed.