zig/lib/std/io/counting_writer.zig
Andrew Kelley 4a69b11e74 add license header to all std lib files
add SPDX license identifier
copyright ownership is zig contributors
2020-08-20 16:07:04 -04:00

52 lines
1.6 KiB
Zig

// SPDX-License-Identifier: MIT
// Copyright (c) 2015-2020 Zig Contributors
// This file is part of [zig](https://ziglang.org/), which is MIT licensed.
// 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 io = std.io;
const testing = std.testing;
/// 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,
pub const Error = WriterType.Error;
pub const Writer = io.Writer(*Self, Error, write);
/// Deprecated: use `Writer`
pub const OutStream = Writer;
const Self = @This();
pub fn write(self: *Self, bytes: []const u8) Error!usize {
const amt = try self.child_stream.write(bytes);
self.bytes_written += amt;
return amt;
}
pub fn writer(self: *Self) Writer {
return .{ .context = self };
}
/// Deprecated: use `writer`
pub fn outStream(self: *Self) OutStream {
return .{ .context = self };
}
};
}
pub fn countingWriter(child_stream: anytype) CountingWriter(@TypeOf(child_stream)) {
return .{ .bytes_written = 0, .child_stream = child_stream };
}
test "io.CountingWriter" {
var counting_stream = countingWriter(std.io.null_writer);
const stream = counting_stream.writer();
const bytes = "yay" ** 100;
stream.writeAll(bytes) catch unreachable;
testing.expect(counting_stream.bytes_written == bytes.len);
}