authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2021-03-01 21:47:11+11:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-03-03 22:45:45+01:00
log34ca6b7b449f065fd0f65f8cfad82140cafe4c7d
tree3a0f2be1f794c083badddf4b4b63a75444d2296a
parentfcd25065ef41ac93e10031ebea0ed2bc0effce17

std: add io.Writer.writeStruct

We have readStruct, add writeStruct for symmetry

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

lib/std/io/writer.zig+7
...@@ -4,6 +4,7 @@...@@ -4,6 +4,7 @@
4// The MIT license requires this copyright notice to be included in all copies4// The MIT license requires this copyright notice to be included in all copies
5// and substantial portions of the software.5// and substantial portions of the software.
6const std = @import("../std.zig");6const std = @import("../std.zig");
7const assert = std.debug.assert;
7const builtin = std.builtin;8const builtin = std.builtin;
8const mem = std.mem;9const mem = std.mem;
910
...@@ -86,5 +87,11 @@ pub fn Writer(...@@ -86,5 +87,11 @@ pub fn Writer(
86 mem.writeInt(T, &bytes, value, endian);87 mem.writeInt(T, &bytes, value, endian);
87 return self.writeAll(&bytes);88 return self.writeAll(&bytes);
88 }89 }
90
91 pub fn writeStruct(self: Self, value: anytype) Error!void {
92 // Only extern and packed structs have defined in-memory layout.
93 comptime assert(@typeInfo(@TypeOf(value)).Struct.layout != builtin.TypeInfo.ContainerLayout.Auto);
94 return self.writeAll(mem.asBytes(&value));
95 }
89 };96 };
90}97}