IdGenerator

Struct IdGenerator 

Source
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: u16

Node identifier (0-4095), used in 64, 128, and 256-bit IDs.

§shard_id: u8

Shard identifier (0-255), used in 64, 128, and 256-bit IDs.

Implementations§

Source§

impl IdGenerator

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.