Skip to main content

UlidGenerator

Struct UlidGenerator 

Source
pub struct UlidGenerator { /* private fields */ }
Expand description

Monotonic ULIDs for one explicitly owned issuance stream.

Supply the timestamp captured for the write operation and fresh host-provided randomness. This generator never reads a clock, seeds an RNG or makes a call. Equal/backward timestamps retain the last timestamp and increment its suffix. That logical timestamp can differ from the operation’s managed timestamps.

Successful issuance consumes an ID even if a later database batch is refused; gaps are permitted. An IC trap reverts in-canister state in that execution. The host must preserve the last issued ID and restore it after upgrades if monotonicity must continue. Restoring an older checkpoint or starting another stream can repeat IDs. Independent streams need independently provisioned randomness; neither global uniqueness nor secret-token suitability is promised.

Implementations§

Source§

impl UlidGenerator

Source

pub const fn new() -> Self

Start a new stream with no preceding identifier, including at Unix epoch.

Source

pub const fn resume_after(last: Ulid) -> Self

Continue after the host’s authoritative last issued identifier. Restoring stale state can repeat identifiers; the database does not own this checkpoint.

Source

pub const fn last(&self) -> Option<Ulid>

The last successfully issued identifier, suitable for a host-owned checkpoint.

Source

pub fn generate( &mut self, timestamp: Timestamp, randomness: [u8; 10], ) -> Result<Ulid, UlidGenerationError>

Issue an ID at the operation’s timestamp using an explicit 80-bit random suffix.

Randomness is used only for the first ID or a timestamp newer than the last issued timestamp. The caller must obtain it before the synchronous write boundary; it may use an already seeded host stream without an inter-canister call.

§Errors

Rejects negative or greater-than-48-bit millisecond timestamps. If the suffix is exhausted at an equal/backward time, refuses without advancing time or wrapping the suffix. Every error preserves the previous state.

Trait Implementations§

Source§

impl Debug for UlidGenerator

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for UlidGenerator

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.