Macro define_snowflake_id

Source
macro_rules! define_snowflake_id {
    (
        $(#[$meta:meta])*
        $name:ident, $int:ty,
        reserved: $reserved_bits:expr,
        timestamp: $timestamp_bits:expr,
        machine_id: $machine_bits:expr,
        sequence: $sequence_bits:expr
    ) => { ... };
}
Expand description

§Field Ordering Semantics

The define_snowflake_id! macro defines a bit layout for a custom Snowflake ID using four required components: reserved, timestamp, machine_id, and sequence.

These components are always laid out from most significant bit (MSB) to least significant bit (LSB) - in that exact order.

  • The first field (reserved) occupies the highest bits.
  • The last field (sequence) occupies the lowest bits.
  • The total number of bits must exactly equal the size of the backing integer type (u64, u128, etc.). If it doesn’t, the macro will trigger a compile-time assertion failure.
define_snowflake_id!(
    <TypeName>, <IntegerType>,
    reserved: <bits>,
    timestamp: <bits>,
    machine_id: <bits>,
    sequence: <bits>
);

§Example: A Twitter-like layout

use ferroid::define_snowflake_id;

define_snowflake_id!(
    MyCustomId, u64,
    reserved: 1,
    timestamp: 41,
    machine_id: 10,
    sequence: 12
);

Which expands to the following bit layout:

 Bit Index:  63           63 62            22 21             12 11             0
             +--------------+----------------+-----------------+---------------+
 Field:      | reserved (1) | timestamp (41) | machine ID (10) | sequence (12) |
             +--------------+----------------+-----------------+---------------+
             |<----------- MSB ---------- 64 bits ----------- LSB ------------>|