forked from rsepassi/ulid-zig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ulid_example.zig
38 lines (32 loc) · 972 Bytes
/
ulid_example.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
const std = @import("std");
const ulid = @import("ulid");
pub fn main() !void {
{
// Generate a ULID. Uses a thread-local factory.
const id = try ulid.next();
// Access timestamp and random components
_ = id.time;
_ = id.rand;
// base32 encode/decode
const encoded = id.encode();
const decoded = try ulid.Ulid.decode(&encoded);
if (!id.equals(decoded)) {
unreachable;
}
std.debug.print("{s}\n", .{encoded});
// Binary encode/decode (simple bitCast)
const bytes = id.bytes();
const decoded_b = try ulid.Ulid.fromBytes(&bytes);
if (!id.equals(decoded_b)) {
unreachable;
}
// Automatic base32 string formatting
std.debug.print("{s}\n", .{id});
}
// Can also use an explicit factory
{
var factory = ulid.Factory{};
const id = try factory.next();
_ = id;
}
}