Struct UniqueIdAllocatorAtomic

Source
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>

Source

pub const fn new() -> Self

Create a new allocator, using T::START as the first id (usually zero).

Source

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.

Source

pub const fn with_start_const(start: T) -> Self
where 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.

Source

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.

Source

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

Attempt to allocate a new id, returning an error if exhausted.

Source

pub fn alloc(&self) -> T

Attempt to allocate a new id, panicking if exhausted.

Trait Implementations§

Source§

impl<T: Debug + IntegerIdCounter> Debug for UniqueIdAllocatorAtomic<T>
where T::Int: Debug,

Source§

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

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

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

Source§

fn default() -> Self

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

Auto Trait Implementations§

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.