pub struct AtomicMonoUlidGenerator<ID, T, R>{ /* private fields */ }ulid and crate feature atomic and target_has_atomic=128 only.Expand description
A lock-free monotonic ULID-style ID generator suitable for single-threaded environments.
This generator stores the ULID in an AtomicU128, allowing safe shared
use across threads.
§Features
- ✅ Thread-safe
- ✅ Probabilistically unique (no coordination required)
- ✅ Time-ordered (monotonically increasing per millisecond)
§Caveats
This implementation uses an AtomicU128 internally, so it only supports
ID layouts where the underlying type is u128. You cannot use layouts
with larger or smaller representations (i.e., ID::Ty must be u128).
§Recommended When
- You’re in a multi-threaded environment
- You need require monotonically increasing IDs (ID generated within the same millisecond increment a sequence counter)
§See Also
Implementations§
Source§impl<ID, T, R> AtomicMonoUlidGenerator<ID, T, R>
impl<ID, T, R> AtomicMonoUlidGenerator<ID, T, R>
Sourcepub fn new(time: T, rng: R) -> Self
Available on crate features snowflake or ulid only.
pub fn new(time: T, rng: R) -> Self
snowflake or ulid only.Creates a new AtomicMonoUlidGenerator with the provided time source
and RNG.
§Parameters
time: 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::{AtomicMonoUlidGenerator, IdGenStatus, ULID, MonotonicClock, ThreadRandom};
let generator = AtomicMonoUlidGenerator::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 from_components(
timestamp: ID::Ty,
random: ID::Ty,
time: T,
rng: R,
) -> Self
Available on crate features snowflake or ulid only.
pub fn from_components( timestamp: ID::Ty, random: ID::Ty, time: T, rng: R, ) -> Self
snowflake or ulid only.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 numbertime: 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>
Available on crate features snowflake or ulid only.
pub fn next_id(&self) -> IdGenStatus<ID>
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::{AtomicMonoUlidGenerator, IdGenStatus, ULID, MonotonicClock, ThreadRandom};
let generator = AtomicMonoUlidGenerator::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>>
Available on crate features snowflake or ulid only.
pub fn try_next_id(&self) -> Result<IdGenStatus<ID>>
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 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
use ferroid::{AtomicMonoUlidGenerator, IdGenStatus, ULID, ToU64, MonotonicClock, ThreadRandom};
let generator = AtomicMonoUlidGenerator::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 AtomicMonoUlidGenerator<ID, T, R>
Available on crate features snowflake or ulid only.
impl<ID, T, R> UlidGenerator<ID, T, R> for AtomicMonoUlidGenerator<ID, T, R>
snowflake or ulid only.type Err = Infallible
fn new(time: 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 AtomicMonoUlidGenerator<ID, T, R>
impl<ID, T, R> RefUnwindSafe for AtomicMonoUlidGenerator<ID, T, R>
impl<ID, T, R> Send for AtomicMonoUlidGenerator<ID, T, R>
impl<ID, T, R> Sync for AtomicMonoUlidGenerator<ID, T, R>
impl<ID, T, R> Unpin for AtomicMonoUlidGenerator<ID, T, R>
impl<ID, T, R> UnwindSafe for AtomicMonoUlidGenerator<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<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>,
Source§type Err = <G as UlidGenerator<ID, T, R>>::Err
type Err = <G as UlidGenerator<ID, T, R>>::Err
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,
fn try_next_id_async<'a, S>(
&'a self,
) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncExt<ID, T, R>>::Err>>where
S: SleepProvider,
futures and ulid only.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>,
Source§type Err = <G as UlidGenerator<ID, T, R>>::Err
type Err = <G as UlidGenerator<ID, T, R>>::Err
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>>
fn try_next_id_async( &self, ) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncSmolExt<ID, T, R>>::Err>>
async-smol and crate feature ulid and (crate features async-tokio or async-smol) only.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>,
Source§type Err = <G as UlidGenerator<ID, T, R>>::Err
type Err = <G as UlidGenerator<ID, T, R>>::Err
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>>
fn try_next_id_async( &self, ) -> impl Future<Output = Result<ID, <G as UlidGeneratorAsyncTokioExt<ID, T, R>>::Err>>
async-tokio and crate feature ulid and (crate features async-tokio or async-smol) only.TokioSleep provider. Read more