pub struct BasicUlidGenerator<ID, T, R>{ /* private fields */ }Expand description
A monotonic ULID-style ID generator suitable for single-threaded environments.
This generator is lightweight and fast, but is not thread-safe.
§Features
- ❌ Not thread-safe
- ✅ Probabilistically unique (no coordination required)
- ✅ Time-ordered (monotonically increasing per millisecond)
§Recommended When
- You’re in a single-threaded environment (no shared access)
- You need require monotonically increasing IDs (ID generated within the same millisecond increment a sequence counter)
§See Also
Implementations§
Source§impl<ID, T, R> BasicUlidGenerator<ID, T, R>
impl<ID, T, R> BasicUlidGenerator<ID, T, R>
Sourcepub fn new(clock: T, rng: R) -> Self
pub 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
use ferroid::{BasicUlidGenerator, ULID, MonotonicClock, ThreadRandom};
let generator = BasicUlidGenerator::<ULID, _, _>::new(MonotonicClock::default(), ThreadRandom::default());
let id = generator.next_id();
println!("Generated ID: {:?}", id);Sourcepub fn from_components(
timestamp: ID::Ty,
random: ID::Ty,
clock: T,
rng: R,
) -> Self
pub fn from_components( timestamp: ID::Ty, random: ID::Ty, clock: T, rng: R, ) -> Self
Creates a new ID generator from explicit component values.
This constructor is primarily useful for advanced use cases such as restoring state from persistent storage or controlling the starting point of the generator manually.
§Parameters
timestamp: The initial timestamp component (usually in milliseconds)machine_id: The machine or worker identifiersequence: The initial sequence numberclock: ATimeSourceimplementation used to fetch the current time
§Returns
A new generator instance preloaded with the given state.
§⚠️ Note
In typical use cases, you should prefer Self::new to let the
generator initialize itself from the current time.
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
use ferroid::{BasicUlidGenerator, IdGenStatus, ULID, MonotonicClock, ThreadRandom};
let clock = MonotonicClock::default();
let rand = ThreadRandom::default();
let generator = BasicUlidGenerator::<ULID, _, _>::new(clock, rand);
// Attempt to generate a new ID
match generator.next_id() {
IdGenStatus::Ready { id } => {
println!("ID: {}", id);
}
IdGenStatus::Pending { yield_for } => {
println!("Exhausted; wait for: {}ms", yield_for);
}
}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 ULID was generated
- Ok(IdGenStatus::Pending { yield_for }) Never, but kept to match the Snowflake API
- Err(e) if the time source or rand source failed
§Example
use ferroid::{BasicUlidGenerator, IdGenStatus, ULID, MonotonicClock, ThreadRandom};
let clock = MonotonicClock::default();
let rand = ThreadRandom::default();
let generator = BasicUlidGenerator::<ULID, _, _>::new(clock, rand);
// Attempt to generate a new ID
match generator.try_next_id() {
Ok(IdGenStatus::Ready { id }) => {
println!("ID: {}", id);
}
Ok(IdGenStatus::Pending { yield_for }) => {
println!("Exhausted; wait for: {}ms", yield_for);
}
Err(e) => eprintln!("Generator error: {}", e),
}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>
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>>
fn try_next_id(&self) -> Result<IdGenStatus<ID>>
Self::next_id that returns a Result.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: Ulid,
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: Ulid,
T: TimeSource<<ID as Id>::Ty>,
R: RandSource<<ID as Id>::Ty>,
Source§fn try_next_id_async<'a, S>(&'a self) -> impl Future<Output = Result<ID, Error>>where
S: SleepProvider,
fn try_next_id_async<'a, S>(&'a self) -> impl Future<Output = Result<ID, Error>>where
S: SleepProvider,
Source§impl<G, ID, T, R> UlidGeneratorAsyncSmolExt<ID, T, R> for Gwhere
G: UlidGenerator<ID, T, R>,
ID: Ulid,
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: Ulid,
T: TimeSource<<ID as Id>::Ty>,
R: RandSource<<ID as Id>::Ty>,
Source§impl<G, ID, T, R> UlidGeneratorAsyncTokioExt<ID, T, R> for Gwhere
G: UlidGenerator<ID, T, R>,
ID: Ulid,
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: Ulid,
T: TimeSource<<ID as Id>::Ty>,
R: RandSource<<ID as Id>::Ty>,
Source§fn try_next_id_async(&self) -> impl Future<Output = Result<ID, Error>>
fn try_next_id_async(&self) -> impl Future<Output = Result<ID, Error>>
TokioSleep provider. Read more