From ec4e67fb0e47ff742d0fd76c6b00b52c2a7983e4 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sun, 6 Jun 2021 12:39:07 +0200 Subject: [PATCH] std: Better formatting of tuple types Skip the useless type name and the numeric field names. --- lib/std/fmt.zig | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 9ab655852c..50def46838 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -495,6 +495,22 @@ pub fn formatType( } }, .Struct => |info| { + if (info.is_tuple) { + // Skip the type and field names when formatting tuples. + if (max_depth == 0) { + return writer.writeAll("{ ... }"); + } + try writer.writeAll("{"); + inline for (info.fields) |f, i| { + if (i == 0) { + try writer.writeAll(" "); + } else { + try writer.writeAll(", "); + } + try formatType(@field(value, f.name), ANY, options, writer, max_depth - 1); + } + return writer.writeAll(" }"); + } try writer.writeAll(@typeName(T)); if (max_depth == 0) { return writer.writeAll("{ ... }"); @@ -2164,6 +2180,10 @@ test "struct" { }; try expectFmt("S{ .a = 456, .b = error.Unused }", "{}", .{inst}); + // Tuples + try expectFmt("{ }", "{}", .{.{}}); + try expectFmt("{ -1 }", "{}", .{.{-1}}); + try expectFmt("{ -1, 42, 2.5e+04 }", "{}", .{.{ -1, 42, 0.25e5 }}); } test "union" {