Skip to main content

IndexVec

Struct IndexVec 

Source
pub struct IndexVec<I: Idx, T> { /* private fields */ }
Expand description

A vector keyed by I instead of by usize.

The whole point is push: it appends and returns the index it assigned, so a producer names a node at the moment it creates one and never recomputes that name. Indexing panics on an out-of-range index the way Vec does, because an index of the wrong domain cannot be constructed in the first place and an index of the right domain that is out of range is a defect in the producer.

PhantomData<fn(I)> keeps I out of drop-checking and out of auto-trait inference: an IndexVec<I, T> is Send/Sync exactly when T is.

Implementations§

Source§

impl<I: Idx, T> IndexVec<I, T>

Source

pub const fn new() -> Self

Returns an empty vector.

Source

pub fn with_capacity(capacity: usize) -> Self

Returns an empty vector with room for capacity elements.

Source

pub const fn from_raw(raw: Vec<T>) -> Self

Adopts raw, naming its positions with I.

Source

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

Returns the underlying storage.

Source

pub fn push(&mut self, value: T) -> I

Appends value and returns the index it was assigned.

Source

pub fn pop(&mut self) -> Option<T>

Removes and returns the last element.

Source

pub fn next_index(&self) -> I

Returns the index the next push will assign.

Source

pub fn len(&self) -> usize

Returns the number of elements.

Source

pub fn is_empty(&self) -> bool

Returns whether there are no elements.

Source

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

Returns the element index names, or None when it is out of range.

Source

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

Returns the element index names mutably, or None when it is out of range.

Source

pub fn iter(&self) -> Iter<'_, T> ⓘ

Returns the elements in position order.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ

Returns the elements in position order, mutably.

Source

pub fn iter_enumerated( &self, ) -> impl DoubleEndedIterator<Item = (I, &T)> + ExactSizeIterator

Returns each element paired with the index naming it.

Source

pub fn iter_enumerated_mut( &mut self, ) -> impl DoubleEndedIterator<Item = (I, &mut T)> + ExactSizeIterator

Returns each element paired with the index naming it, mutably.

Source

pub fn indices(&self) -> impl DoubleEndedIterator<Item = I> + ExactSizeIterator

Returns every index this vector currently names.

Source

pub fn as_slice(&self) -> &[T]

Returns the elements as a plain slice.

Source

pub fn as_mut_slice(&mut self) -> &mut [T]

Returns the elements as a plain mutable slice.

Source

pub fn truncate(&mut self, len: usize)

Shortens the vector to len elements, dropping the rest.

Source

pub fn clear(&mut self)

Drops every element.

Source§

impl<I: Idx, T: Clone> IndexVec<I, T>

Source

pub fn from_elem_n(value: T, count: usize) -> Self

Returns a vector of count clones of value.

Source

pub fn resize(&mut self, len: usize, value: T)

Grows the vector to len elements by cloning value.

Trait Implementations§

Source§

impl<I: Idx, T: Clone> Clone for IndexVec<I, T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<I: Idx, T: Debug> Debug for IndexVec<I, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I: Idx, T> Default for IndexVec<I, T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<I: Idx, T: Eq> Eq for IndexVec<I, T>

Source§

impl<I: Idx, T> FromIterator<T> for IndexVec<I, T>

Source§

fn from_iter<It: IntoIterator<Item = T>>(iter: It) -> Self

Creates a value from an iterator. Read more
Source§

impl<I: Idx, T> Index<I> for IndexVec<I, T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: I) -> &T

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

impl<I: Idx, T> IndexMut<I> for IndexVec<I, T>

Source§

fn index_mut(&mut self, index: I) -> &mut T

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

impl<I: Idx, T> IntoIterator for IndexVec<I, T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, I: Idx, T> IntoIterator for &'a IndexVec<I, T>

Source§

type Item = &'a T

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, I: Idx, T> IntoIterator for &'a mut IndexVec<I, T>

Source§

type Item = &'a mut T

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<I: Idx, T: PartialEq> PartialEq for IndexVec<I, T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl<I, T> Forget for IndexVec<I, T>

§

impl<I, T> Freeze for IndexVec<I, T>
where *const u8 is !null: Freeze, PhantomData<T>: Freeze, PhantomData<fn(I)>: Freeze,

§

impl<I, T> Move for IndexVec<I, T>

§

impl<I, T> RefUnwindSafe for IndexVec<I, T>
where T: RefUnwindSafe,

§

impl<I, T> Send for IndexVec<I, T>
where T: Send, RawVecInner: Send,

§

impl<I, T> Sync for IndexVec<I, T>
where T: Sync, RawVecInner: Sync,

§

impl<I, T> Unpin for IndexVec<I, T>
where T: Unpin, *const u8 is !null: Unpin,

§

impl<I, T> UnsafeUnpin for IndexVec<I, T>

§

impl<I, T> UnwindSafe for IndexVec<I, T>
where RawVecInner: UnwindSafe, 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.