Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

streamExactPreserve

"Pump" exactly n bytes from the reader to the writer.

On success, at least preserve_len bytes will remain buffered if there are enough buffered bytes to do so. The amount buffered by the writer after the call will only be less than preserve_len if w.end + n is less than preserve_len before the call. The intentionally preserved bytes will include up to preserve_len -| n bytes from the previously buffered bytes, plus @min(n, preserve_len) of the newly "pumped" bytes.

Asserts Writer.buffer capacity is at least preserve_len. n can be greater than the Writer.buffer capacity.

Reader.streamExactPreserve
pub fn streamExactPreserve(r: *Reader, w: *Writer, preserve_len: usize, n: usize) StreamError!void

File

lib/std/Io/Reader.zig:239

Code

pub fn streamExactPreserve(r: *Reader, w: *Writer, preserve_len: usize, n: usize) StreamError!void {
    if (w.end + n <= w.buffer.len) {
        @branchHint(.likely);
        return streamExact(r, w, n);
    }
    // If `n` is large, we can ignore `preserve_len` up to a point.
    var remaining = n;
    while (remaining > preserve_len) {
        assert(remaining != 0);
        remaining -= try r.stream(w, .limited(remaining - preserve_len));
        if (w.end + remaining <= w.buffer.len) return streamExact(r, w, remaining);
    }
    // Offset the amount preserved by the amount we have left to stream
    // since the remaining bytes are always going to be part of that
    // preservation.
    try w.rebase(preserve_len -| remaining, remaining);
    return streamExact(r, w, remaining);
}