Crate snowflake_me

source ·
Expand description

A distributed unique ID generator inspired by Twitter’s Snowflake.

This is a Rust implementation of the original [houseme/snowflake-rs], which is written in Go.

Quickstart

Add the following to your Cargo.toml:

[dependencies]
snowflake_me = "0.1"

Use the library like this:

use snowflake_me::Snowflake;

let sf = Snowflake::new().unwrap();
let next_id = sf.next_id().unwrap();
println!("{}", next_id);

Concurrent use

Snowflake is threadSafe. clone it before moving to another thread:

use snowflake_me::Snowflake;
use std::thread;

let sf = Snowflake::new().unwrap();

let mut children = Vec::new();
for _ in 0..10 {
    let mut thread_sf = sf.clone();
    children.push(thread::spawn(move || {
        println!("{}", thread_sf.next_id().unwrap());
    }));
}

for child in children {
    child.join().unwrap();
}

Structs

Enums

  • The error type for this crate.

Functions

  • Break a Snowflake ID up into its parts.