mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
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.
44 lines
1.2 KiB
Zig
44 lines
1.2 KiB
Zig
const std = @import("../std.zig");
|
|
const io = std.io;
|
|
const testing = std.testing;
|
|
|
|
/// A Reader that counts how many bytes has been read from it.
|
|
pub fn CountingReader(comptime ReaderType: anytype) type {
|
|
return struct {
|
|
child_reader: ReaderType,
|
|
bytes_read: u64 = 0,
|
|
|
|
pub const Error = ReaderType.Error;
|
|
pub const Reader = io.Reader(*@This(), Error, read);
|
|
|
|
pub fn read(self: *@This(), buf: []u8) Error!usize {
|
|
const amt = try self.child_reader.read(buf);
|
|
self.bytes_read += amt;
|
|
return amt;
|
|
}
|
|
|
|
pub fn reader(self: *@This()) Reader {
|
|
return .{ .context = self };
|
|
}
|
|
};
|
|
}
|
|
|
|
pub fn countingReader(reader: anytype) CountingReader(@TypeOf(reader)) {
|
|
return .{ .child_reader = reader };
|
|
}
|
|
|
|
test "io.CountingReader" {
|
|
const bytes = "yay" ** 100;
|
|
var fbs = io.fixedBufferStream(bytes);
|
|
|
|
var counting_stream = countingReader(fbs.reader());
|
|
const stream = counting_stream.reader();
|
|
|
|
//read and discard all bytes
|
|
while (stream.readByte()) |_| {} else |err| {
|
|
try testing.expect(err == error.EndOfStream);
|
|
}
|
|
|
|
try testing.expect(counting_stream.bytes_read == bytes.len);
|
|
}
|