From 7f2c7edf0e7f91229be17bda52d0e74d42d862e2 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Wed, 10 Aug 2022 18:14:33 -0700 Subject: [PATCH] ulid: add bytes() method to get array directly since they are known-size --- ulid.zig | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ulid.zig b/ulid.zig index 2b54d654fc149f2d9f76816123ad86853cc8a189..71ef3b997d91e5dc3a8be05dc142f3924bca1f83 100644 --- a/ulid.zig +++ b/ulid.zig @@ -56,13 +56,16 @@ pub const ULID = struct { pub const readField = parse; pub const bindField = toString; - pub fn format(self: ULID, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { - _ = fmt; - _ = options; - + pub fn bytes(self: ULID) [26]u8 { var buf: [26]u8 = undefined; base32.formatInt(u48, self.timestamp, buf[0..10]); base32.formatInt(u80, self.randomnes, buf[10..26]); - try writer.writeAll(&buf); + return buf; + } + + pub fn format(self: ULID, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { + _ = fmt; + _ = options; + try writer.writeAll(&self.bytes()); } }; -- 2.54.0