Zig 0.17.0-dev (Split by item)

Shortcut keys
?
Show these tips
S
Focus search field
Esc
Clear focus & close tips
U
Jump to source code
↑
Move up in search results
↓
Move down in search results
↵
Go to active search result
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 standard library. Public Function

streamExactPreserve

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

"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.

Params

Errors

ReadFailed

See the Reader implementation for detailed diagnostics.

WriteFailed

See the Writer implementation for detailed diagnostics.

EndOfStream

End of stream indicated from the Reader. This error cannot originate from the Writer.

File

Io/Reader.zig:239
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
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);
}