struct Reader [src]
A std.Io.Reader that writes a predetermined list of buffers during stream.
Fields
calls: []const Call
interface: std.Io.Reader
next_call_index: usize
next_offset: usize
artificial_limit: std.Io.Limit = .unlimitedFurther reduces how many bytes are written in each stream call.
Members
Source
pub const Reader = struct {
calls: []const Call,
interface: std.Io.Reader,
next_call_index: usize,
next_offset: usize,
/// Further reduces how many bytes are written in each `stream` call.
artificial_limit: std.Io.Limit = .unlimited,
pub const Call = struct {
buffer: []const u8,
};
pub fn init(buffer: []u8, calls: []const Call) Reader {
return .{
.next_call_index = 0,
.next_offset = 0,
.interface = .{
.vtable = &.{ .stream = stream },
.buffer = buffer,
.seek = 0,
.end = 0,
},
.calls = calls,
};
}
fn stream(io_r: *std.Io.Reader, w: *std.Io.Writer, limit: std.Io.Limit) std.Io.Reader.StreamError!usize {
const r: *Reader = @alignCast(@fieldParentPtr("interface", io_r));
if (r.calls.len - r.next_call_index == 0) return error.EndOfStream;
const call = r.calls[r.next_call_index];
const buffer = r.artificial_limit.sliceConst(limit.sliceConst(call.buffer[r.next_offset..]));
const n = try w.write(buffer);
r.next_offset += n;
if (call.buffer.len - r.next_offset == 0) {
r.next_call_index += 1;
r.next_offset = 0;
}
return n;
}
}