commit 8e7099d14bc8a5a8f5c3c0c7d975433ae0a336db
parent f73e7fbcedb0182b61a96341828839acc751d39f
Author: Drew DeVault <sir@cmpwn.com>
Date: Sat, 4 Sep 2021 08:47:44 +0200
io::tee: use stack allocation
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
2 files changed, 14 insertions(+), 19 deletions(-)
diff --git a/hare/module/scan.ha b/hare/module/scan.ha
@@ -317,9 +317,8 @@ fn scan_file(
hash::write(&sha, strings::toutf8(path));
let tee = io::tee(f, &sha);
- defer io::close(tee);
- let lexer = lex::init(tee, path);
+ let lexer = lex::init(&tee, path);
let imports = parse::imports(&lexer)?;
for (let i = 0z; i < len(imports); i += 1) {
let ident = match (imports[i]) {
@@ -332,7 +331,7 @@ fn scan_file(
};
};
- io::copy(io::empty, tee)?; // Finish spooling out the file for the SHA
+ io::copy(io::empty, &tee)?; // Finish spooling out the file for the SHA
let tmp: [sha256::SIZE]u8 = [0...];
hash::sum(&sha, tmp);
diff --git a/io/tee.ha b/io/tee.ha
@@ -1,26 +1,24 @@
-type tee_stream = struct {
- stream: stream,
+export type teestream = struct {
+ stream,
source: *stream,
sink: *stream,
};
// Creates a reader which copies reads into a sink before forwarding them to the
-// caller. Does not close the secondary streams when the tee stream is closed.
-export fn tee(source: *stream, sink: *stream) *stream = {
- return alloc(tee_stream {
- stream = stream {
- reader = &tee_read,
- closer = &tee_close,
- unwrap = &tee_unwrap,
- ...
- },
+// caller. This stream does not need to be closed, and closing it will not close
+// the secondary streams.
+export fn tee(source: *stream, sink: *stream) teestream = {
+ return teestream {
+ reader = &tee_read,
+ unwrap = &tee_unwrap,
source = source,
sink = sink,
- }): *io::stream;
+ ...
+ };
};
fn tee_read(s: *stream, buf: []u8) (size | EOF | error) = {
- let s = s: *tee_stream;
+ let s = s: *teestream;
let z = match (read(s.source, buf)) {
err: error => return err,
EOF => return EOF,
@@ -32,10 +30,8 @@ fn tee_read(s: *stream, buf: []u8) (size | EOF | error) = {
return z;
};
-fn tee_close(s: *stream) void = free(s);
-
fn tee_unwrap(s: *stream) *io::stream = {
assert(s.unwrap == &tee_unwrap);
- let s = s: *tee_stream;
+ let s = s: *teestream;
return s.source;
};