--- layout: site.njk title: zul.UUID ---

Parse and generate version 4 and version 7 UUIDs.

{% highlight zig %} // v4() returns a zul.UUID const uuid1 = zul.UUID.v4(); // toHex() returns a [36]u8 const hex = uuid1.toHex(.lower); // returns a zul.UUID (or an error) const uuid2 = try zul.UUID.parse("761e3a9d-4f92-4e0d-9d67-054425c2b5c3"); std.debug.print("{any}\n", uuid1.eql(uuid2)); // create a UUIDv7 const uuid3 = zul.UUID.v7(); // zul.UUID can be JSON serialized try std.json.stringify(.{.id = uuid3}, .{}, writer); {% endhighlight %}

zul.UUID is a thin wrapper around a [16]u8. Its main purpose is to generate a hex-encoded version of the UUID.

bin: [16]u8

The binary representation of the UUID.

UUID.v4() UUID

Generate a version 4 (random) UUID.

UUID.v7() UUID

Generate a version 7 UUID.

UUID.random() UUID

Non-compliant pseudo-UUID. Does not have a version or variant. Use UUID.v4() or UUID.v7() unless you have specific reasons not to.

UUID.parse(hex: []const u8) !UUID

Attempts to parse a hex-encoded UUID. Returns error.InvalidUUID is the UUID is not valid.

UUID.bin2Hex(bin: []const u8, case: Case) ![36]iu8

Hex encodes a 16 byte binary UUID.

Hex-encodes the UUID. The case parameter must be .lower or .upper and controls whether lowercase or uppercase hexadecimal is used.

This method should be preferred over the other toHex* variants.

Hex-encodes the UUID into a heap-allocated buffer. The caller must free the returned value when it is no longer in use.

Hex-encodes the UUID into buf, which must have a length equal or greater than 36.