mirror of
https://github.com/ziglang/zig.git
synced 2024-11-16 17:15:37 +00:00
d29871977f
We already have a LICENSE file that covers the Zig Standard Library. We no longer need to remind everyone that the license is MIT in every single file. Previously this was introduced to clarify the situation for a fork of Zig that made Zig's LICENSE file harder to find, and replaced it with their own license that required annual payments to their company. However that fork now appears to be dead. So there is no need to reinforce the copyright notice in every single file.
40 lines
1.1 KiB
Zig
40 lines
1.1 KiB
Zig
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);
|
|
|
|
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 };
|
|
}
|
|
};
|
|
}
|
|
|
|
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;
|
|
try testing.expect(counting_stream.bytes_written == bytes.len);
|
|
}
|