mirror of
https://codeberg.org/ziglang/zig.git
synced 2025-12-07 22:34:28 +00:00
std.io.Writer.writeStructEndian: fix packed structs
This commit is contained in:
parent
dce08d9a57
commit
aa1bdb43e0
1 changed files with 17 additions and 6 deletions
|
|
@ -794,12 +794,23 @@ pub fn writeStruct(w: *Writer, value: anytype) Error!void {
|
||||||
/// comptime-known and matches host endianness.
|
/// comptime-known and matches host endianness.
|
||||||
/// TODO: make sure this value is not a reference type
|
/// TODO: make sure this value is not a reference type
|
||||||
pub inline fn writeStructEndian(w: *Writer, value: anytype, endian: std.builtin.Endian) Error!void {
|
pub inline fn writeStructEndian(w: *Writer, value: anytype, endian: std.builtin.Endian) Error!void {
|
||||||
if (native_endian == endian) {
|
switch (@typeInfo(@TypeOf(value))) {
|
||||||
return w.writeStruct(value);
|
.@"struct" => |info| switch (info.layout) {
|
||||||
} else {
|
.auto => @compileError("ill-defined memory layout"),
|
||||||
var copy = value;
|
.@"extern" => {
|
||||||
std.mem.byteSwapAllFields(@TypeOf(value), ©);
|
if (native_endian == endian) {
|
||||||
return w.writeStruct(copy);
|
return w.writeStruct(value);
|
||||||
|
} else {
|
||||||
|
var copy = value;
|
||||||
|
std.mem.byteSwapAllFields(@TypeOf(value), ©);
|
||||||
|
return w.writeStruct(copy);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
.@"packed" => {
|
||||||
|
return writeInt(w, info.backing_integer.?, @bitCast(value), endian);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
else => @compileError("not a struct"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue