AllocVec

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 allocated at the index
  • 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 allocated at the index
  • None otherwise
Source

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

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

Source

pub fn alloc(&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 alloc_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 dealloc(&mut self, index: usize) -> Option<T>

Deallocates the space at the given index if it is allocated.

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

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 replace
  • item - The item to replace with
§Returns
  • Ok<T> if the space was allocated, where T is the previous value. After this method returns Ok the space at index is to be considered populated with item.
  • Err containing item if the space was unallocated.
Source

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

§Arguments
  • index - The index of the space to check
§Returns
  • Whether an element is allocated at the given index
Source

pub fn len(&self) -> usize

§Returns
  • The total number of allocated elements
Source

pub fn is_empty(&self) -> bool

§Returns
  • True if there are no allocated elements
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_mut().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

Source§

impl<T: Eq> AllocVec<T>

Source

pub fn contains(&self, value: &T) -> bool

Iterates through the vector searching for a slot that’s populated with an item that is equal to the provided value.

Requires T to implement Eq

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.