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
impl SonyflakeGenerator
Sourcepub fn new(machine_id: u64) -> Result<Self, IdError>
pub fn new(machine_id: u64) -> Result<Self, IdError>
Creates a Sonyflake-style generator with default layout and epoch.
§Parameters
machine_id: Machine identifier in0..=65535.
§Returns
A configured generator.
§Errors
Returns IdError::MachineIdOutOfRange when machine_id does not fit
in the default 16-bit machine field.
Sourcepub fn with_epoch(
machine_id: u64,
start_time: SystemTime,
) -> Result<Self, IdError>
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 in0..=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.
Sourcepub fn with_options(
machine_id: u64,
bits_sequence: u8,
bits_machine_id: u8,
time_unit: Duration,
start_time: SystemTime,
) -> Result<Self, IdError>
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, or0for default.bits_machine_id: Machine bit length, or0for 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.
Sourcepub 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>
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>
Creates a Sonyflake-style generator with an explicit clock.
§Parameters
machine_id: Machine identifier.bits_sequence: Sequence bit length, or0for default.bits_machine_id: Machine bit length, or0for 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.