mr-ulid 3.0.0

Robust and Hassle-Free ULIDs (Universally Unique Lexicographically Sortable Identifier)
Documentation

mr-ulid

Crates.io Documentation Dependencies License

A Rust implementation of ULIDs (Universally Unique Lexicographically Sortable Identifiers) with a focus on correctness and ease of use.

Generated ULIDs are guaranteed to be unique and strictly monotonically increasing, even across threads. The random component is overflow-proof by design -- see Overflow Protection for details.

Usage

[dependencies]
mr-ulid = "3"
use mr_ulid::Ulid;

let u = Ulid::new();
println!("{u}");

let s = u.to_string();
let parsed: Ulid = s.parse().unwrap();
assert_eq!(u, parsed);

Features

  • Overflow-proof generation -- At least 1010 ULIDs per millisecond without overflow or failure.
  • Strict monotonicity -- Thread-safe generation; every ULID is greater than the previous one.
  • Non-zero type (Ulid) -- Wraps NonZero<u128>, so Option<Ulid> is the same size as Ulid (16 bytes).
  • Zeroable type (ZeroableUlid) -- For use cases that need a zero sentinel value.
  • Crockford Base32 -- Case-insensitive encoding with automatic i/l to 1 and o to 0 disambiguation.
  • Optional serde support -- Enable the serde feature for string-based serialization.
  • Custom entropy sources -- Swap in your own RNG via the EntropySource trait.
  • Minimal dependencies -- Only rand (enabled by default). Disable with default-features = false.

Serde

Enable the serde feature for JSON (and other format) support:

[dependencies]
mr-ulid = { version = "3", features = ["serde"] }
use mr_ulid::Ulid;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Record {
    id: Ulid,
    data: String,
}

ULIDs are serialized as 26-character Crockford Base32 strings.

Overflow Protection

The 80-bit random component is reduced by 1010 values (a ~0.000000000001% reduction in entropy). This reserves enough space to guarantee at least 1010 monotonically increasing ULIDs per millisecond -- equivalent to 1013 per second -- without overflow or failure. This exceeds the capability of current hardware by orders of magnitude.

Changelog

See CHANGELOG.md for version history.

License

MIT