define_ulid

Macro define_ulid 

Source
macro_rules! define_ulid {
    (
        $(#[$meta:meta])*
        $name:ident, $int:ty,
        reserved: $reserved_bits:expr,
        timestamp: $timestamp_bits:expr,
        random: $random_bits:expr
    ) => { ... };
}
Available on crate feature ulid only.
Expand description

A macro for defining a bit layout for a custom Ulid using three required components: reserved, timestamp, and random.

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 (random) 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_ulid!(
    <TypeName>, <IntegerType>,
    reserved: <bits>,
    timestamp: <bits>,
    random: <bits>
);

§Example: A non-monotonic ULID layout

use ferroid::define_ulid;

define_ulid!(
    MyCustomId, u128,
    reserved: 0,
    timestamp: 48,
    random: 80
);

Which expands to the following bit layout:

 Bit Index:  127            80 79           0
             +----------------+-------------+
 Field:      | timestamp (48) | random (80) |
             +----------------+-------------+
             |<-- MSB -- 128 bits -- LSB -->|