diff options
| author | Chadwain Holness <39384757+chadwain@users.noreply.github.com> | 2024-02-03 16:47:55 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-02-03 21:47:55 +0000 |
| commit | 60308550978be821515323256af17e0b8a610b1c (patch) | |
| tree | 20250d90d9eda8f0279fb751829cbc868d99d6d6 /lib/std/fmt.zig | |
| parent | 122387943bf1d00c85aba77f37c93072e43140c9 (diff) | |
| download | zig-60308550978be821515323256af17e0b8a610b1c.tar.gz zig-60308550978be821515323256af17e0b8a610b1c.zip | |
std.fmt: fix formatting slices of structs with custom formatting
`hasMethod` doesn't make sense for pointers to more than one item.
Diffstat (limited to 'lib/std/fmt.zig')
| -rw-r--r-- | lib/std/fmt.zig | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index f89a205064..f1f38e2db6 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -2255,6 +2255,24 @@ test "slice" { try expectFmt("int: { 1, 1000, 5fad3, 423a35c7 }", "int: {x}", .{int_slice[runtime_zero..]}); try expectFmt("int: { 00001, 01000, 5fad3, 423a35c7 }", "int: {x:0>5}", .{int_slice[runtime_zero..]}); } + { + const S1 = struct { + x: u8, + }; + const struct_slice: []const S1 = &[_]S1{ S1{ .x = 8 }, S1{ .x = 42 } }; + try expectFmt("slice: { fmt.test.slice.S1{ .x = 8 }, fmt.test.slice.S1{ .x = 42 } }", "slice: {any}", .{struct_slice}); + } + { + const S2 = struct { + x: u8, + + pub fn format(s: @This(), comptime _: []const u8, _: std.fmt.FormatOptions, writer: anytype) !void { + try writer.print("S2({})", .{s.x}); + } + }; + const struct_slice: []const S2 = &[_]S2{ S2{ .x = 8 }, S2{ .x = 42 } }; + try expectFmt("slice: { S2(8), S2(42) }", "slice: {any}", .{struct_slice}); + } } test "escape non-printable" { |
