authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-06 18:35:04-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-10 17:51:06-07:00
logb7ba0b69b19a20523af751c0935882ddba579c8c
tree747d64172f5506b8a90b061bc96c567f059c5942
parent574b33e65a6968928d359bc553c4e0c38312a3e9

std.io.Writer: add writeFile method


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

lib/std/io/Writer.zig+11
......@@ -58,3 +58,14 @@ pub fn writeStruct(self: Self, value: anytype) anyerror!void {
5858 comptime assert(@typeInfo(@TypeOf(value)).Struct.layout != .Auto);
5959 return self.writeAll(mem.asBytes(&value));
6060}
61
62pub fn writeFile(self: Self, file: std.fs.File) anyerror!void {
63 // TODO: figure out how to adjust std lib abstractions so that this ends up
64 // doing sendfile or maybe even copy_file_range under the right conditions.
65 var buf: [4000]u8 = undefined;
66 while (true) {
67 const n = try file.readAll(&buf);
68 try self.writeAll(buf[0..n]);
69 if (n < buf.len) return;
70 }
71}