authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-09 17:30:05-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-07-09 17:30:05-07:00
logab84dcc824220d884177cd7abc2e038d21a8e407
treec3428656af300e652a094743dc657a7a09d72712
parentc072cf2bb8120f247f9af0095b6e5ad8fe1fbcd6

std.io.Reader: make vtable contract more consistent


1 files changed, 8 insertions(+), 7 deletions(-)

lib/std/io/Reader.zig+8-7
......@@ -40,6 +40,11 @@ pub const VTable = struct {
4040 /// also called when it needs to be filled more due to the API user
4141 /// requesting contiguous memory. In either case, the existing buffer data
4242 /// should be ignored; new data written to `w`.
43 ///
44 /// In addition to, or instead of writing to `w`, the implementation may
45 /// choose to store data in `buffer`, modifying `seek` and `end`
46 /// accordingly. Stream implementations are encouraged to take advantage of
47 /// this if simplifies the logic.
4348 stream: *const fn (r: *Reader, w: *Writer, limit: Limit) StreamError!usize,
4449
4550 /// Consumes bytes from the internally tracked stream position without
......@@ -59,6 +64,8 @@ pub const VTable = struct {
5964 /// The default implementation is is based on calling `stream`, borrowing
6065 /// `buffer` to construct a temporary `Writer` and ignoring the written
6166 /// data.
67 ///
68 /// This function is only called when `buffer` is empty.
6269 discard: *const fn (r: *Reader, limit: Limit) Error!usize = defaultDiscard,
6370};
6471
......@@ -162,13 +169,7 @@ pub fn defaultDiscard(r: *Reader, limit: Limit) Error!usize {
162169 error.ReadFailed => return error.ReadFailed,
163170 error.EndOfStream => return error.EndOfStream,
164171 };
165 if (n > @intFromEnum(limit)) {
166 const over_amt = n - @intFromEnum(limit);
167 r.seek = dw.writer.end - over_amt;
168 r.end = dw.writer.end;
169 assert(r.end <= dw.writer.buffer.len); // limit may be exceeded only by an amount within buffer capacity.
170 return @intFromEnum(limit);
171 }
172 assert(n <= @intFromEnum(limit));
172173 return n;
173174}
174175