pub struct UniqueIdAllocatorAtomic<T: IntegerIdCounter> { /* private fields */ }Expand description
Allocates unique integer ids across multiple threads.
This is an UniqueIdAllocator that uses atomic instructions,
and so is safe to share across threads.
Implementations§
Source§impl<T: IntegerIdCounter> UniqueIdAllocatorAtomic<T>
impl<T: IntegerIdCounter> UniqueIdAllocatorAtomic<T>
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Create a new allocator,
using T::START as the first id (usually zero).
Sourcepub fn with_start(start: T) -> Self
pub fn with_start(start: T) -> Self
Create a new allocator, using the specified value as the first id.
Use Self::with_start_const if you need a constant function.
Sourcepub const fn with_start_const(start: T) -> Selfwhere
T: NoUninit,
pub const fn with_start_const(start: T) -> Selfwhere
T: NoUninit,
Create a new allocator, using the specified value as the first id.
In order to be usable from a const function,
this requires that T implement the bytemuck::NoUninit trait
and have the same size and representation as T::Int.
If that does not happen, this method will fail to compile with a const panic.
§Safety
This function cannot cause undefined behavior.
Sourcepub fn approx_max_used_id(&self) -> Option<T>
pub fn approx_max_used_id(&self) -> Option<T>
Estimate the maximum currently used id,
or None if no ids have been allocated yet.
Unlike UniqueIdAllocator::max_used_id
this is only an approximation.
This is because other threads may be concurrently allocating a new id,
and the load uses a relaxed ordering.
In the current implementation, this should always be an under-estimate,
since the counter only goes upwards.
However, this should not be relied upon.
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 exhausted.