| 1 | const std = @import("std"); |
| 2 | |
| 3 | const nio = @import("./nio.zig"); |
| 4 | const AnyReadable = @This(); |
| 5 | |
| 6 | vtable: *const struct { |
| 7 | read: *const fn (*allowzero anyopaque, []u8) anyerror!usize, |
| 8 | }, |
| 9 | state: *allowzero anyopaque, |
| 10 | |
| 11 | const R = nio.Readable(@This(), ._const); |
| 12 | pub const readAll = R.readAll; |
| 13 | pub const readAtLeast = R.readAtLeast; |
| 14 | pub const readNoEof = R.readNoEof; |
| 15 | pub const readAllAlloc = R.readAllAlloc; |
| 16 | pub const readArray = R.readArray; |
| 17 | pub const readByte = R.readByte; |
| 18 | pub const readUntilDelimiterArrayList = R.readUntilDelimiterArrayList; |
| 19 | pub const readUntilDelimiterAlloc = R.readUntilDelimiterAlloc; |
| 20 | pub const readUntilDelimiterOrEofAlloc = R.readUntilDelimiterOrEofAlloc; |
| 21 | pub const readUntilDelimitersBuf = R.readUntilDelimitersBuf; |
| 22 | pub const readUntilDelimitersArrayList = R.readUntilDelimitersArrayList; |
| 23 | pub const readAlloc = R.readAlloc; |
| 24 | pub const readInt = R.readInt; |
| 25 | pub const readUntilDelimitersAlloc = R.readUntilDelimitersAlloc; |
| 26 | pub const readUntilDelimiter = R.readUntilDelimiter; |
| 27 | pub const readUntilDelimiterOrEof = R.readUntilDelimiterOrEof; |
| 28 | pub const readExpected = R.readExpected; |
| 29 | pub const readType = R.readType; |
| 30 | pub const skipBytes = R.skipBytes; |
| 31 | pub const skipUntilDelimiterOrEof = R.skipUntilDelimiterOrEof; |
| 32 | pub const pipeTo = R.pipeTo; |
| 33 | |
| 34 | pub const Error = ReadError; // std compat |
| 35 | pub const ReadError = anyerror; |
| 36 | pub fn read(r: AnyReadable, buffer: []u8) !usize { |
| 37 | return r.vtable.read(r.state, buffer); |
| 38 | } |
| 39 | pub fn anyReadable(r: AnyReadable) AnyReadable { |
| 40 | return r; |
| 41 | } |
| 42 | |
| 43 | pub fn fromStd(reader_ptr: *std.Io.Reader) AnyReadable { |
| 44 | const S = struct { |
| 45 | fn _read(s: *allowzero anyopaque, buffer: []u8) !usize { |
| 46 | const r: *std.Io.Reader = @ptrCast(@alignCast(s)); |
| 47 | var w: std.Io.Writer = .fixed(buffer); |
| 48 | return r.stream(&w, .limited(buffer.len)) catch |err| switch (err) { |
| 49 | error.ReadFailed => |e| e, |
| 50 | error.WriteFailed => unreachable, |
| 51 | error.EndOfStream => 0, |
| 52 | }; |
| 53 | } |
| 54 | }; |
| 55 | return .{ |
| 56 | .vtable = &.{ .read = &S._read }, |
| 57 | .state = @ptrCast(@constCast(reader_ptr)), |
| 58 | }; |
| 59 | } |