From 0bd9cc0259a15a5b78bfe2889a9866800560067d Mon Sep 17 00:00:00 2001 From: Meghan Denny Date: Fri, 13 Mar 2026 19:47:54 -0700 Subject: [PATCH] add CountingWriter --- counting_writer.zig | 27 +++++++++++++++++++++++++++ nio.zig | 2 ++ 2 files changed, 29 insertions(+) create mode 100644 counting_writer.zig diff --git a/counting_writer.zig b/counting_writer.zig new file mode 100644 index 0000000000000000000000000000000000000000..291ea5cb5bd75e871a3e756392350367cbcee702 --- /dev/null +++ b/counting_writer.zig @@ -0,0 +1,27 @@ +const std = @import("std"); +const extras = @import("extras"); +const nio = @import("./nio.zig"); + +pub fn CountingWriter(WriterType: type) type { + return struct { + backing_writer: WriterType, + bytes_written: u64, + + const Self = @This(); + + pub fn init(backing_writer: WriterType) Self { + return .{ + .backing_writer = backing_writer, + .bytes_written = 0, + }; + } + + pub const WriteError = extras.Pointee(WriterType).WriteError; + pub usingnamespace nio.Writable(@This(), ._var); + pub fn write(self: *Self, bytes: []const u8) WriteError!usize { + const len = try self.backing_writer.write(bytes); + self.bytes_written += len; + return len; + } + }; +} diff --git a/nio.zig b/nio.zig index 4b563b98c3dfc9537a4a0da6a0ae95ac1e09253f..f9c07a4fe028b6d2057086a4031e6f27d0d56738 100644 --- a/nio.zig +++ b/nio.zig @@ -289,3 +289,5 @@ pub const FixedBufferStream = @import("./fixed_buffer_stream.zig").FixedBufferSt pub const BufferedReader = @import("./buffered_reader.zig").BufferedReader; pub const BufferedWriter = @import("./buffered_writer.zig").BufferedWriter; + +pub const CountingWriter = @import("./counting_writer.zig").CountingWriter; -- 2.54.0