pub struct ClassicLayout<I>where
I: MachineId,{ /* private fields */ }Expand description
A Layout implementation for the classic snowflake layout introduced by Twitter.
Snowflakes constructed with this layout consist of a leading 0 bit, 41 bits for a timestamp in milliseconds, 10
bits for an instance ID, and 12 bits for the sequence number. The leading 0 bit guarantees that snowflakes with
this layout keep their properties (namely, monotonicity) when converted into signed 64-bit integers.
Note that this layout doesn’t specify the snowflake’s epoch, however. Even when using this layout, you’ll have to
specify your own epoch by implementing Epoch.
§Example
use snowdon::{
ClassicLayout, ClassicLayoutSnowflakeExtension, Epoch, Generator,
MachineId, Snowflake,
};
struct SnowflakeParams;
impl Epoch for SnowflakeParams {
fn millis_since_unix() -> u64 {
// The epoch used by Twitter for their snowflake IDs
1288834974657
}
}
impl MachineId for SnowflakeParams {
fn machine_id() -> u64 {
// Somehow obtain this machine's ID (e.g. from the private IP
// address or a configuration file)
}
}
// Make our snowflake specification available to the rest of the application
type MySnowflake =
Snowflake<ClassicLayout<SnowflakeParams>, SnowflakeParams>;
type MySnowflakeGenerator =
Generator<ClassicLayout<SnowflakeParams>, SnowflakeParams>;
// Use our snowflake format
let snowflake = MySnowflake::from_raw(1541815603606036480).unwrap();
assert_eq!(367597485448, snowflake.timestamp_raw());
assert_eq!(0x017A, snowflake.machine_id());
assert_eq!(0, snowflake.sequence_number());Implementations§
Source§impl<I> ClassicLayout<I>where
I: MachineId,
impl<I> ClassicLayout<I>where
I: MachineId,
Sourcepub fn machine_id(input: u64) -> u64
pub fn machine_id(input: u64) -> u64
Returns the machine ID of the given snowflake.
Usually, you shouldn’t call this function directly. Instead, use
machine_id directly on the snowflake by importing
ClassicLayoutSnowflakeExtension.