zig/lib/std/io/Reader.zig

132 lines
4.4 KiB
Zig

const std = @import("../std.zig");
const Reader = @This();
const assert = std.debug.assert;
context: ?*anyopaque,
vtable: *const VTable,
pub const VTable = struct {
/// Writes bytes from the internally tracked stream position to `bw`.
///
/// Returns the number of bytes written, which will be at minimum `0` and at
/// most `limit`. The number of bytes read, including zero, does not
/// indicate end of stream.
///
/// If the reader has an internal seek position, it moves forward in
/// accordance with the number of bytes return from this function.
///
/// The implementation should do a maximum of one underlying read call.
///
/// If `error.Unstreamable` is returned, the resource cannot be used via a
/// streaming reading interface.
read: *const fn (ctx: ?*anyopaque, bw: *std.io.BufferedWriter, limit: Limit) anyerror!Status,
/// Writes bytes from the internally tracked stream position to `data`.
///
/// Returns the number of bytes written, which will be at minimum `0` and at
/// most `limit`. The number of bytes read, including zero, does not
/// indicate end of stream.
///
/// If the reader has an internal seek position, it moves forward in
/// accordance with the number of bytes return from this function.
///
/// The implementation should do a maximum of one underlying read call.
///
/// If `error.Unstreamable` is returned, the resource cannot be used via a
/// streaming reading interface.
readv: *const fn (ctx: ?*anyopaque, data: []const []u8) anyerror!Status,
};
pub const Len = @Type(.{ .int = .{ .signedness = .unsigned, .bits = @bitSizeOf(usize) - 1 } });
pub const Status = packed struct(usize) {
/// Number of bytes that were transferred. Zero does not mean end of
/// stream.
len: Len = 0,
/// Indicates end of stream.
end: bool = false,
};
pub const Limit = enum(usize) {
none = std.math.maxInt(usize),
_,
pub fn min(l: Limit, int: usize) usize {
return @min(int, @intFromEnum(l));
}
};
pub fn read(r: Reader, w: *std.io.BufferedWriter, limit: Limit) anyerror!Status {
return r.vtable.read(r.context, w, limit);
}
pub fn readv(r: Reader, data: []const []u8) anyerror!Status {
return r.vtable.readv(r.context, data);
}
/// Returns total number of bytes written to `w`.
pub fn readAll(r: Reader, w: *std.io.BufferedWriter) anyerror!usize {
const readFn = r.vtable.read;
var offset: usize = 0;
while (true) {
const status = try readFn(r.context, w, .none);
offset += status.len;
if (status.end) return offset;
}
}
/// Allocates enough memory to hold all the contents of the stream. If the allocated
/// memory would be greater than `max_size`, returns `error.StreamTooLong`.
///
/// Caller owns returned memory.
///
/// If this function returns an error, the contents from the stream read so far are lost.
pub fn readAlloc(r: Reader, gpa: std.mem.Allocator, max_size: usize) anyerror![]u8 {
const readFn = r.vtable.read;
var aw: std.io.AllocatingWriter = undefined;
errdefer aw.deinit();
const bw = aw.init(gpa);
var remaining = max_size;
while (remaining > 0) {
const status = try readFn(r.context, bw, .init(remaining));
if (status.end) break;
remaining -= status.len;
}
return aw.toOwnedSlice(gpa);
}
/// Reads the stream until the end, ignoring all the data.
/// Returns the number of bytes discarded.
pub fn discardUntilEnd(r: Reader) anyerror!usize {
var bw = std.io.null_writer.unbuffered();
return readAll(r, &bw);
}
test "readAlloc when the backing reader provides one byte at a time" {
const OneByteReader = struct {
str: []const u8,
curr: usize,
fn read(self: *@This(), dest: []u8) anyerror!usize {
if (self.str.len <= self.curr or dest.len == 0)
return 0;
dest[0] = self.str[self.curr];
self.curr += 1;
return 1;
}
fn reader(self: *@This()) std.io.Reader {
return .{
.context = self,
};
}
};
const str = "This is a test";
var one_byte_stream: OneByteReader = .init(str);
const res = try one_byte_stream.reader().streamReadAlloc(std.testing.allocator, str.len + 1);
defer std.testing.allocator.free(res);
try std.testing.expectEqualStrings(str, res);
}