Crates.io | snowflake-rs-impl |
lib.rs | snowflake-rs-impl |
version | 1.0.1 |
source | src |
created_at | 2024-08-06 11:46:16.918177 |
updated_at | 2024-08-07 14:17:08.255665 |
description | A Rust implementation of Twitter's Snowflake ID generation algorithm |
homepage | |
repository | https://github.com/bytemaker-io/snowflake-rs |
max_upload_size | |
id | 1327192 |
size | 16,389 |
A Rust implementation of the Snowflake ID generator, which produces unique 64-bit IDs. This implementation ensures thread safety and high performance, suitable for distributed systems.
Each Snowflake ID consists of three parts:
[dependencies]
snowflake-rs-impl="*"
use snowflake::Snowflake;
fn main() {
// Create a new Snowflake instance with node ID 1 and default epoch
let snowflake = Snowflake::new(1, None).unwrap();
// Generate a new ID
let id = snowflake.generate().unwrap();
println!("Generated ID: {}", id);
}
use snowflake::Snowflake;
fn main() {
// Custom epoch (2023-01-01T00:00:00Z in milliseconds since Unix epoch)
let custom_epoch = 1672531200000;
let snowflake = Snowflake::new(1, Some(custom_epoch)).unwrap();
// Generate a new ID
let id = snowflake.generate().unwrap();
println!("Generated ID: {}", id);
}
This library includes tests to verify the correct functionality of the Snowflake ID generator.
cargo test
cargo bench
Approximately 4,100,000 IDs per second