diff options
| author | daurnimator <quae@daurnimator.com> | 2021-03-01 21:47:11 +1100 |
|---|---|---|
| committer | Jakub Konka <kubkon@jakubkonka.com> | 2021-03-03 22:45:45 +0100 |
| commit | 34ca6b7b449f065fd0f65f8cfad82140cafe4c7d (patch) | |
| tree | 3a0f2be1f794c083badddf4b4b63a75444d2296a /lib/std | |
| parent | fcd25065ef41ac93e10031ebea0ed2bc0effce17 (diff) | |
| download | zig-34ca6b7b449f065fd0f65f8cfad82140cafe4c7d.tar.gz zig-34ca6b7b449f065fd0f65f8cfad82140cafe4c7d.zip | |
std: add io.Writer.writeStruct
We have readStruct, add writeStruct for symmetry
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/io/writer.zig | 7 |
1 files changed, 7 insertions, 0 deletions
diff --git a/lib/std/io/writer.zig b/lib/std/io/writer.zig index 0a9edb425a..6f9386b8de 100644 --- a/lib/std/io/writer.zig +++ b/lib/std/io/writer.zig @@ -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)); + } }; } |
