| ... | @@ -0,0 +1,73 @@ |
| 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); |
| 3 | const extras = @import("extras"); |
| 4 | const nio = @import("./nio.zig"); |
| 5 | |
| 6 | const sys = switch (builtin.target.os.tag) { |
| 7 | .linux => @import("sys-linux"), |
| 8 | .macos => @import("sys-darwin"), |
| 9 | else => unreachable, |
| 10 | }; |
| 11 | |
| 12 | pub fn LimitedReader(ReaderType: type) type { |
| 13 | return struct { |
| 14 | backing_reader: ReaderType, |
| 15 | bytes_left: u64, |
| 16 | |
| 17 | const Self = @This(); |
| 18 | |
| 19 | pub fn init(backing_reader: ReaderType, bytes_left: u64) Self { |
| 20 | return .{ |
| 21 | .backing_reader = backing_reader, |
| 22 | .bytes_left = bytes_left, |
| 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 max_read = @min(self.bytes_left, bytes.len); |
| 50 | const n = try self.backing_reader.read(bytes[0..max_read]); |
| 51 | self.bytes_left -= n; |
| 52 | return n; |
| 53 | } |
| 54 | // pub fn readv(self: *Self, iovec: []sys.struct_iovec) ReadError!usize { |
| 55 | // const len = try self.backing_reader.readv(iovec); |
| 56 | // self.bytes_read += len; |
| 57 | // return len; |
| 58 | // } |
| 59 | |
| 60 | pub fn anyReadable(self: *Self) nio.AnyReadable { |
| 61 | const S = struct { |
| 62 | fn read(s: *allowzero anyopaque, buffer: []u8) anyerror!usize { |
| 63 | const fbs: *Self = @ptrCast(@alignCast(s)); |
| 64 | return fbs.read(buffer); |
| 65 | } |
| 66 | }; |
| 67 | return .{ |
| 68 | .vtable = &.{ .read = S.read }, |
| 69 | .state = @ptrCast(self), |
| 70 | }; |
| 71 | } |
| 72 | }; |
| 73 | } |