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...@@ -122,7 +122,7 @@ pub fn valueMaxDepth(self: *Serializer, val: anytype, options: ValueOptions, dep
122122
123/// Serialize a value, similar to `serializeArbitraryDepth`.123/// Serialize a value, similar to `serializeArbitraryDepth`.
124pub fn valueArbitraryDepth(self: *Serializer, val: anytype, options: ValueOptions) Error!void {124pub fn valueArbitraryDepth(self: *Serializer, val: anytype, options: ValueOptions) Error!void {
125 comptime assert(canSerializeType(@TypeOf(val)));125 comptime assertCanSerializeType(@TypeOf(val));
126 switch (@typeInfo(@TypeOf(val))) {126 switch (@typeInfo(@TypeOf(val))) {
127 .int, .comptime_int => if (options.emit_codepoint_literals.emitAsCodepoint(val)) |c| {127 .int, .comptime_int => if (options.emit_codepoint_literals.emitAsCodepoint(val)) |c| {
128 self.codePoint(c) catch |err| switch (err) {128 self.codePoint(c) catch |err| switch (err) {
...@@ -321,7 +321,7 @@ pub fn tupleArbitraryDepth(...@@ -321,7 +321,7 @@ pub fn tupleArbitraryDepth(
321}321}
322322
323fn tupleImpl(self: *Serializer, val: anytype, options: ValueOptions) Error!void {323fn tupleImpl(self: *Serializer, val: anytype, options: ValueOptions) Error!void {
324 comptime assert(canSerializeType(@TypeOf(val)));324 comptime assertCanSerializeType(@TypeOf(val));
325 switch (@typeInfo(@TypeOf(val))) {325 switch (@typeInfo(@TypeOf(val))) {
326 .@"struct" => {326 .@"struct" => {
327 var container = try self.beginTuple(.{ .whitespace_style = .{ .fields = val.len } });327 var container = try self.beginTuple(.{ .whitespace_style = .{ .fields = val.len } });
...@@ -814,6 +814,10 @@ test checkValueDepth {...@@ -814,6 +814,10 @@ test checkValueDepth {
814 try expectValueDepthEquals(3, @as([]const []const u8, &.{&.{ 1, 2, 3 }}));814 try expectValueDepthEquals(3, @as([]const []const u8, &.{&.{ 1, 2, 3 }}));
815}815}
816816
817inline fn assertCanSerializeType(T: type) void {
818 if (!canSerializeType(T)) @compileError("cannot serialize: " ++ @typeName(T));
819}
820
817inline fn canSerializeType(T: type) bool {821inline fn canSerializeType(T: type) bool {
818 comptime return canSerializeTypeInner(T, &.{}, false);822 comptime return canSerializeTypeInner(T, &.{}, false);
819}823}