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>
impl<T: IntegerIdCounter> IdAllocator<T>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new allocator, with ids starting at T::START (usually zero).
Sourcepub const fn with_start(start: T) -> Self
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.
Sourcepub fn try_alloc(&mut self) -> Result<T, IdExhaustedError<T>>
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
Sourcepub fn alloc(&mut self) -> T
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.