GenIndexAllocator

Struct GenIndexAllocator 

Source
pub struct GenIndexAllocator<I: GenIndex = IndexF64> { /* private fields */ }
Expand description

Allocator of generational indices.

Implementations§

Source§

impl<I: GenIndex> GenIndexAllocator<I>

Source

pub fn new() -> Self

Constructs a new GenIndexAllocator.

§Examples
let allocator = <GenIndexAllocator>::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty GenIndexAllocator with the specified capacity. The allocator will be able to hold exactly capacity elements without reallocating. If capacity is 0, it will not allocate.

§Panic

Panics if the capacity overflows.

§Examples
let allocator = <GenIndexAllocator>::with_capacity(10);
assert_eq!(allocator.capacity(), 10);
Source

pub fn len(&self) -> usize

Returns the number of elements in the allocator, also referred to as its ‘length’.

§Examples
let mut allocator = <GenIndexAllocator>::new();
assert_eq!(allocator.len(), 0);
allocator.create();
assert_eq!(allocator.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the allocator contains no elements.

§Examples
let mut allocator = <GenIndexAllocator>::new();
assert!(allocator.is_empty());
allocator.create();
assert!(!allocator.is_empty());
Source

pub fn capacity(&self) -> usize

Returns the number of elements the allocator can hold without reallocating.

§Examples
let allocator = <GenIndexAllocator>::with_capacity(10);
assert_eq!(allocator.capacity(), 10);
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted in the given allocator. The collection may reserve more space to avoid frequent reallocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.

§Panics

Panics if the capacity overflows.

§Examples
let mut allocator = <GenIndexAllocator>::new();
allocator.reserve(10);
assert!(allocator.capacity() >= 10);
Source

pub fn clear(&mut self)

Clears the allocator, removing all values. Note that this method has no effect on the allocated capacity of the allocator.

§Examples
allocator.create();
allocator.create();
allocator.clear();
assert!(allocator.is_empty());
Source

pub fn create(&mut self) -> I

Creates and returns the next index, allocating more capacity if necessary.

§Panics

Panics if the capacity overflows.

§Examples
let mut allocator = <GenIndexAllocator>::new();
let i = &allocator.create();
assert!(allocator.contains(i));
Source

pub fn remove(&mut self, i: &I) -> bool

Removes index i from the allocator if exists. Returns a bool indicating whether the allocator originally contains the index.

§Examples
let mut allocator = <GenIndexAllocator>::new();
let i = &allocator.create();
assert!(allocator.remove(i));
assert!(!allocator.remove(i));
Source

pub fn contains(&self, i: &I) -> bool

Returns true if the allocator contains the index i .

§Examples
let mut allocator = <GenIndexAllocator>::new();
let i = &allocator.create();
assert!(allocator.contains(i));
allocator.remove(i);
assert!(!allocator.contains(i));
Source

pub fn get(&self, idx: &I::Index) -> Option<&I>

Given an index without a generation, get the GenIndex at that slot.

§Examples
let mut allocator = <GenIndexAllocator>::new();
let i = allocator.create();
assert_eq!(i, *allocator.get(&i.index()).unwrap());
Source

pub fn retain(&mut self, f: impl FnMut(&I) -> bool)

Retains only the indices specified by the predicate. In other words, removes all indices such that f(index) returns false.

§Examples
let mut allocator = <GenIndexAllocator>::new();
let idx1 = &allocator.create();
let idx2 = &allocator.create();
allocator.retain(|idx| idx == idx1);
assert!(allocator.contains(idx1));
assert!(!allocator.contains(idx2));
Source

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

Returns an iterator over the allocator.

§Examples
let mut allocator = <GenIndexAllocator>::new();
for i in 0..10 {
    allocator.create();
}

for idx in &allocator {
    println!("{:?}", idx);
}

Trait Implementations§

Source§

impl<I: Clone + GenIndex> Clone for GenIndexAllocator<I>
where I::Index: Clone,

Source§

fn clone(&self) -> GenIndexAllocator<I>

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<I: Debug + GenIndex> Debug for GenIndexAllocator<I>
where I::Index: Debug,

Source§

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

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

impl<I: GenIndex> Default for GenIndexAllocator<I>

Source§

fn default() -> Self

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

impl<'de, I: GenIndex + Deserialize<'de>> Deserialize<'de> for GenIndexAllocator<I>

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a, I: GenIndex> IntoIterator for &'a GenIndexAllocator<I>

Source§

type Item = &'a I

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, I>

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: GenIndex + Serialize> Serialize for GenIndexAllocator<I>

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<I> Freeze for GenIndexAllocator<I>
where <I as GenIndex>::Index: Freeze,

§

impl<I> RefUnwindSafe for GenIndexAllocator<I>

§

impl<I> Send for GenIndexAllocator<I>
where <I as GenIndex>::Index: Send, I: Send,

§

impl<I> Sync for GenIndexAllocator<I>
where <I as GenIndex>::Index: Sync, I: Sync,

§

impl<I> Unpin for GenIndexAllocator<I>
where <I as GenIndex>::Index: Unpin, I: Unpin,

§

impl<I> UnwindSafe for GenIndexAllocator<I>
where <I as GenIndex>::Index: UnwindSafe, I: 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,