authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-02-19 22:06:44-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-05-25 18:54:34-07:00
log052aeee4150eba68051311982d883688670f6ca4
treecbe4ba48df369ef496d953d3af545f11cc1b1838
parentb9aeedd23c303efc3c74a308ed2620ab1c470af7

std.zon.Serializer: slightly more helpful message

when a type is unserializable

1 files changed, 6 insertions(+), 2 deletions(-)

lib/std/zon/Serializer.zig+6-2
......@@ -122,7 +122,7 @@ pub fn valueMaxDepth(self: *Serializer, val: anytype, options: ValueOptions, dep
122122
123123/// Serialize a value, similar to `serializeArbitraryDepth`.
124124pub fn valueArbitraryDepth(self: *Serializer, val: anytype, options: ValueOptions) Error!void {
125 comptime assert(canSerializeType(@TypeOf(val)));
125 comptime assertCanSerializeType(@TypeOf(val));
126126 switch (@typeInfo(@TypeOf(val))) {
127127 .int, .comptime_int => if (options.emit_codepoint_literals.emitAsCodepoint(val)) |c| {
128128 self.codePoint(c) catch |err| switch (err) {
......@@ -321,7 +321,7 @@ pub fn tupleArbitraryDepth(
321321}
322322
323323fn tupleImpl(self: *Serializer, val: anytype, options: ValueOptions) Error!void {
324 comptime assert(canSerializeType(@TypeOf(val)));
324 comptime assertCanSerializeType(@TypeOf(val));
325325 switch (@typeInfo(@TypeOf(val))) {
326326 .@"struct" => {
327327 var container = try self.beginTuple(.{ .whitespace_style = .{ .fields = val.len } });
......@@ -814,6 +814,10 @@ test checkValueDepth {
814814 try expectValueDepthEquals(3, @as([]const []const u8, &.{&.{ 1, 2, 3 }}));
815815}
816816
817inline fn assertCanSerializeType(T: type) void {
818 if (!canSerializeType(T)) @compileError("cannot serialize: " ++ @typeName(T));
819}
820
817821inline fn canSerializeType(T: type) bool {
818822 comptime return canSerializeTypeInner(T, &.{}, false);
819823}