Skip to main content

SnowflakeGenerator

Struct SnowflakeGenerator 

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

Lock-free monotonic 64-bit Snowflake ID generator safe for multi-threaded sharing.

Implementations§

Source§

impl SnowflakeGenerator

Lock-free ID generation methods for SnowflakeGenerator.

Source

pub fn new(node_id: u16) -> Result<Self, IdError>

Creates a new generator with the default custom epoch (2020-01-01T00:00:00Z).

§Arguments
  • u16 - Unique node identifier in 0..=1023.
§Returns
  • Result<Self, IdError> - The ready generator or IdError::NodeIdOutOfRange.
Source

pub fn with_epoch(node_id: u16, epoch_ms: u64) -> Result<Self, IdError>

Creates a new generator with an explicit custom epoch in Unix milliseconds.

§Arguments
  • u16 - Unique node identifier in 0..=1023.
  • u64 - Custom epoch in Unix milliseconds.
§Returns
  • Result<Self, IdError> - The ready generator or IdError::NodeIdOutOfRange.
Source

pub fn get_epoch_ms(&self) -> u64

Returns the configured custom epoch in Unix milliseconds.

§Returns
  • u64 - Custom epoch timestamp in milliseconds.
Source

pub fn get_node_id(&self) -> u16

Returns the configured 10-bit node identifier.

§Returns
  • u16 - Node identifier (0..=1023).
Source

pub fn next_id(&self) -> SnowflakeId

Generates the next strictly monotonic 64-bit Snowflake ID using wall-clock time.

Uses a single lock-free AtomicU64 compare-exchange loop; clock rollbacks or intra-millisecond sequence exhaustion advance the logical millisecond monotonically without blocking or panicking.

§Returns
  • SnowflakeId - A unique, strictly increasing Snowflake identifier.
Source

pub fn next_id_at(&self, now_unix_ms: u64) -> SnowflakeId

Generates the next strictly monotonic 64-bit Snowflake ID at an explicit Unix timestamp.

§Arguments
  • u64 - Current Unix timestamp in milliseconds.
§Returns
  • SnowflakeId - A unique, strictly increasing Snowflake identifier.

Trait Implementations§

Source§

impl Debug for SnowflakeGenerator

Source§

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

Formats the value using the given formatter. 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, 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.