From 4896f0e2a57c715d9db27c2500d138899674a972 Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 2 Jan 2026 19:10:41 -0800 Subject: [PATCH] add Writable --- nio.zig | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/nio.zig b/nio.zig index 6c074d2959625a3cda1aaeb285ad90dbd159e900..b57ea344f387a2a5507374e7486cd98a8b5a7d81 100644 --- a/nio.zig +++ b/nio.zig @@ -89,6 +89,51 @@ pub fn Readable(T: type, this_kind: enum { _var, _const, _bare }) type { }; } +pub fn Writable(T: type, this_kind: enum { _var, _const, _bare }) type { + return struct { + const Error = T.WriteError; + + const Self = switch (this_kind) { + ._var => *T, + ._const => *const T, + ._bare => T, + }; + + // pub fn write(self: Self, bytes: []const u8) Error!usize { + // } + + pub fn writeAll(self: Self, bytes: []const u8) Error!void { + var index: usize = 0; + while (index != bytes.len) { + index += try self.write(bytes[index..]); + } + } + + pub fn writeByteNTimes(self: Self, byte: u8, n: usize) Error!void { + var bytes: [1024]u8 = undefined; + @memset(bytes[0..], byte); + var remaining: usize = n; + while (remaining > 0) { + const to_write = @min(remaining, bytes.len); + try self.writeAll(bytes[0..to_write]); + remaining -= to_write; + } + } + + pub fn writeInt(self: Self, comptime I: type, value: I, endian: std.builtin.Endian) Error!void { + var bytes: [@as(u16, @intCast((@as(u17, @typeInfo(I).Int.bits) + 7) / 8))]u8 = undefined; + std.mem.writeInt(std.math.ByteAlignedInt(I), &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 std.debug.assert(@typeInfo(@TypeOf(value)).Struct.layout != .Auto); + return self.writeAll(std.mem.asBytes(&value)); + } + }; +} + pub const AnyReadable = @import("./AnyReadable.zig"); pub const FixedBufferStream = @import("./fixed_buffer_stream.zig").FixedBufferStream; -- 2.54.0