[][src]Crate sonyflake

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.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

Builder

A builder to build a Sonyflake generator.

Sonyflake

Sonyflake is a distributed unique ID generator.

Enums

Error

The error type for this crate.

Functions

decompose

Break a Sonyflake ID up into its parts.