ClassicLayout

Struct ClassicLayout 

Source
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,

Source

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.

Trait Implementations§

Source§

impl<I> Debug for ClassicLayout<I>
where I: MachineId + Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<I> Layout for ClassicLayout<I>
where I: MachineId,

Source§

fn construct_snowflake(timestamp: u64, sequence_number: u64) -> u64

Combines the given timestamp and sequence number into a 64-bit snowflake integer. Read more
Source§

fn timestamp(input: u64) -> u64

Returns the timestamp stored in the given snowflake. Read more
Source§

fn exceeds_timestamp(input: u64) -> bool

Returns whether the given timestamp exceeds the number of bits dedicated to timestamps in this layout. Read more
Source§

fn sequence_number(input: u64) -> u64

Returns the sequence number of the given snowflake. Read more
Source§

fn exceeds_sequence_number(input: u64) -> bool

Returns whether the given sequence number exceeds the number of bits dedicated in this layout. Read more
Source§

fn is_valid_snowflake(input: u64) -> bool

Returns whether the given snowflake is a valid integer representation of a snowflake. Read more

Auto Trait Implementations§

§

impl<I> Freeze for ClassicLayout<I>

§

impl<I> RefUnwindSafe for ClassicLayout<I>
where I: RefUnwindSafe,

§

impl<I> Send for ClassicLayout<I>
where I: Send,

§

impl<I> Sync for ClassicLayout<I>
where I: Sync,

§

impl<I> Unpin for ClassicLayout<I>
where I: Unpin,

§

impl<I> UnwindSafe for ClassicLayout<I>
where I: UnwindSafe,

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.