Expand description
A distributed unique ID generator inspired by Twitter’s Snowflake.
This is a Rust implementation of the original houseme/snowflake, which is written in Go.
§Quickstart
Add the following to your Cargo.toml
:
[dependencies]
snowflake_me = "0.2"
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§
- Builder
- A builder to build a
Snowflake
generator. - Decomposed
Snowflake - DecomposedSnowflake is the parts of a Snowflake ID.
- Snowflake
- Snowflake is a distributed unique ID generator. It is thread-safe and can be cloned to be used in multiple threads.
Enums§
- Error
- The error type for this crate.
Functions§
- decompose
- Break a Snowflake ID up into its parts.