#twitter #snowflake #id #uid

rustflake

Thread-safe 'twitter' snowflakes

2 releases

0.1.1 Aug 4, 2019
0.1.0 Aug 4, 2019

#16 in #uid

Download history 37/week @ 2024-07-22 51/week @ 2024-07-29 33/week @ 2024-08-05 49/week @ 2024-08-12 25/week @ 2024-08-19 36/week @ 2024-08-26 44/week @ 2024-09-02 46/week @ 2024-09-09 49/week @ 2024-09-16 68/week @ 2024-09-23 70/week @ 2024-09-30 1/week @ 2024-10-07 34/week @ 2024-10-14 46/week @ 2024-10-21 40/week @ 2024-10-28 44/week @ 2024-11-04

164 downloads per month
Used in zero4rs

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–2MB
~32K SLoC