UniqueIdAllocator

Struct UniqueIdAllocator 

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

Source

pub fn max_used_id(&self) -> Option<T>

Return the maximum currently used id, or None if no ids have been allocated yet.

Source

pub const fn new() -> Self

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

Source

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.

Source

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.

Source

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.

Source

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.

Source

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>

Source§

fn clone(&self) -> UniqueIdAllocator<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + IntegerIdCounter> Debug for UniqueIdAllocator<T>

Source§

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

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

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

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<T> !Freeze for UniqueIdAllocator<T>

§

impl<T> !RefUnwindSafe for UniqueIdAllocator<T>

§

impl<T> Send for UniqueIdAllocator<T>

§

impl<T> !Sync for UniqueIdAllocator<T>

§

impl<T> Unpin for UniqueIdAllocator<T>
where T: Unpin,

§

impl<T> UnwindSafe for UniqueIdAllocator<T>
where T: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.