zig/lib/std / io/counting_writer.zig

A Writer that counts how many bytes has been written to it.

const std = @import("../std.zig");
const io = std.io;
const testing = std.testing;

CountingWriter()


/// A Writer that counts how many bytes has been written to it.
pub fn CountingWriter(comptime WriterType: type) type {
    return struct {
        bytes_written: u64,
        child_stream: WriterType,

Error


        pub const Error = WriterType.Error;

Writer

        pub const Writer = io.Writer(*Self, Error, write);

write()


        const Self = @This();

writer()


        pub fn write(self: *Self, bytes: []const u8) Error!usize {
            const amt = try self.child_stream.write(bytes);
            self.bytes_written += amt;
            return amt;
        }

countingWriter()


        pub fn writer(self: *Self) Writer {
            return .{ .context = self };
        }
    };
}

Test: CountingWriter


pub fn countingWriter(child_stream: anytype) CountingWriter(@TypeOf(child_stream)) {
    return .{ .bytes_written = 0, .child_stream = child_stream };
}

test CountingWriter {
    var counting_stream = countingWriter(std.io.null_writer);
    const stream = counting_stream.writer();

    const bytes = "yay" ** 100;
    stream.writeAll(bytes) catch unreachable;
    try testing.expect(counting_stream.bytes_written == bytes.len);
}