feature. See also
. The project being documented here (as the example) is the Zig library itself.
Serializer.checkValueDepth
fn checkValueDepth(val: anytype, depth: usize) error
File
Code
fn checkValueDepth(val: anytype, depth: usize) error{ExceededMaxDepth}!void {
if (depth == 0) return error.ExceededMaxDepth;
const child_depth = depth - 1;
switch (@typeInfo(@TypeOf(val))) {
.pointer => |pointer| switch (pointer.size) {
.one => try checkValueDepth(val.*, child_depth),
.slice => for (val) |item| {
try checkValueDepth(item, child_depth);
},
.c, .many => {},
},
.array => for (val) |item| {
try checkValueDepth(item, child_depth);
},
.@"struct" => |@"struct"| inline for (@"struct".field_names) |field_name| {
try checkValueDepth(@field(val, field_name), child_depth);
},
.@"union" => |@"union"| if (@"union".tag_type == null) {
return;
} else switch (val) {
inline else => |payload| {
return checkValueDepth(payload, child_depth);
},
},
.optional => if (val) |inner| try checkValueDepth(inner, child_depth),
else => {},
}
}