std: Better formatting of tuple types

Skip the useless type name and the numeric field names.
This commit is contained in:
LemonBoy 2021-06-06 12:39:07 +02:00 committed by Veikka Tuominen
parent 9ac6d28614
commit ec4e67fb0e

View File

@ -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" {