StableVec

Struct StableVec 

Source
pub struct StableVec<T> { /* private fields */ }
Expand description

A Vec<T>-like collection that maintains stable indices for its elements.

Unlike with a standard Vec<T>, removing elements from a StableVec<T> is O(1), and it does not move other elements or invalidate their indices. This is achieved by internally storing each element as an Option<T>, and reusing freed slots for new elements.

§Overiew of Important Methods

  • push adds a new element and returns its stable index (O(1)).
  • remove removes an element at a given index and returns it (O(1)).
  • try_remove attempts to remove an element at a given index, returning None if it doesn’t exist (O(1)).
  • get and get_mut provide access to elements by index (O(1)).
  • clear removes all elements without deallocating memory (O(1)).

Implementations§

Source§

impl<T> StableVec<T>

Source

pub const fn new() -> Self

Creates a new empty StableVec.

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new StableVec with the given initial capacity.

This is useful for preallocating space to avoid reallocations.

Source

pub fn push(&mut self, element: T) -> usize

Pushes a new element to the first available slot, returning its index.

This may reuse a previously freed slot.

§Time Complexity

O(1)

Source

pub fn next_push_index(&self) -> usize

Returns the next index that will be used for a push.

This index may reuse a previously freed slot.

§Time Complexity

O(1)

Source

pub fn remove(&mut self, index: usize) -> T

Removes the element at the given index, returning it.

§Time Complexity

O(1)

§Panics

Panics if there is no element at the given index.

Source

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

Tries to remove the element at the given index, returning it if it existed.

§Time Complexity

O(1)

Source

pub fn clear(&mut self)

Removes all elements from the StableVec.

No memory is deallocated, so the capacity remains the same.

§Time Complexity

O(1)

Source

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

Returns a reference to the element at the given index, if it exists.

§Time Complexity

O(1)

Source

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

Returns a mutable reference to the element at the given index, if it exists.

§Time Complexity

O(1)

Source

pub unsafe fn get_unchecked(&self, index: usize) -> &T

Returns a reference to the element at the given index without bounds checking.

§Time Complexity

O(1)

§Safety

The caller must ensure that the index is in bounds and that there is an element at that index.

Source

pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T

Returns a mutable reference to the element at the given index without bounds checking.

§Time Complexity

O(1)

§Safety

The caller must ensure that the index is in bounds and that there is an element at that index.

Source

pub fn get_disjoint_mut2( &mut self, index1: usize, index2: usize, ) -> Option<[&mut T; 2]>

Returns mutable references to two disjoint elements at the given indices, if they exist.

If the indices are the same, or if either index is out of bounds or has no element, None is returned.

§Time Complexity

O(1)

Source

pub unsafe fn get_disjoint_mut_unchecked( &mut self, index1: usize, index2: usize, ) -> [&mut T; 2]

Returns mutable references to two disjoint elements at the given indices without bounds checking.

If the indices are the same, or if either index has no element, the behavior is undefined.

§Time Complexity

O(1)

§Safety

The caller must ensure that the indices are disjoint and that there are elements at both indices.

Source

pub fn len(&self) -> usize

Returns the number of elements in the StableVec.

§Time Complexity

O(1)

Source

pub fn is_empty(&self) -> bool

Returns true if the StableVec is empty.

§Time Complexity

O(1)

Source

pub fn capacity(&self) -> usize

Returns the capacity of the StableVec.

§Time Complexity

O(1)

Source

pub fn iter(&self) -> impl Iterator<Item = (usize, &T)>

Returns an iterator over the elements and indices of the StableVec.

Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (usize, &mut T)>

Returns a mutable iterator over the elements and indices of the StableVec.

Trait Implementations§

Source§

impl<T: Clone> Clone for StableVec<T>

Source§

fn clone(&self) -> StableVec<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: Debug> Debug for StableVec<T>

Source§

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

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

impl<T> Default for StableVec<T>

Source§

fn default() -> Self

Creates a new empty StableVec.

Source§

impl<T> Index<usize> for StableVec<T>

Source§

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

Returns a reference to the element at the given index.

§Time Complexity

O(1)

§Panics

Panics if there is no element at the given index.

Source§

type Output = T

The returned type after indexing.
Source§

impl<T> IndexMut<usize> for StableVec<T>

Source§

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

Returns a mutable reference to the element at the given index.

§Time Complexity

O(1)

§Panics

Panics if there is no element at the given index.

Source§

impl<T> IntoIterator for StableVec<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = StableVecIntoIterator<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

Auto Trait Implementations§

§

impl<T> Freeze for StableVec<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for StableVec<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, U> AsBindGroupShaderType<U> for T
where U: ShaderType, &'a T: for<'a> Into<U>,

Source§

fn as_bind_group_shader_type(&self, _images: &RenderAssets<GpuImage>) -> U

Return the T ShaderType for self. When used in AsBindGroup derives, it is safe to assume that all images in self exist.
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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_sync(self: Box<T>) -> Box<dyn Any + Send + Sync>

Converts Box<Trait> (where Trait: DowncastSync) to Box<dyn Any + Send + Sync>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

Converts Arc<Trait> (where Trait: DowncastSync) to Arc<Any>, which can then be downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromWorld for T
where T: Default,

Source§

fn from_world(_world: &mut World) -> T

Creates Self using default().

Source§

impl<T, W> HasTypeWitness<W> for T
where W: MakeTypeWitness<Arg = T>, T: ?Sized,

Source§

const WITNESS: W = W::MAKE

A constant of the type witness
Source§

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

Source§

const TYPE_EQ: TypeEq<T, <T as Identity>::Type> = TypeEq::NEW

Proof that Self is the same type as Self::Type, provides methods for casting between Self and Self::Type.
Source§

type Type = T

The same type as Self, used to emulate type equality bounds (T == U) with associated type equality constraints (T: Identity<Type = U>).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoResult<T> for T

Source§

fn into_result(self) -> Result<T, RunSystemError>

Converts this type into the system output type.
Source§

impl<A> Is for A
where A: Any,

Source§

fn is<T>() -> bool
where T: Any,

Checks if the current type “is” another type, using a TypeId equality comparison. This is most useful in the context of generic logic. Read more
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
Source§

impl<T> TypeData for T
where T: 'static + Send + Sync + Clone,

Source§

fn clone_type_data(&self) -> Box<dyn TypeData>

Creates a type-erased clone of this value.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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

Source§

impl<T> Settings for T
where T: 'static + Send + Sync,

Source§

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

Source§

impl<T> WasmNotSendSync for T

Source§

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