IdAllocator

Struct IdAllocator 

Source
pub struct IdAllocator<T: IntegerIdCounter> { /* private fields */ }
Expand description

A type that allocates integer ids, with the ability to free unused ids back to storage.

This will minimize the integer value of the keys, reducing memory needed for lookup tables and bitsets. It is useful in conjunction with the “direct” maps/sets of the idmap crate.

If the ability to free unused ids is not necessary, consider crate::UniqueIdAllocator or crate::UniqueIdAllocatorAtomic. These are more efficient and do not require an allocator.

There is not any way to iterate over all currently allocated ids. With the current implementation (a BinaryHeap), it would be difficult to implement without any allocation.

Implementations§

Source§

impl<T: IntegerIdCounter> IdAllocator<T>

Source

pub const fn new() -> Self

Create a new allocator, with ids starting at T::START (usually zero).

Source

pub const fn with_start(start: T) -> Self

Create a new allocator, with ids starting at the specified value.

This function is a const fn on all rust versions since 1.80. Before that release, it can only be called at runtime.

Source

pub fn try_alloc(&mut self) -> Result<T, IdExhaustedError<T>>

Allocate a new id, reusing freed ids wherever possible.

§Errors

If no more ids are available, this will return an error. This can only happen if the entire range of the IntegerIdCounter has been , and none have been fred

Source

pub fn alloc(&mut self) -> T

Allocate a new id, reusing freed ids wherever possible.

§Panics

If there are no ids available, this will panic. See Self::try_alloc for a version that returns an error instead.

Source

pub fn free_all(&mut self)

Free all existing ids, resetting the allocator.

Source

pub fn free(&mut self, id: T)

Free the specified id, making it available

Used ids will be used in preference to creating new ones.

Trait Implementations§

Source§

impl<T: IntegerIdCounter> Default for IdAllocator<T>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for IdAllocator<T>

§

impl<T> !RefUnwindSafe for IdAllocator<T>

§

impl<T> Send for IdAllocator<T>

§

impl<T> !Sync for IdAllocator<T>

§

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

§

impl<T> UnwindSafe for IdAllocator<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> 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> 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, 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.