ddn.ulid
ddn.ulid — Universally Unique Lexicographically Sortable Identifiers.
A ULID is a 128-bit identifier: a 48-bit Unix timestamp in milliseconds followed by 80 random bits. It is written as 26 characters of Crockford base32 (01ARZ3NDEKTSV4RRFFQ69G5FAV) or stored as 16 big-endian bytes, and both forms sort by creation time. That makes ULIDs good database keys and log identifiers, and every ULID converts losslessly to a UUID.
Specification: https://github.com/ulid/spec
- Ulid is the value type: parse, format, compare, hash and
convert it to bytes or a
UUID.Ulid.generatemakes a fresh one from the system clock and the operating system's random number generator. - UlidGenerator makes monotonic ULIDs: within one millisecond
it increments the random part instead of drawing a new one, so the values keep increasing.
Generation and the value codec are @nogc nothrow; toString() allocates, and sink formatting inherits the sink's attributes. Failures come back as UlidResult values carrying a UlidError. The codec is pure and also runs at compile time. Parsing is strict: exactly 26 characters from the Crockford alphabet, in either case, and no value above 128 bits (first character above 7), as the specification requires.
Randomness comes from ddn.os.entropy: getrandom on Linux, arc4random_buf on FreeBSD, BCryptGenRandom on Windows. It is drawn through a small per-thread buffer that a fork child discards. On other platforms ULIDs can be parsed, formatted and built from explicit parts, but generation reports UlidError.ENTROPY_FAILURE. Design record: ADR-2026-11-26-01 and ADR-2026-11-26-02.
Example:
import ddn.ulid;
auto id = Ulid.generate().value(); // fresh ULID, current time
string text = id.toString(); // 26 characters, e.g. "01KB..."
assert(Ulid.parse(text).value() == id); // lossless round trip
UlidGenerator gen; // monotonic within a millisecond
auto a = gen.next().value();
auto b = gen.next().value();
assert(a < b);
auto bad = Ulid.parse("8ZZZZZZZZZZZZZZZZZZZZZZZZZ");
assert(!bad && bad.error() == UlidError.VALUE_OVERFLOW);Types 4
A ULID: a 48-bit millisecond timestamp and 80 random bits, 128 bits in all.
Values compare, sort and hash by their 128-bit value. That order is the same as the order of their bytes and of their canonical text, so it is creation order to the millisecond. Ulid.init is the nil ULID, Ulid.MIN. A Ulid is a plain 16-byte value; copying is free.
size_t TEXT_LENGTHLength of the canonical text form, in characters.size_t BYTE_LENGTHLength of the binary form, in bytes.size_t RANDOM_LENGTHLength of the random component, in bytes.ulong MAX_TIMESTAMPLargest timestamp a ULID can hold: 2^48 - 1 ms, in the year 10889.Ulid MINThe smallest ULID, `00000000000000000000000000`; equal to `Ulid.init`.Ulid MAXThe largest ULID, `7ZZZZZZZZZZZZZZZZZZZZZZZZZ`.UlidResult fromParts(ulong timestampMs, const ubyte[RANDOM_LENGTH] randomness) static @safe pure nothrow @nogcBuilds a ULID from a timestamp and an explicit random component.Ulid fromBytes(const ubyte[BYTE_LENGTH] data) static @safe pure nothrow @nogcBuilds a ULID from its 16-byte binary form (most significant byte first). Every 128-bit value is a valid ULID, so this cannot fail. A slice with compile-time bounds converts implicitly: `Ulid.fromB...UlidResult parse(scope const(char)[] text) static @safe pure nothrow @nogcParses the canonical text form.Ulid fromUuid(const UUID uuid) static @safe pure nothrow @nogcConverts a `UUID` with the same 128-bit value. Every UUID is a valid ULID bit pattern, although its first 48 bits are only a meaningful timestamp if it was made from a ULID (or is a version 7 UUID).UlidResult generate() static @safe nothrow @nogcGenerates a ULID for the current time, with 80 fresh random bits from the operating system's cryptographically secure random number generator. Two calls in the same millisecond produce unrelated va...UlidResult generate(ulong timestampMs) static @safe nothrow @nogcGenerates a ULID for a given time, with 80 fresh random bits from the operating system.ulong timestamp() const @safe pure nothrow @nogcReturns: the timestamp, in milliseconds since the Unix epoch.ubyte[RANDOM_LENGTH] randomness() const @safe pure nothrow @nogcReturns: the 80-bit random component, most significant byte first (bytes 6 to 15 of the binary form).ubyte[BYTE_LENGTH] bytes() const @safe pure nothrow @nogcReturns: the 16-byte binary form, most significant byte first, as the specification defines it; byte order does not depend on the machine.UUID toUuid() const @safe pure nothrow @nogcReturns: a `UUID` with the same 128-bit value. The UUID's version and variant fields hold whatever bits the ULID has there; the conversion is for storage in UUID columns, not to make an RFC 9562 UUID.char[TEXT_LENGTH] toChars() const @safe pure nothrow @nogcFormats the canonical text form without allocating.string toString() const @safe pure nothrowReturns: the canonical text form, in a new GC-allocated string.void toString(W)(ref W sink) if (isOutputRange!(W, char)) constWrites the canonical text form to an output range without allocating. This is the overload `std.format` and `writeln` use.this(ulong hi, ulong lo)Builds a ULID from its two native words. Private: it also disables the struct literal `Ulid(hi, lo)`, which would otherwise expose the internal representation to other modules.Why a ULID operation failed.
Generates monotonic ULIDs.
The first ULID, and the first in every later millisecond, gets fresh randomness. A ULID requested in the same millisecond as the previous one, or in an earlier one because the wall clock was set back, is the previous ULID plus one, carrying across the whole 80-bit random component, as the specification describes. Its timestamp then stays at the last one issued, so the generator's output is strictly increasing even when the clock steps backwards. After 2^80 values in one millisecond, or fewer when the fresh random part started high, next reports UlidError.RANDOM_OVERFLOW and leaves the state unchanged until time moves on.
The ordering guarantee holds per generator. An instance is not thread-safe: give each thread its own generator, or guard a shared one with a lock.
Ulid last() const @safe pure nothrow @nogcReturns: the last ULID issued, or the one given to the constructor; `Ulid.MIN` for a fresh generator.UlidResult next(ulong timestampMs) @safe nothrow @nogcGenerates the next ULID for a given time. Fresh randomness comes from the operating system, and only when the timestamp moves past the last one.UlidResult next(ulong timestampMs, const ubyte[Ulid.RANDOM_LENGTH] randomness) @safe pure nothrow @nogcGenerates the next ULID for a given time with caller-supplied randomness, for a custom random source or for reproducible sequences.