pub struct IdGenerator<T> { /* private fields */ }Expand description
Thread-safe ID allocator.
The counter starts at 1 so every allocated ID is a valid NonZeroU32.
Zero is never handed out: it is the niche value that makes Option<Id>
free (4 bytes, not 8). Allocation panics only after exhausting the full
u32 space (>4 billion ids), which is treated as a programmer limit.
The counter uses Relaxed ordering: uniqueness needs the atomicity of the
read-modify-write, not an ordering edge to any other memory location.
Implementations§
Source§impl<T> IdGenerator<T>
impl<T> IdGenerator<T>
pub fn new() -> Self
pub fn with_start(start: u32) -> Self
pub fn next(&self) -> Twhere
T: From<NonZeroU32>,
Sourcepub fn reserve(&self, count: u32) -> u32
pub fn reserve(&self, count: u32) -> u32
Reserve count contiguous identifiers and return the first slot.
A caller that numbers a whole unit of work reserves one block, so the block order follows the caller’s order and never the thread timing. An empty reservation returns the next slot without consuming it.
Trait Implementations§
Source§impl<T: Debug> Debug for IdGenerator<T>
impl<T: Debug> Debug for IdGenerator<T>
Auto Trait Implementations§
impl<T> !Freeze for IdGenerator<T>
impl<T> RefUnwindSafe for IdGenerator<T>where
PhantomData<T>: RefUnwindSafe,
impl<T> Send for IdGenerator<T>where
PhantomData<T>: Send,
impl<T> Sync for IdGenerator<T>where
PhantomData<T>: Sync,
impl<T> Unpin for IdGenerator<T>where
PhantomData<T>: Unpin,
impl<T> UnsafeUnpin for IdGenerator<T>where
PhantomData<T>: UnsafeUnpin,
impl<T> UnwindSafe for IdGenerator<T>where
PhantomData<T>: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more