pub struct BasicUlidGenerator<ID, T, R>{ /* private fields */ }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)
§Recommended When
- 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>
impl<ID, T, R> BasicUlidGenerator<ID, T, R>
Sourcepub const fn new(clock: T, rng: R) -> Self
pub const fn new(clock: T, rng: R) -> Self
Creates a new BasicUlidGenerator with the provided time source and
RNG.
§Parameters
clock: ATimeSourceused to retrieve the current timestamprng: ARandSourceused to generate random bits
§Returns
A ready-to-use ULID generator suitable for producing unique, sortable IDs.
§Example
#[cfg(all(feature = "std", feature = "alloc", feature = "ulid"))]
{
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(),
}
};
}Sourcepub fn next_id(&self) -> IdGenStatus<ID>
pub fn next_id(&self) -> IdGenStatus<ID>
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
#[cfg(all(feature = "std", feature = "alloc", feature = "ulid"))]
{
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(),
}
};
}Sourcepub fn try_next_id(&self) -> Result<IdGenStatus<ID>>
pub fn try_next_id(&self) -> Result<IdGenStatus<ID>>
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 availableOk(IdGenStatus::Pending { yield_for }): The time to wait (in milliseconds) before trying againErr(_): 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
#[cfg(all(feature = "std", feature = "alloc", feature = "ulid"))]
{
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>
impl<ID, T, R> UlidGenerator<ID, T, R> for BasicUlidGenerator<ID, T, R>
type Err = Infallible
fn new(clock: T, rng: R) -> Self
Source§fn next_id(&self) -> IdGenStatus<ID>
fn next_id(&self) -> IdGenStatus<ID>
Source§fn try_next_id(&self) -> Result<IdGenStatus<ID>, Self::Err>
fn try_next_id(&self) -> Result<IdGenStatus<ID>, Self::Err>
Auto Trait Implementations§
impl<ID, T, R> Freeze for BasicUlidGenerator<ID, T, R>
impl<ID, T, R> RefUnwindSafe for BasicUlidGenerator<ID, T, R>
impl<ID, T, R> Send for BasicUlidGenerator<ID, T, R>
impl<ID, T, R> Sync for BasicUlidGenerator<ID, T, R>
impl<ID, T, R> Unpin for BasicUlidGenerator<ID, T, R>
impl<ID, T, R> UnwindSafe for BasicUlidGenerator<ID, T, R>
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
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<G, ID, T, R> UlidGeneratorAsyncExt<ID, T, R> for Gwhere
G: UlidGenerator<ID, T, R>,
ID: UlidId,
T: TimeSource<<ID as Id>::Ty>,
R: RandSource<<ID as Id>::Ty>,
impl<G, ID, T, R> UlidGeneratorAsyncExt<ID, T, R> for Gwhere
G: UlidGenerator<ID, T, R>,
ID: UlidId,
T: TimeSource<<ID as Id>::Ty>,
R: RandSource<<ID as Id>::Ty>,
type Err = <G as UlidGenerator<ID, T, R>>::Err
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,
fn try_next_id_async<'a, S>(
&'a self,
) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncExt<ID, T, R>>::Err>>where
S: SleepProvider,
Source§impl<G, ID, T, R> UlidGeneratorAsyncSmolExt<ID, T, R> for Gwhere
G: UlidGenerator<ID, T, R>,
ID: UlidId,
T: TimeSource<<ID as Id>::Ty>,
R: RandSource<<ID as Id>::Ty>,
impl<G, ID, T, R> UlidGeneratorAsyncSmolExt<ID, T, R> for Gwhere
G: UlidGenerator<ID, T, R>,
ID: UlidId,
T: TimeSource<<ID as Id>::Ty>,
R: RandSource<<ID as Id>::Ty>,
type Err = <G as UlidGenerator<ID, T, R>>::Err
Source§fn try_next_id_async(
&self,
) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncSmolExt<ID, T, R>>::Err>>
fn try_next_id_async( &self, ) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncSmolExt<ID, T, R>>::Err>>
Source§impl<G, ID, T, R> UlidGeneratorAsyncTokioExt<ID, T, R> for Gwhere
G: UlidGenerator<ID, T, R>,
ID: UlidId,
T: TimeSource<<ID as Id>::Ty>,
R: RandSource<<ID as Id>::Ty>,
impl<G, ID, T, R> UlidGeneratorAsyncTokioExt<ID, T, R> for Gwhere
G: UlidGenerator<ID, T, R>,
ID: UlidId,
T: TimeSource<<ID as Id>::Ty>,
R: RandSource<<ID as Id>::Ty>,
type Err = <G as UlidGenerator<ID, T, R>>::Err
Source§fn try_next_id_async(
&self,
) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncTokioExt<ID, T, R>>::Err>>
fn try_next_id_async( &self, ) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncTokioExt<ID, T, R>>::Err>>
TokioSleep provider. Read more