uuid.zig
octopus-foundation/uuid.zig
MIT
UUID v4 implementation in pure Zig. No allocations
7 7 0 1
2
zig, zig-package
build.zig.zon
build.zig
View on Github
octopus-foundation/uuid.zig
UUID v4 implementation in pure Zig. No allocations
RFC 4122 compliant UUID v4 implementation in Zig.
zig fetch --save https://github.com/octopus-foundation/uuid.zig/archive/refs/tags/0.0.0.tar.gz
In your build.zig
:
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const uuid = b.dependency("uuid", .{
.target = target,
.optimize = optimize,
});
const exe = b.addExecutable(.{
.name = "example",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("uuid", uuid.module("uuid"));
b.installArtifact(exe);
}
const uuid = @import("uuid").v4;
// Generate random UUID
var prng = std.Random.DefaultPrng.init(0);
const id = uuid.random(prng.random());
// Format as string
var buf: [36]u8 = undefined;
uuid.toString(id, &buf);
// "550e8400-e29b-41d4-a716-446655440000"
// Parse from string
const parsed = try uuid.fromString("550e8400-e29b-41d4-a716-446655440000");
// Convert to/from bytes
var bytes: [16]u8 = undefined;
uuid.toBytes(id, &bytes);
const fromBytes = try uuid.fromBytes(&bytes);
// With crypto random
const crypto_id = uuid.random(std.crypto.random);