pub struct AllocVec<T> { /* private fields */ }Expand description
Simple wrapper for Vec that handles allocating / deallocating slots inside the vector, optimized for frequent indexing and frequent allocations / de-allocations.
The index obtained through allocation is guaranteed to remain the same and won’t be reused until deallocated
Implementations§
Source§impl<T> AllocVec<T>
impl<T> AllocVec<T>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates a new AllocVec backed by a Vec with the given capacity
Sourcepub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Constructs a new vector containing all the elements of this
AllocVec that are allocated
Sourcepub fn alloc_cyclic<F>(&mut self, f: F) -> usize
pub fn alloc_cyclic<F>(&mut self, f: F) -> usize
Allocates a space and populates it with an item given by the closure f,
which will receive the allocation index as a parameter. Useful for items that
need to contain information about the index they are allocated in
§Arguments
f- A closure that will receive the index and return the item to populate with
§Returns
- The index of the newly allocated space, equal to the index the closure sees
Sourcepub fn realloc(&mut self, index: usize, item: T) -> Result<T, T>
pub fn realloc(&mut self, index: usize, item: T) -> Result<T, T>
Replaces the element in the space at index, with item.
§Arguments
index- The index of the space whose item to replaceitem- The item to replace with
§Returns
Ok<T>if the space was allocated, whereTis the previous value. After this method returnsOkthe space atindexis to be considered populated withitem.Errcontainingitemif the space was unallocated.
Sourcepub fn is_allocated(&self, index: usize) -> bool
pub fn is_allocated(&self, index: usize) -> bool
Sourcepub fn enumerate(&self) -> impl Iterator<Item = (usize, &T)> + '_
pub fn enumerate(&self) -> impl Iterator<Item = (usize, &T)> + '_
Returns an immutable iterator over the populated spaces that acts in a way similar to
.iter().enumerate() except the index actually represents an allocation
Sourcepub fn enumerate_mut(&mut self) -> impl Iterator<Item = (usize, &mut T)> + '_
pub fn enumerate_mut(&mut self) -> impl Iterator<Item = (usize, &mut T)> + '_
Returns a mutable iterator over the populated spaces that acts in a way similar to
.iter_mut().enumerate() except the index actually represents an allocation
Sourcepub fn iter(&self) -> impl Iterator<Item = &T> + '_
pub fn iter(&self) -> impl Iterator<Item = &T> + '_
Returns an immutable iterator over the populated spaces