pub struct UniqueIdAllocator<T: IntegerIdCounter> { /* private fields */ }Expand description
Allocates unique integer ids.
Guarantees that each call to the Self::alloc function will return a unique id,
unless Self::reset is called.
Ids start at IntegerIdCounter::START by default, counting upwards from there.
Implementations§
Source§impl<T: IntegerIdCounter> UniqueIdAllocator<T>
impl<T: IntegerIdCounter> UniqueIdAllocator<T>
Sourcepub fn max_used_id(&self) -> Option<T>
pub fn max_used_id(&self) -> Option<T>
Return the maximum currently used id,
or None if no ids have been allocated yet.
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new allocator,
using T::START as the first id (usually zero).
Sourcepub const fn with_start(start: T) -> Self
pub const fn with_start(start: T) -> Self
Create a new allocator, using the specified value as the first id.
Equivalent to calling Self::new then Self::set_next_id in sequence.
Sourcepub fn alloc(&self) -> T
pub fn alloc(&self) -> T
Attempt to allocate a new id, panicking if none are available.
§Panics
This will panic when the range of the underlying IntegerIdCounter is exhausted.
See Self::try_alloc for a version that returns an error instead.
Sourcepub fn try_alloc(&self) -> Result<T, IdExhaustedError<T>>
pub fn try_alloc(&self) -> Result<T, IdExhaustedError<T>>
Attempt to allocate a new id, returning an error if there are no more available.
§Errors
If the range of the underlying IntegerIdCounter is exhausted,
this will return an error.
Sourcepub fn set_next_id(&self, next_id: T)
pub fn set_next_id(&self, next_id: T)
Set the id that will be returned from the Self::alloc function.
Like a call to Self::reset, this may cause the counter to unexpectedly jump backwards.
It may also cause the counter to jump unexpectedly forwards.
Keep the allocator private if this behavior is undesired.
Sourcepub fn reset(&self)
pub fn reset(&self)
Reset the allocator to a pristine state, beginning allocations all over again.
This is equivalent to running *allocator = UniqueIdAllocator::new(),
but does not require a &mut Self reference.
This may cause unexpected behavior if ids are expected to be monotonically increasing, or if the new ids conflict with ones still in use. To avoid this, keep the id allocator private.
See also the Self::set_next_id function,
which can cause the counter to jump forwards in addition to jumping backwards.
Trait Implementations§
Source§impl<T: Clone + IntegerIdCounter> Clone for UniqueIdAllocator<T>
impl<T: Clone + IntegerIdCounter> Clone for UniqueIdAllocator<T>
Source§fn clone(&self) -> UniqueIdAllocator<T>
fn clone(&self) -> UniqueIdAllocator<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more