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 {...@@ -40,6 +40,11 @@ pub const VTable = struct {
40 /// also called when it needs to be filled more due to the API user40 /// also called when it needs to be filled more due to the API user
41 /// requesting contiguous memory. In either case, the existing buffer data41 /// requesting contiguous memory. In either case, the existing buffer data
42 /// should be ignored; new data written to `w`.42 /// 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.
43 stream: *const fn (r: *Reader, w: *Writer, limit: Limit) StreamError!usize,48 stream: *const fn (r: *Reader, w: *Writer, limit: Limit) StreamError!usize,
4449
45 /// Consumes bytes from the internally tracked stream position without50 /// Consumes bytes from the internally tracked stream position without
...@@ -59,6 +64,8 @@ pub const VTable = struct {...@@ -59,6 +64,8 @@ pub const VTable = struct {
59 /// The default implementation is is based on calling `stream`, borrowing64 /// The default implementation is is based on calling `stream`, borrowing
60 /// `buffer` to construct a temporary `Writer` and ignoring the written65 /// `buffer` to construct a temporary `Writer` and ignoring the written
61 /// data.66 /// data.
67 ///
68 /// This function is only called when `buffer` is empty.
62 discard: *const fn (r: *Reader, limit: Limit) Error!usize = defaultDiscard,69 discard: *const fn (r: *Reader, limit: Limit) Error!usize = defaultDiscard,
63};70};
6471
...@@ -162,13 +169,7 @@ pub fn defaultDiscard(r: *Reader, limit: Limit) Error!usize {...@@ -162,13 +169,7 @@ pub fn defaultDiscard(r: *Reader, limit: Limit) Error!usize {
162 error.ReadFailed => return error.ReadFailed,169 error.ReadFailed => return error.ReadFailed,
163 error.EndOfStream => return error.EndOfStream,170 error.EndOfStream => return error.EndOfStream,
164 };171 };
165 if (n > @intFromEnum(limit)) {172 assert(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 return n;173 return n;
173}174}
174175