mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-06 13:54:21 +00:00
std.json: support parsing json at comptime using FixedBufferAllocator (#16488)
This commit is contained in:
parent
2ad16248d7
commit
c72a9feabe
2 changed files with 27 additions and 2 deletions
|
|
@ -436,7 +436,7 @@ pub const FixedBufferAllocator = struct {
|
|||
const self: *FixedBufferAllocator = @ptrCast(@alignCast(ctx));
|
||||
_ = log2_buf_align;
|
||||
_ = return_address;
|
||||
assert(self.ownsSlice(buf)); // sanity check
|
||||
assert(@inComptime() or self.ownsSlice(buf));
|
||||
|
||||
if (!self.isLastAllocation(buf)) {
|
||||
if (new_size > buf.len) return false;
|
||||
|
|
@ -465,7 +465,7 @@ pub const FixedBufferAllocator = struct {
|
|||
const self: *FixedBufferAllocator = @ptrCast(@alignCast(ctx));
|
||||
_ = log2_buf_align;
|
||||
_ = return_address;
|
||||
assert(self.ownsSlice(buf)); // sanity check
|
||||
assert(@inComptime() or self.ownsSlice(buf));
|
||||
|
||||
if (self.isLastAllocation(buf)) {
|
||||
self.end_index -= buf.len;
|
||||
|
|
|
|||
|
|
@ -901,3 +901,28 @@ test "json parse allocate when streaming" {
|
|||
try testing.expectEqualSlices(u8, parsed.not_const, "non const string");
|
||||
try testing.expectEqualSlices(u8, parsed.is_const, "const string");
|
||||
}
|
||||
|
||||
test "parse at comptime" {
|
||||
const doc =
|
||||
\\{
|
||||
\\ "vals": {
|
||||
\\ "testing": 1,
|
||||
\\ "production": 42
|
||||
\\ },
|
||||
\\ "uptime": 9999
|
||||
\\}
|
||||
;
|
||||
const Config = struct {
|
||||
vals: struct { testing: u8, production: u8 },
|
||||
uptime: u64,
|
||||
};
|
||||
const config = comptime x: {
|
||||
var buf: [32]u8 = undefined;
|
||||
var fba = std.heap.FixedBufferAllocator.init(&buf);
|
||||
const res = parseFromSliceLeaky(Config, fba.allocator(), doc, .{});
|
||||
// Assert no error can occur since we are
|
||||
// parsing this JSON at comptime!
|
||||
break :x res catch unreachable;
|
||||
};
|
||||
comptime testing.expectEqual(@as(u64, 9999), config.uptime) catch unreachable;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue