Debian Codegraph @

io/drain.ha

raw
1 // SPDX-License-Identifier: MPL-2.0
2 // (c) Hare authors <https://harelang.org>
3
4 // Reads an entire stream into a []u8. The caller must free the return value.
5 // Note that this function will never return if the handle is infinite.
6 export fn drain(in: handle) ([]u8 | error) = {
7 let sink: []u8 = [];
8 let ok = false;
9 defer if (!ok) free(sink);
10
11 static let buf: [4096]u8 = [0...];
12
13 for (let n => read(in, buf[..])?) {
14 append(sink, buf[..n]...)?;
15 };
16
17 ok = true;
18 return sink;
19 };