pub struct SnowflakeGenerator { /* private fields */ }Expand description
Classic Snowflake generator using 41 timestamp, 10 node, and 12 sequence bits.
Implementations§
Source§impl SnowflakeGenerator
impl SnowflakeGenerator
Sourcepub fn new(node_id: u64) -> Result<Self, IdError>
pub fn new(node_id: u64) -> Result<Self, IdError>
Creates a classic Snowflake generator with the default Qubit epoch.
§Parameters
node_id: Node identifier in0..=1023.
§Returns
A configured generator.
§Errors
Returns IdError::NodeOutOfRange when node_id does not fit in 10 bits.
Sourcepub fn with_epoch(node_id: u64, epoch: SystemTime) -> Result<Self, IdError>
pub fn with_epoch(node_id: u64, epoch: SystemTime) -> Result<Self, IdError>
Creates a classic Snowflake generator with an explicit epoch.
§Parameters
node_id: Node identifier in0..=1023.epoch: Timestamp origin.
§Returns
A configured generator using the system clock.
§Errors
Returns IdError::NodeOutOfRange when node_id does not fit in 10 bits.
Sourcepub fn with_clock<F>(
node_id: u64,
epoch: SystemTime,
clock: F,
) -> Result<Self, IdError>
pub fn with_clock<F>( node_id: u64, epoch: SystemTime, clock: F, ) -> Result<Self, IdError>
Creates a classic Snowflake generator with an explicit clock.
§Parameters
node_id: Node identifier in0..=1023.epoch: Timestamp origin.clock: Function returning the current time.
§Returns
A configured generator.
§Errors
Returns IdError::NodeOutOfRange when node_id does not fit in 10 bits.
Sourcepub const fn epoch(&self) -> SystemTime
pub const fn epoch(&self) -> SystemTime
Sourcepub const fn max_timestamp(&self) -> u64
pub const fn max_timestamp(&self) -> u64
Returns the maximum representable timestamp.
§Returns
Maximum timestamp in milliseconds since the epoch.
Sourcepub const fn max_sequence(&self) -> u64
pub const fn max_sequence(&self) -> u64
Sourcepub fn compose(&self, timestamp: u64, sequence: u64) -> Result<u64, IdError>
pub fn compose(&self, timestamp: u64, sequence: u64) -> Result<u64, IdError>
Composes an ID from timestamp and sequence parts.
§Parameters
timestamp: Milliseconds elapsed since the epoch.sequence: Sequence value inside the timestamp millisecond.
§Returns
Encoded ID.
§Errors
Returns IdError::TimestampOverflow or IdError::SequenceOverflow
when a part does not fit the classic Snowflake layout.