authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-21 00:14:48-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-02-23 02:37:11-07:00
logd943ce5dc79d2438ac82bb8e32baa33d3105306d
tree026977ec1c5d674b0028f5d4ea893d78a05c292c
parent12a9e0f4150f0d30c851727f4dc2354fa6c90682

std.io.Reader: add discard function

Reads the stream until the end, ignoring all the data. Returns the number of bytes discarded.

1 files changed, 12 insertions(+), 0 deletions(-)

lib/std/io/Reader.zig+12
......@@ -360,6 +360,18 @@ pub fn readEnum(self: Self, comptime Enum: type, endian: std.builtin.Endian) any
360360 return E.InvalidValue;
361361}
362362
363/// Reads the stream until the end, ignoring all the data.
364/// Returns the number of bytes discarded.
365pub fn discard(self: Self) anyerror!u64 {
366 var trash: [4096]u8 = undefined;
367 var index: u64 = 0;
368 while (true) {
369 const n = try self.read(&trash);
370 if (n == 0) return index;
371 index += n;
372 }
373}
374
363375const std = @import("../std.zig");
364376const Self = @This();
365377const math = std.math;