| ... | @@ -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 user | 40 | /// also called when it needs to be filled more due to the API user |
| 41 | /// requesting contiguous memory. In either case, the existing buffer data | 41 | /// 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, |
| 44 | | 49 | |
| 45 | /// Consumes bytes from the internally tracked stream position without | 50 | /// 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`, borrowing | 64 | /// The default implementation is is based on calling `stream`, borrowing |
| 60 | /// `buffer` to construct a temporary `Writer` and ignoring the written | 65 | /// `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 | }; |
| 64 | | 71 | |
| ... | @@ -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 | } |
| 174 | | 175 | |