BasicUlidGenerator

Struct BasicUlidGenerator 

Source
pub struct BasicUlidGenerator<ID, T, R>
where ID: UlidId, T: TimeSource<ID::Ty>, R: RandSource<ID::Ty>,
{ /* private fields */ }
Available on crate features ulid and basic only.
Expand description

A non-monotonic ULID-style ID generator suitable for multi-threaded environments.

This generator is lightweight and fast, but has a higher collision probabiliy than it’s monotonic counterpart.

§Features

  • ✅ Thread-safe
  • ✅ Probabilistically unique (no coordination required)
  • ✅ Time-ordered (not monotonically increasing, random per millisecond)
  • You’re in a single or multi-threaded environment
  • You require purely random IDs (even within the same millisecond)

§See Also

Implementations§

Source§

impl<ID, T, R> BasicUlidGenerator<ID, T, R>
where ID: UlidId, T: TimeSource<ID::Ty>, R: RandSource<ID::Ty>,

Source

pub const fn new(time: T, rng: R) -> Self

Available on crate features snowflake or ulid only.

Creates a new BasicUlidGenerator with the provided time source and RNG.

§Parameters
  • time: A TimeSource used to retrieve the current timestamp
  • rng: A RandSource used to generate random bits
§Returns

A ready-to-use ULID generator suitable for producing unique, sortable IDs.

§Example
use ferroid::{BasicUlidGenerator, IdGenStatus, ULID, MonotonicClock, ThreadRandom};

let generator = BasicUlidGenerator::new(MonotonicClock::default(), ThreadRandom::default());

let id: ULID = loop {
    match generator.next_id() {
        IdGenStatus::Ready { id } => break id,
        IdGenStatus::Pending { .. } => core::hint::spin_loop(),
    }
};
Source

pub fn next_id(&self) -> IdGenStatus<ID>

Available on crate features snowflake or ulid only.

Generates a new ULID.

Internally calls Self::try_next_id and unwraps the result. This method will panic on error, so prefer the fallible version if you want explicit control over error handling.

§Panics

This method currently has no fallible code paths, but may panic if an internal error occurs in future implementations. For explicitly fallible behavior, use Self::try_next_id instead.

§Example
use ferroid::{BasicUlidGenerator, IdGenStatus, ULID, MonotonicClock, ThreadRandom};

let generator = BasicUlidGenerator::new(MonotonicClock::default(), ThreadRandom::default());

let id: ULID = loop {
    match generator.next_id() {
        IdGenStatus::Ready { id } => break id,
        IdGenStatus::Pending { .. } => std::thread::yield_now(),
    }
};
Source

pub fn try_next_id(&self) -> Result<IdGenStatus<ID>>

Available on crate features snowflake or ulid only.

Attempts to generate a new ULID with fallible error handling.

Combines the current timestamp with a freshly generated random value to produce a unique identifier. Returns IdGenStatus::Ready on success.

§Returns
  • Ok(IdGenStatus::Ready { id }): A new ID is available
  • Ok(IdGenStatus::Pending { yield_for }): The time to wait (in milliseconds) before trying again
  • Err(_): infallible for this generator
§Errors
  • This method currently does not return any errors and always returns Ok. It is marked as fallible to allow for future extensibility
§Example
use ferroid::{BasicUlidGenerator, IdGenStatus, ULID, ToU64, MonotonicClock, ThreadRandom};

let generator = BasicUlidGenerator::new(MonotonicClock::default(), ThreadRandom::default());

// Attempt to generate a new ID
let id: ULID = loop {
    match generator.try_next_id() {
        Ok(IdGenStatus::Ready { id }) => break id,
        Ok(IdGenStatus::Pending { yield_for }) => {
            std::thread::sleep(core::time::Duration::from_millis(yield_for.to_u64()));
        }
        Err(_) => unreachable!(),
    }
};

Trait Implementations§

Source§

impl<ID, T, R> UlidGenerator<ID, T, R> for BasicUlidGenerator<ID, T, R>
where ID: UlidId, T: TimeSource<ID::Ty>, R: RandSource<ID::Ty>,

Available on crate features snowflake or ulid only.
Source§

type Err = Infallible

Source§

fn new(time: T, rng: R) -> Self

Source§

fn next_id(&self) -> IdGenStatus<ID>

Returns the next available ID
Source§

fn try_next_id(&self) -> Result<IdGenStatus<ID>, Self::Err>

A fallible version of Self::next_id that returns a Result. Read more

Auto Trait Implementations§

§

impl<ID, T, R> Freeze for BasicUlidGenerator<ID, T, R>
where T: Freeze, R: Freeze,

§

impl<ID, T, R> RefUnwindSafe for BasicUlidGenerator<ID, T, R>

§

impl<ID, T, R> Send for BasicUlidGenerator<ID, T, R>
where T: Send, R: Send, ID: Send,

§

impl<ID, T, R> Sync for BasicUlidGenerator<ID, T, R>
where T: Sync, R: Sync, ID: Sync,

§

impl<ID, T, R> Unpin for BasicUlidGenerator<ID, T, R>
where T: Unpin, R: Unpin, ID: Unpin,

§

impl<ID, T, R> UnwindSafe for BasicUlidGenerator<ID, T, R>
where T: UnwindSafe, R: UnwindSafe, ID: 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> 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.
Source§

impl<G, ID, T, R> UlidGeneratorAsyncExt<ID, T, R> for G
where G: UlidGenerator<ID, T, R>, ID: UlidId, T: TimeSource<<ID as Id>::Ty>, R: RandSource<<ID as Id>::Ty>,

Source§

type Err = <G as UlidGenerator<ID, T, R>>::Err

Available on crate features futures and ulid only.
Source§

fn try_next_id_async<'a, S>( &'a self, ) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncExt<ID, T, R>>::Err>>
where S: SleepProvider,

Available on crate features futures and ulid only.
Returns a future that resolves to the next available Snowflake ID. Read more
Source§

impl<G, ID, T, R> UlidGeneratorAsyncSmolExt<ID, T, R> for G
where G: UlidGenerator<ID, T, R>, ID: UlidId, T: TimeSource<<ID as Id>::Ty>, R: RandSource<<ID as Id>::Ty>,

Source§

type Err = <G as UlidGenerator<ID, T, R>>::Err

Available on crate feature async-smol and crate feature ulid and (crate features async-tokio or async-smol) only.
Source§

fn try_next_id_async( &self, ) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncSmolExt<ID, T, R>>::Err>>

Available on crate feature async-smol and crate feature ulid and (crate features async-tokio or async-smol) only.
Returns a future that resolves to the next available Ulid using the SmolSleep provider. Read more
Source§

impl<G, ID, T, R> UlidGeneratorAsyncTokioExt<ID, T, R> for G
where G: UlidGenerator<ID, T, R>, ID: UlidId, T: TimeSource<<ID as Id>::Ty>, R: RandSource<<ID as Id>::Ty>,

Source§

type Err = <G as UlidGenerator<ID, T, R>>::Err

Available on crate feature async-tokio and crate feature ulid and (crate features async-tokio or async-smol) only.
Source§

fn try_next_id_async( &self, ) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncTokioExt<ID, T, R>>::Err>>

Available on crate feature async-tokio and crate feature ulid and (crate features async-tokio or async-smol) only.
Returns a future that resolves to the next available ULID using the TokioSleep provider. Read more
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V