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 and not reserved
Sourcepub fn allocate_cyclic<F>(&mut self, f: F) -> usize
pub fn allocate_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 reserve(&mut self) -> usize
pub fn reserve(&mut self) -> usize
Reserves a space. No guarantees are made about the value of the returned index, except that it will remain valid until deallocated.
§Returns
- The index of the newly reserved space
Sourcepub fn deallocate(&mut self, index: usize) -> Option<T>
pub fn deallocate(&mut self, index: usize) -> Option<T>
Sourcepub fn take(&mut self, index: usize) -> Option<T>
pub fn take(&mut self, index: usize) -> Option<T>
Takes an element out of the space at the given index, possibly downgrading it
from populated to just reserved. If this method returns Some, then the space
at the given index is to be considered reserved. If None is returned instead,
then it might have already been reserved or even unallocated
§Arguments
index- The index of the space whose item is to take
§Returns
Someif the element was populatedNoneotherwise
Sourcepub fn replace(&mut self, index: usize, item: T) -> Result<Option<T>, T>
pub fn replace(&mut self, index: usize, item: T) -> Result<Option<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
Okif the space is populated or reserved, along with anOptionthat will beSomein case the space was populated, containing the previous value,Noneif the space was reserved. After this method returnsOkthe space atindexis to be considered populated withitem.Errcontainingitemif the space is unallocated.
Sourcepub fn is_allocated(&self, index: usize) -> bool
pub fn is_allocated(&self, index: usize) -> bool
Sourcepub fn is_populated(&self, index: usize) -> bool
pub fn is_populated(&self, index: usize) -> bool
Sourcepub fn is_reserved(&self, index: usize) -> bool
pub fn is_reserved(&self, index: usize) -> bool
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
§Returns
- The total number of elements that are either reserved or populated
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().enumerate() except the index actually represents an allocation