web: Update UUID code to zig 0.16.0

This commit is contained in:
2026-06-24 13:15:57 +02:00
parent d2719a77f0
commit 91020a2741

View File

@@ -5,9 +5,10 @@ bytes: [16]u8,
pub const zero: UUID = .{ .bytes = @splat(0) };
pub fn v4() UUID {
pub fn v4(io: std.Io) UUID {
var bytes: [16]u8 = undefined;
std.crypto.random.bytes(&bytes);
io.random(&bytes);
bytes[6] = (bytes[6] & 0x0F) | 0x40;
bytes[8] = (bytes[8] & 0x3F) | 0x80;
return .{ .bytes = bytes };
@@ -35,7 +36,7 @@ pub const namespaces = struct {
pub fn v5(namespace: UUID, name: []const u8) UUID {
const hash = blk: {
var hasher = std.crypto.hash.Sha1.init(.{});
hasher.update(namespace);
hasher.update(namespace.bytes);
hasher.update(name);
break :blk hasher.finalResult();
};
@@ -47,13 +48,13 @@ pub fn v5(namespace: UUID, name: []const u8) UUID {
return .{ .bytes = bytes };
}
var v7_lock: std.Thread.Mutex = .{};
var v7_last_timestamp: std.atomic.Value(u64) = .{ .raw = 0 };
var v7_counter: std.atomic.Value(u32) = .{ .raw = 0 };
var v7_lock: std.Io.Mutex = .init;
var v7_last_timestamp: std.atomic.Value(u64) = .init(0);
var v7_counter: std.atomic.Value(u32) = .init(0);
fn getCount(timestamp: u64) u32 {
v7_lock.lock();
defer v7_lock.unlock();
fn getCount(io: std.Io, timestamp: u64) u32 {
v7_lock.lockUncancelable(io);
defer v7_lock.unlock(io);
if (v7_last_timestamp.swap(timestamp, .monotonic) != timestamp) {
v7_counter.store(0, .monotonic);
@@ -62,12 +63,12 @@ fn getCount(timestamp: u64) u32 {
return v7_counter.fetchAdd(1, .monotonic) % 4096;
}
pub fn v7() UUID {
pub fn v7(io: std.Io) UUID {
const timestamp: u64 = @intCast(@max(0, std.time.milliTimestamp()));
const count = getCount(timestamp);
const random = blk: {
var bytes: [8]u8 = undefined;
std.crypto.random.bytes(&bytes);
io.random(&bytes);
break :blk bytes;
};