Struct AllocVec

Source
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>

Source

pub const fn new() -> Self

Creates a new AllocVec backed by an empty Vec

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new AllocVec backed by a Vec with the given capacity

Source

pub fn get(&self, index: usize) -> Option<&T>

Returns an immutable reference to an element at the given position

§Arguments
  • index - The index of the element
§Returns
  • Some if the element is present, allocated and not reserved
  • None otherwise
Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut T>

Returns a mutable reference to an element at the given position

§Arguments
  • index - The index of the element
§Returns
  • Some if the element is present, allocated and not reserved
  • None otherwise
Source

pub fn into_vec(self) -> Vec<T>

Constructs a new vector containing all the elements of this AllocVec that are allocated and not reserved

Source

pub fn allocate(&mut self, item: T) -> usize

Allocates a space and populates it with item. No guarantees are made about the value of the returned index, except that it will remain valid until deallocated.

§Arguments
  • item - The item to allocate
§Returns
  • The index of the newly allocated item
Source

pub fn allocate_cyclic<F>(&mut self, f: F) -> usize
where F: FnOnce(usize) -> T,

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
Source

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
Source

pub fn deallocate(&mut self, index: usize) -> Option<T>

Deallocates the space at the given index if it is populated or reserved

§Arguments
  • index - The index of the space to deallocate
§Returns
  • Some if the space at the given index was populated
  • None otherwise
Source

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
  • Some if the element was populated
  • None otherwise
Source

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 replace
  • item - The item to replace with
§Returns
  • Ok if the space is populated or reserved, along with an Option that will be Some in case the space was populated, containing the previous value, None if the space was reserved. After this method returns Ok the space at index is to be considered populated with item.
  • Err containing item if the space is unallocated.
Source

pub fn is_allocated(&self, index: usize) -> bool

§Arguments
  • index - The index of the space to check
§Returns
  • Whether the space at the given index is populated or reserved
Source

pub fn is_populated(&self, index: usize) -> bool

§Arguments
  • index - The index of the space to check
§Returns
  • Whether the space at the given index is populated and not reserved
Source

pub fn is_reserved(&self, index: usize) -> bool

§Arguments
  • index - The index of the space to check
§Returns
  • Whether the space at the given index is reserved and not populated
Source

pub fn len(&self) -> usize

§Returns
  • The total number of elements that are either reserved or populated
Source

pub fn vec_len(&self) -> usize

§Returns
  • The length of the underlying Vec
Source

pub fn capacity(&self) -> usize

§Returns
  • The capacity of the underlying Vec
Source

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

Source

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

Source

pub fn iter(&self) -> impl Iterator<Item = &T> + '_

Returns an immutable iterator over the populated spaces

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T> + '_

Returns a mutable iterator over the populated spaces

Trait Implementations§

Source§

impl<T: Clone> Clone for AllocVec<T>

Source§

fn clone(&self) -> AllocVec<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> From<Vec<T>> for AllocVec<T>

Source§

fn from(value: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> Index<usize> for AllocVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<usize> for AllocVec<T>

Source§

fn index_mut(&mut self, index: usize) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for AllocVec<T>

§

impl<T> RefUnwindSafe for AllocVec<T>
where T: RefUnwindSafe,

§

impl<T> Send for AllocVec<T>
where T: Send,

§

impl<T> Sync for AllocVec<T>
where T: Sync,

§

impl<T> Unpin for AllocVec<T>
where T: Unpin,

§

impl<T> UnwindSafe for AllocVec<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.