commit 4f5aa8ca2f055c3871f94997de1267491c572811
parent bf44ee8f689a7c04516daf77bb590c20a7f5a14b
Author: Drew DeVault <sir@cmpwn.com>
Date: Mon, 1 Feb 2021 12:59:12 -0500
io: add io::copy
Diffstat:
1 file changed, 23 insertions(+), 0 deletions(-)
diff --git a/io/copy.ha b/io/copy.ha
@@ -0,0 +1,23 @@
+// Copies data from one stream into another. Note that this function will never
+// return if the source stream is infinite.
+export fn copy(dest: *stream, src: *stream) (error | size) = {
+ let w = 0z;
+ let buf: [4096]u8 = [0u8...];
+ for (true) {
+ match (read(src, buf[..])) {
+ err: error => match (err) {
+ closed => break,
+ * => return err,
+ },
+ n: size => {
+ match (write(dest, buf[..n])) {
+ err: error => return err,
+ size => {
+ w += n;
+ },
+ };
+ },
+ };
+ };
+ return w;
+};