#snowflake #twitter #epoch #time #zero-dependency #projects #id

k-snowflake

Simple zero-dependency implementation of Twitter`s (X) snowflake in rust

2 stable releases

1.1.0 Feb 8, 2025
1.0.0 Feb 8, 2025

#5 in #zero-dependency

Download history 219/week @ 2025-02-05 22/week @ 2025-02-12 1/week @ 2025-02-19 3/week @ 2025-02-26

245 downloads per month

MIT license

24KB
110 lines

k-Snowflake


Simple zero-dependency implementation of Twitter`s (X) snowflake in rust. Firstly used for my projects

Twitter snowflake schema

snowflake schema

Snowflakes are 64 bits in binary. (Only 63 are used to fit in a signed integer.) The first 41 bits are a timestamp, representing milliseconds since the chosen epoch (default X epoch is 1288834974657 (in Unix time milliseconds)) . The next 10 bits represent a machine ID, preventing clashes. Twelve more bits represent a per-machine sequence number, to allow creation of multiple snowflakes in the same millisecond. The final number is generally serialized in decimal

Basic usage

use k_snowflake::Snowflake;

fn main() {
    // Create snowflake
    let snowflake = Snowflake::new(123, 123);

    // convert to decimal
    let decimal_snowflake = snowflake.to_decimal().unwrap();

    // supports display
    println!("{}", snowflake);
}

See other methods in docs

No runtime deps