#snowflake-id #uid #twitter #snowflake

rustflake

Thread-safe 'twitter' snowflakes

2 releases

0.1.1 Aug 4, 2019
0.1.0 Aug 4, 2019

#35 in #snowflake-id

Download history 40/week @ 2025-03-28 33/week @ 2025-04-04 60/week @ 2025-04-11 54/week @ 2025-04-18 74/week @ 2025-04-25 54/week @ 2025-05-02 40/week @ 2025-05-09 23/week @ 2025-05-16 20/week @ 2025-05-23 2/week @ 2025-05-30 20/week @ 2025-06-06 27/week @ 2025-06-13 88/week @ 2025-06-20 55/week @ 2025-06-27 45/week @ 2025-07-04 70/week @ 2025-07-11

272 downloads per month
Used in 2 crates

MIT license

5KB
59 lines

rustflake

Thread-safe "twitter" snowflakes.

By default the original Twitter snowflake format defines:

  • 41 bits are used to store a custom epoch with millisecond precision
  • 10 bits are used to store worker and datacenter information
  • 12 bits are used to store a sequence number

This crate lets you customize your own epoch and worker/datacenter information.

Usage

Add this to your Cargo.toml:

[dependencies]
rustflake = "0.1.0"

and this to your crate root:

use rustflake;

Example

use rustflake::Snowflake;

fn main() {
    let mut snowflake = Snowflake::default();
    println!("{}", &snowflake.generate());
}
use rustflake::Snowflake;

fn main() {
    // Discord Epoch
    // Though those are not "real" discord Ids,
    // because discord increases the sequence
    // for *every* generated Id on that process
    let mut snowflake = Snowflake::new(1420070400000, 1, 1);
    println!("{}", &snowflake.generate());
}
use rustflake::Snowflake;

fn main() {
    // Using a builder approach
    let mut snowflake = Snowflake::default()
        .epoch(1_564_790_400_000)
        .worker_id(2)
        .datacenter_id(3);
    println!("{}", &snowflake.generate());
}

Dependencies

~1.6–2.1MB
~33K SLoC