UUIDs: parse and write every spelling, make versions 1 to 8, and a generator over a clock and randomness you supply.
let id = @moonuuid.parse("{C232AB00-9414-11EC-B3C8-9F6BDECED846}")
id.to_string() // "c232ab00-9414-11ec-b3c8-9f6bdeced846"
id.version() // Some(1)moon add moonbitstack/moonuuid// Name-based ids are the same every time.
let page = @moonuuid.v5(@moonuuid.namespace_url, "https://example.org/")
// Time-ordered ids from a generator over your clock and randomness.
let gen = @moonuuid.Generator::new(
clock=() => now_in_nanoseconds(),
random=n => system_random_bytes(n),
)
let row = gen.v7() // sorts in the order made, even within one millisecond| Version | Made from | Function | Sorts by time |
|---|---|---|---|
| 1 | timestamp, clock sequence, node | v1(ticks~, clock_seq~, node~) · gen.v1(node~) | |
| 3 | MD5 of a namespace and a name | v3(space, name) | |
| 4 | 122 random bits | v4(random) · gen.v4() | |
| 5 | SHA-1 of a namespace and a name | v5(space, name) | |
| 6 | version 1's fields, high time bits first | v6(ticks~, clock_seq~, node~) · gen.v6(node~) | ✓ |
| 7 | Unix milliseconds, a counter and random bits | v7(millis~, random) · gen.v7() | ✓ |
| 8 | 122 bits of your own layout | v8(custom) |
@moonuuid.parse("6ba7b8109dad11d180b400c04fd430c8")
@moonuuid.parse("urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8")| Write | Gives |
|---|---|
| id.to_string() | 6ba7b810-9dad-11d1-80b4-00c04fd430c8 |
| id.hex() | 6ba7b8109dad11d180b400c04fd430c8 |
| id.urn() | urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8 |
| id.bytes() | the 16 bytes, most significant first |
| Json(id) | the canonical text; @json.from_json reads any spelling back |
| Method | Returns |
|---|---|
| variant() | Ncs, Rfc, Microsoft or Future |
| version() | the version for an RFC 9562 id, else None |
| time() | 100-ns ticks since 1582 for versions 1 and 6, milliseconds since 1970 for version 7 |
| clock_seq() | the 14-bit clock sequence |
| node() | the 48-bit node |
| Refused | When |
|---|---|
| Malformed(at~, reason~) | text that is not 32 hexadecimal digits; at is where it went wrong |
| Size(want~, got~) | bytes, or a random source, of the wrong length |
pub struct Generator {
// private fields
}let gregorian_offset : Int64fn ticks_of(unix_nanos : Int64) -> Int64Install
Download zipUUIDs: parse and write every spelling, make versions 1 to 8, and a generator over a clock and randomness you supply.