Skip to main content

SonyflakeGenerator

Struct SonyflakeGenerator 

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

Sonyflake-style generator using configurable time, sequence, and machine bits.

By default, the layout is compatible with Sonyflake’s commonly documented allocation: 39 bits of time in 10 ms units, 8 sequence bits, and 16 machine bits. The sign bit is not used.

Implementations§

Source§

impl SonyflakeGenerator

Source

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

Creates a Sonyflake-style generator with default layout and epoch.

§Parameters
  • machine_id: Machine identifier in 0..=65535.
§Returns

A configured generator.

§Errors

Returns IdError::MachineIdOutOfRange when machine_id does not fit in the default 16-bit machine field.

Source

pub fn with_epoch( machine_id: u64, start_time: SystemTime, ) -> Result<Self, IdError>

Creates a Sonyflake-style generator with default layout and explicit epoch.

§Parameters
  • machine_id: Machine identifier in 0..=65535.
  • start_time: Start time used as elapsed-time origin.
§Returns

A configured generator using the system clock.

§Errors

Returns the same errors as SonyflakeGenerator::with_options.

Source

pub fn with_options( machine_id: u64, bits_sequence: u8, bits_machine_id: u8, time_unit: Duration, start_time: SystemTime, ) -> Result<Self, IdError>

Creates a Sonyflake-style generator with explicit layout.

Passing 0 for either bit length selects the Sonyflake default for that field.

§Parameters
  • machine_id: Machine identifier.
  • bits_sequence: Sequence bit length, or 0 for default.
  • bits_machine_id: Machine bit length, or 0 for default.
  • time_unit: Time unit; must be at least one millisecond.
  • start_time: Start time used as elapsed-time origin.
§Returns

A configured generator using the system clock.

§Errors

Returns IdError::InvalidBitLength for invalid bit allocation, IdError::InvalidTimeUnit for sub-millisecond time units, IdError::StartTimeAhead when start_time is in the future, or IdError::MachineIdOutOfRange when machine_id does not fit.

Source

pub fn with_clock<F>( machine_id: u64, bits_sequence: u8, bits_machine_id: u8, time_unit: Duration, start_time: SystemTime, clock: F, ) -> Result<Self, IdError>
where F: Fn() -> SystemTime + Send + Sync + 'static,

Creates a Sonyflake-style generator with an explicit clock.

§Parameters
  • machine_id: Machine identifier.
  • bits_sequence: Sequence bit length, or 0 for default.
  • bits_machine_id: Machine bit length, or 0 for default.
  • time_unit: Time unit; must be at least one millisecond.
  • start_time: Start time used as elapsed-time origin.
  • clock: Function returning the current time.
§Returns

A configured generator.

§Errors

Returns the same validation errors as SonyflakeGenerator::with_options.

Source

pub const fn bits_time(&self) -> u8

Returns the number of time bits.

§Returns

Time bit length.

Source

pub const fn bits_sequence(&self) -> u8

Returns the number of sequence bits.

§Returns

Sequence bit length.

Source

pub const fn bits_machine(&self) -> u8

Returns the number of machine bits.

§Returns

Machine bit length.

Source

pub const fn max_elapsed_time(&self) -> u64

Returns the maximum representable elapsed time unit.

§Returns

Maximum elapsed time value.

Source

pub const fn max_sequence(&self) -> u64

Returns the maximum representable sequence.

§Returns

Maximum sequence number.

Source

pub const fn max_machine_id(&self) -> u64

Returns the maximum representable machine identifier.

§Returns

Maximum machine identifier.

Source

pub fn compose( &self, elapsed_time: u64, sequence: u64, machine_id: u64, ) -> Result<u64, IdError>

Composes a Sonyflake-style ID from explicit parts.

§Parameters
  • elapsed_time: Time units elapsed since the start time.
  • sequence: Sequence value inside the time unit.
  • machine_id: Machine identifier.
§Returns

Encoded ID.

§Errors

Returns range errors when any part does not fit the configured layout.

Source

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

Extracts elapsed time from a Sonyflake-style ID.

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

Elapsed time units since the start time.

Source

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

Extracts sequence from a Sonyflake-style ID.

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

Sequence number.

Source

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

Extracts machine ID from a Sonyflake-style ID.

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

Machine identifier.

Trait Implementations§

Source§

impl IdGenerator<u64> for SonyflakeGenerator

Source§

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

Generates the next Sonyflake-style 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.