Crates.io | snowflake_me |
lib.rs | snowflake_me |
version | 0.3.1 |
source | src |
created_at | 2022-10-04 17:24:17.38606 |
updated_at | 2024-10-16 05:27:14.8815 |
description | A distributed unique ID generator inspired by Twitter's Snowflake |
homepage | https://github.com/houseme/snowflake-rs |
repository | https://github.com/houseme/snowflake-rs |
max_upload_size | |
id | 679982 |
size | 50,196 |
A simple to use rust package to generate or parse Twitter snowflake IDs, generate time sortable 64-bit unique ids for distributed systems (inspired from twitter snowflake)
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.
A Snowflake ID is composed of
Add the following to your Cargo.toml
:
[dependencies]
snowflake_me = "0.3"
use snowflake_me::Snowflake;
let sf = Snowflake::new().unwrap();
let next_id = sf.next_id().unwrap();
println!("{}", next_id);
use snowflake_me::Snowflake;
use chrono::prelude::*;
let sf = Snowflake::builder().start_time(Utc::now()).finalize().unwrap();
let next_id = sf.next_id().unwrap();
println!("{}", next_id);
use snowflake_me::Snowflake;
let sf = Snowflake::builder().machine_id( & | | Ok(42)).finalize().unwrap();
let next_id = sf.next_id().unwrap();
println!("{}", next_id);
use snowflake_me::Snowflake;
let sf = Snowflake::builder().data_center_id( & | | Ok(42)).finalize().unwrap();
let next_id = sf.next_id().unwrap();
println!("{}", next_id);
use snowflake_me::{decompose, Snowflake};
let sf = Snowflake::new().unwrap();
let next_id = sf.next_id().unwrap();
let parts = decompose(next_id);
println!("timestamp: {}, machine_id: {}, sequence: {}, data_center_id:{}", parts.time, parts.machine_id, parts.sequence, parts.data_center_id);
Run them yourself with cargo bench
.
test bench_decompose ... bench: 651 ns/iter (+/- 251)
test bench_new ... bench: 795,722 ns/iter (+/- 371,556)
test bench_next_id ... bench: 36,652 ns/iter (+/- 1,105)
test bench_decompose ... bench: 1,066 ns/iter (+/- 132)
test bench_new ... bench: 738,129 ns/iter (+/- 318,192)
test bench_next_id ... bench: 37,390 ns/iter (+/- 499)
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.