authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-31 14:45:16 -07:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2026-05-31 14:45:16 -07:00
log6fa4f951dc0c384f9ae10e2d698638b966de9f14
tree585b840541df07179026c354d4e43229a13efcb7
parent74f584a9ee2232470ef015b4863f4bc12f5fa406
signaturebadge-check Signed by SSH key SHA256:4hHJbtBRU58AYXwjL7fkz2fnQHdiye8x1QpTCQ0sHNw

add CountingReader


2 files changed, 74 insertions(+), 0 deletions(-)

counting_reader.zig created+72
...@@ -0,0 +1,72 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const extras = @import("extras");
4const nio = @import("./nio.zig");
5
6const sys = switch (builtin.target.os.tag) {
7 .linux => @import("sys-linux"),
8 .macos => @import("sys-darwin"),
9 else => unreachable,
10};
11
12pub fn CountingReader(ReaderType: type) type {
13 return struct {
14 backing_reader: ReaderType,
15 bytes_read: u64,
16
17 const Self = @This();
18
19 pub fn init(backing_reader: ReaderType) Self {
20 return .{
21 .backing_reader = backing_reader,
22 .bytes_read = 0,
23 };
24 }
25
26 const R = nio.Readable(@This(), ._var);
27 pub const readAll = R.readAll;
28 pub const readAtLeast = R.readAtLeast;
29 pub const readNoEof = R.readNoEof;
30 pub const readAllAlloc = R.readAllAlloc;
31 pub const readArray = R.readArray;
32 pub const readByte = R.readByte;
33 pub const readUntilDelimiterArrayList = R.readUntilDelimiterArrayList;
34 pub const readUntilDelimiterAlloc = R.readUntilDelimiterAlloc;
35 pub const readUntilDelimiterOrEofAlloc = R.readUntilDelimiterOrEofAlloc;
36 pub const readUntilDelimitersBuf = R.readUntilDelimitersBuf;
37 pub const readUntilDelimitersArrayList = R.readUntilDelimitersArrayList;
38 pub const readAlloc = R.readAlloc;
39 pub const readInt = R.readInt;
40 pub const readUntilDelimitersAlloc = R.readUntilDelimitersAlloc;
41 pub const readUntilDelimiterOrEof = R.readUntilDelimiterOrEof;
42 pub const readExpected = R.readExpected;
43 pub const skipBytes = R.skipBytes;
44 pub const skipUntilDelimiterOrEof = R.skipUntilDelimiterOrEof;
45
46 pub const ReadError = extras.Pointee(ReaderType).ReadError;
47
48 pub fn read(self: *Self, bytes: []u8) ReadError!usize {
49 const len = try self.backing_reader.read(bytes);
50 self.bytes_read += len;
51 return len;
52 }
53 // pub fn readv(self: *Self, iovec: []sys.struct_iovec) ReadError!usize {
54 // const len = try self.backing_reader.readv(iovec);
55 // self.bytes_read += len;
56 // return len;
57 // }
58
59 pub fn anyReadable(self: *Self) nio.AnyReadable {
60 const S = struct {
61 fn read(s: *allowzero anyopaque, buffer: []u8) anyerror!usize {
62 const fbs: *Self = @ptrCast(@alignCast(s));
63 return fbs.read(buffer);
64 }
65 };
66 return .{
67 .vtable = &.{ .read = S.read },
68 .state = @ptrCast(self),
69 };
70 }
71 };
72}
nio.zig+2
...@@ -393,3 +393,5 @@ pub const CountingWriter = @import("./counting_writer.zig").CountingWriter;...@@ -393,3 +393,5 @@ pub const CountingWriter = @import("./counting_writer.zig").CountingWriter;
393pub const NullWriter = @import("./null_writer.zig").NullWriter;393pub const NullWriter = @import("./null_writer.zig").NullWriter;
394394
395pub const AllocatingWriter = @import("./allocating_writer.zig").AllocatingWriter;395pub const AllocatingWriter = @import("./allocating_writer.zig").AllocatingWriter;
396
397pub const CountingReader = @import("./counting_reader.zig").CountingReader;