authorgravatar for mrjbq7@gmail.comJohn Benediktsson <mrjbq7@gmail.com> 2025-07-17 09:42:53-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-07-17 16:42:53+00:00
loge62e42f0d901fd1b053b22ecd14a42bdac7dbac4
tree5ae070e8f06ad0edd812f1630f50801a5946147a
parenta8dc32e4ec5a1370b0b8d1d0ae3bdc5ad3add49f
signaturebadge-check Signed by PGP key B5690EEEBB952194

std.io.Writer: remove requirement of a 2-byte buffer for extern unions (#24489)

closes #24486

1 files changed, 2 insertions(+), 7 deletions(-)

lib/std/Io/Writer.zig+2-7
...@@ -618,10 +618,6 @@ pub fn writeAllPreserve(w: *Writer, preserve_length: usize, bytes: []const u8) E...@@ -618,10 +618,6 @@ pub fn writeAllPreserve(w: *Writer, preserve_length: usize, bytes: []const u8) E
618/// A user type may be a `struct`, `vector`, `union` or `enum` type.618/// A user type may be a `struct`, `vector`, `union` or `enum` type.
619///619///
620/// To print literal curly braces, escape them by writing them twice, e.g. `{{` or `}}`.620/// To print literal curly braces, escape them by writing them twice, e.g. `{{` or `}}`.
621///
622/// Asserts `buffer` capacity of at least 2 if a union is printed. This
623/// requirement could be lifted by adjusting the code, but if you trigger that
624/// assertion it is a clue that you should probably be using a buffer.
625pub fn print(w: *Writer, comptime fmt: []const u8, args: anytype) Error!void {621pub fn print(w: *Writer, comptime fmt: []const u8, args: anytype) Error!void {
626 const ArgsType = @TypeOf(args);622 const ArgsType = @TypeOf(args);
627 const args_type_info = @typeInfo(ArgsType);623 const args_type_info = @typeInfo(ArgsType);
...@@ -1257,14 +1253,13 @@ pub fn printValue(...@@ -1257,14 +1253,13 @@ pub fn printValue(
1257 .@"extern", .@"packed" => {1253 .@"extern", .@"packed" => {
1258 if (info.fields.len == 0) return w.writeAll(".{}");1254 if (info.fields.len == 0) return w.writeAll(".{}");
1259 try w.writeAll(".{ ");1255 try w.writeAll(".{ ");
1260 inline for (info.fields) |field| {1256 inline for (info.fields, 1..) |field, i| {
1261 try w.writeByte('.');1257 try w.writeByte('.');
1262 try w.writeAll(field.name);1258 try w.writeAll(field.name);
1263 try w.writeAll(" = ");1259 try w.writeAll(" = ");
1264 try w.printValue(ANY, options, @field(value, field.name), max_depth - 1);1260 try w.printValue(ANY, options, @field(value, field.name), max_depth - 1);
1265 (try w.writableArray(2)).* = ", ".*;1261 try w.writeAll(if (i < info.fields.len) ", " else " }");
1266 }1262 }
1267 w.buffer[w.end - 2 ..][0..2].* = " }".*;
1268 },1263 },
1269 }1264 }
1270 },1265 },