Crate sonyflake

source ·
Expand description

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

This is a Rust implementation of the original sony/sonyflake, which is written in Go.

Quickstart

Add the following to your Cargo.toml:

[dependencies]
sonyflake = "0.1"

Use the library like this:

use sonyflake::Sonyflake;

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

Concurrent use

Sonyflake is threadsafe. clone it before moving to another thread:

use sonyflake::Sonyflake;
use std::thread;

let sf = Sonyflake::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 Sonyflake ID up into its parts.