Skip to content

An RFC 9562 complient UUID library for ZIG

License

Notifications You must be signed in to change notification settings

KeithBrown39423/zuid

Repository files navigation



Banner


ZUID

A simple UUID library for ZIG

FeaturesInstallationExamplesContributing

This library provides a simple and efficient way to generate and manipulate UUIDs (Universally Unique Identifiers) in Zig.

Features

  • Generate UUIDs of most versions (1, 3, 4, 5, 6, 7, and 8)
  • Parse UUIDs from strings
  • Create UUIDs from binary arrays
  • Convert UUIDs to strings, 128-bit integers, and byte-arrays
  • Access to parts of UUID (set_1, version, variant, etc.)

Installation

To install this library, add the following to your build.zig file:

pub fn build(b: *std.Build) void {
    // ...
    const zuid_dep = b.dependency("zuid", .{});
    const zuid_mod = zuid_dep.module("zuid");

    exe.root_module.addImport("zuid", zuid_mod);
    // ...
}

Also make sure to add the following to your build.zig.zon file:

zig fetch --save https://github.com/KeithBrown39423/zuid/archive/refs/tags/v2.0.0.tar.gz

Examples

Here is a simple example of how to generate a UUID:

const std = @import("std");
const zuid = @import("zuid");

pub fn main() !void {
    const uuid = zuid.new.v4();

    std.debug.print("UUID: {s}\n", .{ uuid });
}

If you are creating a v3 or v5 UUID, make sure to include the namespace and data.

const std = @import("std");
const zuid = @import("zuid");

pub fn main() !void {
    const uuid = zuid.new.v5(zuid.UuidNamespace.URL, "https://example.com");

    std.debug.print("UUID: {s}\n", .{ uuid });
}

You can also get the UUID as an int through @bitCast.

const std = @import("std");
const zuid = @import("zuid");

pub fn main() !void {
    const uuid = zuid.new.v4();

    std.debug.print("UUID: {s}\n", .{ uuid });
    const uuid_int = @as(u128, @bitCast(uuid));
    std.debug.print("UUID as int: {d}\n", .{ uuid_int });

    // or

    std.debug.print("UUID: {d}\n", .{ uuid });
}

Contributing

Contributions are welcome! Please submit a pull request or create an issue to get started.

(ZUID is protected by the MIT licence)