Struct stable_id::Tec

source ·
pub struct Tec<IndexT, DataT> { /* private fields */ }
Expand description

Short for tombstone-based vector. Inspired by generational-arena, but without the generation stuff.

Features

  • index stability when deleting an element
  • maintain freed list, and is basically free for large structs

Use case: you have compact data that needs to be inserted & deleted while other objects maintain their index-based references.

Don’t use it if:

  • the data are sparse (use a HashMap or Entities instead)
  • you don’t need to remove data (use a Vec with Sequence instead)
use stable_id::Tec;

// use the `derive_more` crate to shortern the list
#[derive(derive_stable_id::StableId, Debug)]
struct Id(u8);
struct Data { field: usize }

let mut storage: Tec<Id, Data> = Default::default();
assert_eq!(storage.alloc(Data {field: 123}), Id(0));
assert_eq!(storage.get(Id(0)).unwrap().field, 123);

Implementations§

source§

impl<IndexT, DataT> Tec<IndexT, DataT>where IndexT: CastUsize + Ord + Copy + Maximum,

source

pub fn with_capacity(capacity: usize) -> Self

source

pub fn len(&self) -> usize

Number of items in this data structure.

source

pub fn is_empty(&self) -> bool

source

pub fn clear(&mut self)

source

pub fn alloc(&mut self, data: DataT) -> IndexT

Allocates an id from the given data. Note: can store at most IndexT::max_value() - 1 elements, because the next free node needs to be count + 1.

source

pub fn remove(&mut self, index: IndexT) -> DataT

Panic if index is invalid

source

pub fn get(&self, index: IndexT) -> Option<&DataT>

source

pub fn get_mut(&mut self, index: IndexT) -> Option<&mut DataT>

source

pub fn iter(&self) -> impl Iterator<Item = &DataT> + DoubleEndedIterator

source

pub fn iter_with_id( &self ) -> impl Iterator<Item = (IndexT, &DataT)> + DoubleEndedIterator

source

pub fn iter_mut( &mut self ) -> impl Iterator<Item = &mut DataT> + DoubleEndedIterator

source

pub fn iter_mut_with_id( &mut self ) -> impl Iterator<Item = (IndexT, &mut DataT)> + DoubleEndedIterator

source

pub fn into_iter_with_id( self ) -> impl Iterator<Item = (IndexT, DataT)> + DoubleEndedIterator

source

pub fn capacity(&self) -> usize

The amount of occupied space in the underlying vec. Note:

self.len() <= self.capacity() == self.vec.len() <= self.vec.capacity()
source

pub fn coalesce<F>(&mut self, f: F)where F: FnMut(IndexT, IndexT),

Coalesce the data by removing the dead slots. Takes a function f(old_id, new_id) that allows you to deal with changes made by the process, i.e. say in your game model, you have an entity which occupied old_id, you would need to change all references to use the new_id. This is intended to be used before saving a game.

Note: this algorithm is O(n lg n) due to the use of binary heap.

source§

impl<IndexT, DataT> Tec<IndexT, DataT>where IndexT: CastUsize + Ord + Copy + Maximum, DataT: Clone,

source

pub fn populate(data: DataT, count: usize) -> Self

Populate count number of items by cloning the given data.

source§

impl<IndexT, DataT> Tec<IndexT, DataT>where IndexT: CastUsize + Ord + Copy + Maximum, DataT: Clone + Default,

source

pub fn populate_defaults(count: usize) -> Self

Populate count number of items with the default value.

source§

impl<IndexT, DataT> Tec<IndexT, DataT>where IndexT: CastUsize + Ord + Copy + Maximum, DataT: Default,

source

pub fn alloc_default(&mut self) -> IndexT

Trait Implementations§

source§

impl<IndexT: Clone, DataT: Clone> Clone for Tec<IndexT, DataT>

source§

fn clone(&self) -> Tec<IndexT, DataT>

Returns a copy 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<IndexT, DataT> Debug for Tec<IndexT, DataT>where IndexT: Debug, DataT: Debug,

source§

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

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

impl<IndexT, DataT> Default for Tec<IndexT, DataT>where IndexT: Maximum,

source§

fn default() -> Self

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

impl<IndexT, DataT> Index<IndexT> for Tec<IndexT, DataT>where IndexT: CastUsize + Ord + Copy + Maximum,

§

type Output = DataT

The returned type after indexing.
source§

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

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

impl<IndexT, DataT> IndexMut<IndexT> for Tec<IndexT, DataT>where IndexT: CastUsize + Ord + Copy + Maximum,

source§

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

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

Auto Trait Implementations§

§

impl<IndexT, DataT> RefUnwindSafe for Tec<IndexT, DataT>where DataT: RefUnwindSafe, IndexT: RefUnwindSafe,

§

impl<IndexT, DataT> Send for Tec<IndexT, DataT>where DataT: Send, IndexT: Send,

§

impl<IndexT, DataT> Sync for Tec<IndexT, DataT>where DataT: Sync, IndexT: Sync,

§

impl<IndexT, DataT> Unpin for Tec<IndexT, DataT>where DataT: Unpin, IndexT: Unpin,

§

impl<IndexT, DataT> UnwindSafe for Tec<IndexT, DataT>where DataT: UnwindSafe, IndexT: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.