Struct Ulid

Source
pub struct Ulid;
Expand description

A thread-local ULID generator with monotonic and non-monotonic modes.

Provides fast, per-thread ULID generation using a shared monotonic clock and thread-local RNG. Monotonic overflows are handled with configurable Backoff strategies.

Requires the ulid and thread_local features.

Implementations§

Source§

impl Ulid

Source

pub fn new_ulid() -> ULID

Generates a non-monotonic ULID that is always random, even when generated within the same millisecond.

§Example
#[cfg(all(feature = "ulid", feature = "thread_local"))]
{
    use ferroid::Ulid;
    let id = Ulid::new_ulid();
}
Source

pub fn new_mono_ulid() -> ULID

Generates a monotonic ULID using Backoff::Yield as the overflow strategy.

§Example
#[cfg(all(feature = "ulid", feature = "thread_local"))]
{
    use ferroid::Ulid;
    let id = Ulid::new_mono_ulid();
}
Source

pub fn with_mono_backoff(strategy: Backoff) -> ULID

Generates a monotonic ULID using the given Backoff strategy to handle overflow.

If the generator exhausts available entropy within the same millisecond, the backoff strategy determines how it waits before retrying.

§Example
#[cfg(all(feature = "ulid", feature = "thread_local"))]
{
    use ferroid::{Ulid, Backoff};
    let id = Ulid::with_mono_backoff(Backoff::Spin);
}
Source

pub fn from_timestamp(timestamp: <ULID as Id>::Ty) -> ULID

Creates a ULID from a given millisecond timestamp.

Random bits are generated using the thread-local RNG. The resulting ID is not guaranteed to be monotonic.

§Example
#[cfg(all(feature = "ulid", feature = "thread_local"))]
{
    use ferroid::Ulid;
    let id = Ulid::from_timestamp(1_694_201_234_000);
}
Source

pub fn from_timestamp_and_rand<R>(timestamp: <ULID as Id>::Ty, rng: &R) -> ULID
where R: RandSource<<ULID as Id>::Ty>,

Creates a ULID from a given millisecond timestamp and a custom RNG.

Useful in deterministic or testable scenarios where a specific random source is preferred.

§Example
#[cfg(all(feature = "ulid", feature = "thread_local"))]
{
    use ferroid::{Ulid, ThreadRandom};
    let id = Ulid::from_timestamp_and_rand(0, &ThreadRandom);
}
Source

pub fn from_datetime(datetime: SystemTime) -> ULID

Creates a ULID from a SystemTime.

This is a convenience wrapper over Ulid::from_timestamp that extracts the Unix timestamp in milliseconds.

§Example
#[cfg(all(feature = "ulid", feature = "thread_local"))]
{
    use ferroid::Ulid;
    let id = Ulid::from_datetime(std::time::SystemTime::now());
}
Source

pub fn from_datetime_and_rand<R>(datetime: SystemTime, rng: &R) -> ULID
where R: RandSource<<ULID as Id>::Ty>,

Creates a ULID from a SystemTime and a custom RNG.

§Example
#[cfg(all(feature = "ulid", feature = "thread_local"))]
{
    use ferroid::{Ulid, ThreadRandom};
    let now = std::time::SystemTime::now();
    let id = Ulid::from_datetime_and_rand(now, &ThreadRandom);
}

Auto Trait Implementations§

§

impl Freeze for Ulid

§

impl RefUnwindSafe for Ulid

§

impl Send for Ulid

§

impl Sync for Ulid

§

impl Unpin for Ulid

§

impl UnwindSafe for Ulid

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more