Commit aa1bdb43e0

Andrew Kelley <andrew@ziglang.org>
2025-07-10 04:04:21
std.io.Writer.writeStructEndian: fix packed structs
1 parent dce08d9
Changed files (1)
lib
std
lib/std/io/Writer.zig
@@ -794,12 +794,23 @@ pub fn writeStruct(w: *Writer, value: anytype) Error!void {
 /// comptime-known and matches host endianness.
 /// TODO: make sure this value is not a reference type
 pub inline fn writeStructEndian(w: *Writer, value: anytype, endian: std.builtin.Endian) Error!void {
-    if (native_endian == endian) {
-        return w.writeStruct(value);
-    } else {
-        var copy = value;
-        std.mem.byteSwapAllFields(@TypeOf(value), &copy);
-        return w.writeStruct(copy);
+    switch (@typeInfo(@TypeOf(value))) {
+        .@"struct" => |info| switch (info.layout) {
+            .auto => @compileError("ill-defined memory layout"),
+            .@"extern" => {
+                if (native_endian == endian) {
+                    return w.writeStruct(value);
+                } else {
+                    var copy = value;
+                    std.mem.byteSwapAllFields(@TypeOf(value), &copy);
+                    return w.writeStruct(copy);
+                }
+            },
+            .@"packed" => {
+                return writeInt(w, info.backing_integer.?, @bitCast(value), endian);
+            },
+        },
+        else => @compileError("not a struct"),
     }
 }