pub struct IdGenerator {
pub node_id: u16,
pub shard_id: u8,
}Expand description
The core generator struct for producing unique IDs.
This struct holds node and shard identifiers, which are incorporated into 64, 128, and 256-bit IDs to ensure uniqueness in a distributed environment.
While you can create an IdGenerator instance, the library is designed
to be used through the static methods on AtomicId, which manage a global generator.
Fields§
§node_id: u16Node identifier (0-4095), used in 64, 128, and 256-bit IDs.
shard_id: u8Shard identifier (0-255), used in 64, 128, and 256-bit IDs.
Implementations§
Source§impl IdGenerator
impl IdGenerator
Sourcepub fn new(node_id: u16, shard_id: u8) -> Self
pub fn new(node_id: u16, shard_id: u8) -> Self
Create a new generator with the given node and shard IDs.
§Arguments
node_id- Node identifier (0..=4095).shard_id- Shard identifier (0..=255).
§Returns
A new IdGenerator instance.
Sourcepub fn gen24(&self) -> u32
pub fn gen24(&self) -> u32
Generate a 24-bit unique ID.
The ID is generated from a single atomic counter that wraps around. It is suitable for short-lived, high-throughput scenarios where a small ID is needed.
- Structure: 24 bits for the sequence.
- Uniqueness: Guarantees up to 16.7 million (2^24) unique IDs before the counter wraps around.
§Returns
A 24-bit unique ID as a u32.
Sourcepub fn gen32(&self) -> u32
pub fn gen32(&self) -> u32
Generate a 32-bit unique ID.
This ID combines a thread-specific identifier with a sequence number,
providing better collision resistance in multi-threaded applications than gen24.
- Structure: 8 bits for the thread ID | 24 bits for the sequence.
§Returns
A 32-bit unique ID as a u32.
Sourcepub fn gen64(&self) -> u64
pub fn gen64(&self) -> u64
Generate a 64-bit unique ID, inspired by Twitter’s Snowflake.
This ID is ideal for distributed systems, as it combines a timestamp, node/shard identifiers, and a sequence number to ensure global uniqueness.
- Structure: 20-bit timestamp | 12-bit node ID | 8-bit shard ID | 8-bit thread ID | 16-bit sequence.
- Timestamp: Milliseconds since the custom epoch, providing a lifespan of ~34 years (2^20 ms).
- Node ID: Supports up to 4096 nodes (2^12).
- Shard ID: Supports up to 256 shards per node (2^8).
- Sequence: Supports up to 65,536 IDs per millisecond per thread (2^16).
§Returns
A 64-bit unique ID as a u64.
Sourcepub fn gen128(&self) -> u128
pub fn gen128(&self) -> u128
Generate a 128-bit unique ID with enhanced collision resistance.
This ID uses a two-part structure to maximize entropy, combining a timestamp-based part with a high-entropy part derived from nanoseconds and sequence numbers.
- High 64 bits: 32-bit timestamp | 12-bit node | 8-bit shard | 8-bit thread | 4-bit reserved.
- Low 64 bits: 32-bit nanoseconds | 24-bit sequence | 8-bit rotated thread ID.
§Returns
A 128-bit unique ID as a u128.
Sourcepub fn gen256(&self) -> [u64; 4]
pub fn gen256(&self) -> [u64; 4]
Generate a 256-bit unique ID for maximum entropy and uniqueness.
This ID is constructed from four 64-bit parts, each derived from different sources of entropy (timestamps, nanoseconds, node/shard/thread IDs, and sequences). It is suitable for applications requiring cryptographic-level uniqueness.
§Returns
An array of four u64 values representing the 256-bit ID.