mirror of
https://github.com/ziglang/zig.git
synced 2024-11-27 15:42:49 +00:00
std: add io.Writer.writeStruct
We have readStruct, add writeStruct for symmetry
This commit is contained in:
parent
fcd25065ef
commit
34ca6b7b44
@ -4,6 +4,7 @@
|
||||
// The MIT license requires this copyright notice to be included in all copies
|
||||
// and substantial portions of the software.
|
||||
const std = @import("../std.zig");
|
||||
const assert = std.debug.assert;
|
||||
const builtin = std.builtin;
|
||||
const mem = std.mem;
|
||||
|
||||
@ -86,5 +87,11 @@ pub fn Writer(
|
||||
mem.writeInt(T, &bytes, value, endian);
|
||||
return self.writeAll(&bytes);
|
||||
}
|
||||
|
||||
pub fn writeStruct(self: Self, value: anytype) Error!void {
|
||||
// Only extern and packed structs have defined in-memory layout.
|
||||
comptime assert(@typeInfo(@TypeOf(value)).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto);
|
||||
return self.writeAll(mem.asBytes(&value));
|
||||
}
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user