| Crates.io | snowflake-id-generator |
| lib.rs | snowflake-id-generator |
| version | 0.4.0 |
| created_at | 2025-09-12 14:36:44.573091+00 |
| updated_at | 2025-12-19 13:30:31.077927+00 |
| description | Simple snowflake id implementation |
| homepage | |
| repository | https://github.com/Axoneo/snowflake-id-rs |
| max_upload_size | |
| id | 1835888 |
| size | 25,421 |
A compact, easy-to-use Rust implementation of a Snowflake-style 64-bit ID generator. This crate provides both single-threaded and multi-threaded generators, and supports synchronous (blocking) and asynchronous (non-blocking) usage patterns.
Key goals:
Example (async, multi-threaded):
use snowflake_id_generator::multi_thread::async_generator::SnowflakeGenerator;
#[tokio::main]
async fn main() {
// worker_id, custom_epoch_ms
let generator = SnowflakeGenerator::new(0, 1).expect("valid generator");
let id = generator.generate_id().await;
println!("Generated ID: {}", id);
}
Example (sync, single-threaded):
use snowflake_id_generator::single_thread::sync_generator::SnowflakeGenerator;
fn main() {
let mut generator = SnowflakeGenerator::new(0, 1).expect("valid generator");
let id = generator.generate_id().expect("generate id");
println!("Generated ID: {}", id);
}
See the src/ modules for more generators and utilities:
src/common.rs — core SnowflakeState, bit layout and helperssrc/single_thread.rs — single-threaded sync/async generatorssrc/multi_thread.rs — multi-threaded sync/async generatorsThe 64-bit ID layout used by this generator (from most-significant bit to least):
This layout allows unique IDs across workers and within the same millisecond.
Common error conditions provided by the crate include:
Refer to the crate's error types in src/common.rs for exact variants and Display messages.
Contributions are welcome. Please:
This project is licensed under the terms in the repository's LICENSE file.