Skip to main content

SnowflakeGenerator

Struct SnowflakeGenerator 

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

Classic Snowflake generator using 41 timestamp, 10 node, and 12 sequence bits.

Implementations§

Source§

impl SnowflakeGenerator

Source

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

Creates a classic Snowflake generator with the default Qubit epoch.

§Parameters
  • node_id: Node identifier in 0..=1023.
§Returns

A configured generator.

§Errors

Returns IdError::NodeOutOfRange when node_id does not fit in 10 bits.

Source

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 in 0..=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.

Source

pub fn with_clock<F>( node_id: u64, epoch: SystemTime, clock: F, ) -> Result<Self, IdError>
where F: Fn() -> SystemTime + Send + Sync + 'static,

Creates a classic Snowflake generator with an explicit clock.

§Parameters
  • node_id: Node identifier in 0..=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.

Source

pub const fn node_id(&self) -> u64

Returns the configured node identifier.

§Returns

Node identifier.

Source

pub const fn epoch(&self) -> SystemTime

Returns the configured epoch.

§Returns

Timestamp origin.

Source

pub const fn max_timestamp(&self) -> u64

Returns the maximum representable timestamp.

§Returns

Maximum timestamp in milliseconds since the epoch.

Source

pub const fn max_sequence(&self) -> u64

Returns the maximum representable sequence.

§Returns

Maximum sequence number.

Source

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.

Source

pub const fn extract_timestamp(&self, id: u64) -> u64

Extracts the timestamp part from an ID.

§Parameters
  • id: ID generated by this layout.
§Returns

Milliseconds elapsed since the epoch.

Source

pub const fn extract_node_id(&self, id: u64) -> u64

Extracts the node identifier from an ID.

§Parameters
  • id: ID generated by this layout.
§Returns

Node identifier.

Source

pub const fn extract_sequence(&self, id: u64) -> u64

Extracts the sequence number from an ID.

§Parameters
  • id: ID generated by this layout.
§Returns

Sequence number.

Trait Implementations§

Source§

impl IdGenerator<u64> for SnowflakeGenerator

Source§

fn next_id(&self) -> Result<u64, Self::Error>

Generates the next classic Snowflake ID.

Source§

type Error = IdError

Error returned when generation fails.
Source§

fn format_id(&self, id: &T) -> String
where T: Display,

Formats an already generated ID. Read more
Source§

fn next_string(&self) -> Result<String, Self::Error>
where T: Display,

Generates the next ID and formats it as a string. 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 = 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.