| ... | @@ -794,12 +794,23 @@ pub fn writeStruct(w: *Writer, value: anytype) Error!void { | ... | @@ -794,12 +794,23 @@ pub fn writeStruct(w: *Writer, value: anytype) Error!void { |
| 794 | /// comptime-known and matches host endianness. | 794 | /// comptime-known and matches host endianness. |
| 795 | /// TODO: make sure this value is not a reference type | 795 | /// TODO: make sure this value is not a reference type |
| 796 | pub inline fn writeStructEndian(w: *Writer, value: anytype, endian: std.builtin.Endian) Error!void { | 796 | pub inline fn writeStructEndian(w: *Writer, value: anytype, endian: std.builtin.Endian) Error!void { |
| 797 | if (native_endian == endian) { | 797 | switch (@typeInfo(@TypeOf(value))) { |
| 798 | return w.writeStruct(value); | 798 | .@"struct" => |info| switch (info.layout) { |
| 799 | } else { | 799 | .auto => @compileError("ill-defined memory layout"), |
| 800 | var copy = value; | 800 | .@"extern" => { |
| 801 | std.mem.byteSwapAllFields(@TypeOf(value), &copy); | 801 | if (native_endian == endian) { |
| 802 | return w.writeStruct(copy); | 802 | return w.writeStruct(value); |
| | 803 | } else { |
| | 804 | var copy = value; |
| | 805 | std.mem.byteSwapAllFields(@TypeOf(value), &copy); |
| | 806 | return w.writeStruct(copy); |
| | 807 | } |
| | 808 | }, |
| | 809 | .@"packed" => { |
| | 810 | return writeInt(w, info.backing_integer.?, @bitCast(value), endian); |
| | 811 | }, |
| | 812 | }, |
| | 813 | else => @compileError("not a struct"), |
| 803 | } | 814 | } |
| 804 | } | 815 | } |
| 805 | | 816 | |