Skip to main content

Crate snowflake_gen

Crate snowflake_gen 

Source
Expand description

§snowflake-gen

A configurable Snowflake ID generator for Rust.

§Overview

This crate implements the Snowflake algorithm with fully configurable bit allocation so you can tune the balance between:

  • Timestamp range — how far into the future IDs remain valid
  • Throughput — how many IDs per millisecond (sequence bits)
  • Node / machine count — how many distributed generators you can run

The classic Twitter layout is used by default:

FieldBitsMax value
Timestamp41~69 years
Machine ID531
Node ID531
Sequence124 096 / ms

§Quick start

use snowflake_gen::SnowflakeIdGenerator;

let mut idgen = SnowflakeIdGenerator::new(1, 1).unwrap();
let id = idgen.generate().unwrap();
assert!(id > 0);

§Custom layout

use snowflake_gen::{BitLayout, SnowflakeIdGenerator};

// 10 sequence bits → 1 023 IDs/ms, 8 machine + 7 node bits
let layout = BitLayout::new(38, 8, 7, 10).unwrap();
let mut idgen = SnowflakeIdGenerator::with_layout(1, 1, layout).unwrap();
let id = idgen.generate().unwrap();
assert!(id > 0);

Re-exports§

pub use bucket::SnowflakeIdBucket;
pub use error::SnowflakeError;
pub use generator::SnowflakeComponents;
pub use generator::SnowflakeIdGenerator;
pub use global::init;
pub use global::init_with_epoch;
pub use global::is_initialized;
pub use global::next_id;
pub use global::real_time_next_id;
pub use layout::BitLayout;

Modules§

bucket
error
generator
global
layout
prelude
Convenience re-exports.