From e62e42f0d901fd1b053b22ecd14a42bdac7dbac4 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 17 Jul 2025 09:42:53 -0700 Subject: [PATCH] std.io.Writer: remove requirement of a 2-byte buffer for extern unions (#24489) closes #24486 --- lib/std/Io/Writer.zig | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/std/Io/Writer.zig b/lib/std/Io/Writer.zig index 0cebde1e3f..4b0e142fb0 100644 --- a/lib/std/Io/Writer.zig +++ b/lib/std/Io/Writer.zig @@ -618,10 +618,6 @@ pub fn writeAllPreserve(w: *Writer, preserve_length: usize, bytes: []const u8) E /// A user type may be a `struct`, `vector`, `union` or `enum` type. /// /// To print literal curly braces, escape them by writing them twice, e.g. `{{` or `}}`. -/// -/// Asserts `buffer` capacity of at least 2 if a union is printed. This -/// requirement could be lifted by adjusting the code, but if you trigger that -/// assertion it is a clue that you should probably be using a buffer. pub fn print(w: *Writer, comptime fmt: []const u8, args: anytype) Error!void { const ArgsType = @TypeOf(args); const args_type_info = @typeInfo(ArgsType); @@ -1257,14 +1253,13 @@ pub fn printValue( .@"extern", .@"packed" => { if (info.fields.len == 0) return w.writeAll(".{}"); try w.writeAll(".{ "); - inline for (info.fields) |field| { + inline for (info.fields, 1..) |field, i| { try w.writeByte('.'); try w.writeAll(field.name); try w.writeAll(" = "); try w.printValue(ANY, options, @field(value, field.name), max_depth - 1); - (try w.writableArray(2)).* = ", ".*; + try w.writeAll(if (i < info.fields.len) ", " else " }"); } - w.buffer[w.end - 2 ..][0..2].* = " }".*; }, } },