Struct stable_id::Eids

source ·
pub struct Eids<IndexT>where
    IndexT: Ord,{ /* private fields */ }
Expand description

Stands for Entity Id generator (ids are redeemable). Basically a counter with a B-tree based free “set” (list).

Use case

  • you want to recycle ids due to frequent entity removal
  • you want to use custom data structure but need id management
  • ids start from zero

Example

use stable_id::Eids;

let mut entities: Eids<u8> = Default::default();
let id = entities.claim();
entities.unclaim(id);

See Self::coalesce() if you want to pack ids together, like when you’re trying to tighten up an array and saving it into a database/save file (i.e. when game players are saving their progress).

Implementations§

source§

impl<IndexT> Eids<IndexT>where IndexT: Successor + Predecessor + Clone + Copy + Ord + Maximum,

source

pub fn claim(&mut self) -> IndexT

source

pub fn unclaim(&mut self, val: IndexT)

source

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

Pack up recycled ids from the freed list while you deal with the change through f(old_id, new_id).

use stable_id::Eids;

let mut entities: Eids<u8> = Default::default();
(0..255).for_each(|i| {
    assert_eq!(entities.claim(), i);
});

entities.unclaim(27);
entities.unclaim(15);

// free up 254 to 251, so that coalescing would be 15 & 27 to takeover 249, 250
entities.unclaim(254);
entities.unclaim(252);
entities.unclaim(251);
entities.unclaim(253);

let mut records_old = Vec::new();
let mut records_new = Vec::new();

entities.coalesce(|old_id, new_id| {
    records_old.push(old_id);
    records_new.push(new_id);

    // update all data that reference the old_id and replace them with new_id
});

assert_eq!(records_old, [250,249]); // reclaiming from the last-issued
assert_eq!(records_new, [27,15]); // note: larger ids come first

Trait Implementations§

source§

impl<IndexT> Clone for Eids<IndexT>where IndexT: Ord + Clone,

source§

fn clone(&self) -> Eids<IndexT>

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> Default for Eids<IndexT>where IndexT: Ord + Default,

source§

fn default() -> Eids<IndexT>

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

Auto Trait Implementations§

§

impl<IndexT> RefUnwindSafe for Eids<IndexT>where IndexT: RefUnwindSafe,

§

impl<IndexT> Send for Eids<IndexT>where IndexT: Send,

§

impl<IndexT> Sync for Eids<IndexT>where IndexT: Sync,

§

impl<IndexT> Unpin for Eids<IndexT>where IndexT: Unpin,

§

impl<IndexT> UnwindSafe for Eids<IndexT>where IndexT: UnwindSafe + RefUnwindSafe,

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.