1const std = @import("std");
2
3const nio = @import("./nio.zig");
4const AnyReadable = @This();
5
6vtable: *const struct {
7 read: *const fn (*allowzero anyopaque, []u8) anyerror!usize,
8},
9state: *allowzero anyopaque,
10
11const R = nio.Readable(@This(), ._const);
12pub const readAll = R.readAll;
13pub const readAtLeast = R.readAtLeast;
14pub const readNoEof = R.readNoEof;
15pub const readAllAlloc = R.readAllAlloc;
16pub const readArray = R.readArray;
17pub const readByte = R.readByte;
18pub const readUntilDelimiterArrayList = R.readUntilDelimiterArrayList;
19pub const readUntilDelimiterAlloc = R.readUntilDelimiterAlloc;
20pub const readUntilDelimiterOrEofAlloc = R.readUntilDelimiterOrEofAlloc;
21pub const readUntilDelimitersBuf = R.readUntilDelimitersBuf;
22pub const readUntilDelimitersArrayList = R.readUntilDelimitersArrayList;
23pub const readAlloc = R.readAlloc;
24pub const readInt = R.readInt;
25pub const readUntilDelimitersAlloc = R.readUntilDelimitersAlloc;
26pub const readUntilDelimiter = R.readUntilDelimiter;
27pub const readUntilDelimiterOrEof = R.readUntilDelimiterOrEof;
28pub const readExpected = R.readExpected;
29pub const readType = R.readType;
30pub const skipBytes = R.skipBytes;
31pub const skipUntilDelimiterOrEof = R.skipUntilDelimiterOrEof;
32pub const pipeTo = R.pipeTo;
33
34pub const Error = ReadError; // std compat
35pub const ReadError = anyerror;
36pub fn read(r: AnyReadable, buffer: []u8) !usize {
37 return r.vtable.read(r.state, buffer);
38}
39pub fn anyReadable(r: AnyReadable) AnyReadable {
40 return r;
41}
42
43pub 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}