commit 15a9922
shrub
·
2026-11-23 22:19:25 +0000 UTC
parent d934c7a
make buffer reader and file abstract
2 files changed,
+10,
-5
+3,
-4
1@@ -7,11 +7,11 @@ use <mem.kav> as mem
2
3 extern fn kavstrlen(s: cstr) -> int
4
5-pub linear datatype Buffer {
6+pub abstract linear datatype Buffer {
7 | Buffer(int, int, bytes::Bytes[4096])
8 }
9
10-pub linear datatype Reader {
11+pub abstract linear datatype Reader {
12 /* read, close, the fd, the next unread byte, where the data read ends, the storage */
13 | Reader(fn(int, ptr, int) -> int, fn(int) -> int, int, int, int, bytes::Bytes[4096])
14 }
15@@ -36,8 +36,7 @@ pub fn reader(w: !World, fd: int) -> Reader {
16
17 /% read from a File, which the Reader now owns and closes on release %/
18 pub fn fromfile(f: fs::File) -> Reader {
19- val ~fs::File(fd) = f
20- Reader(rt::read, rt::close, fd, 0, 0, bytes::alloc(cap()))
21+ Reader(rt::read, rt::close, fs::intofd(f), 0, 0, bytes::alloc(cap()))
22 }
23
24 pub fn stdin(w: !World) -> Reader {
+7,
-1
1@@ -2,7 +2,7 @@ use <prelude.kav>
2 use <rt.kav> as rt
3 use <either.kav> as eth
4
5-pub linear datatype File {
6+pub abstract linear datatype File {
7 | File(int)
8 }
9
10@@ -28,6 +28,12 @@ pub fn openappend(w: !World, path: cstr) -> eth::Either[int, File] {
11 open(w, path, 1 + 64 + 1024, 420)
12 }
13
14+/% Release the File, returning it's fd. Now you have to close it. %/
15+pub fn intofd(f: File) -> int {
16+ val ~File(fd) = f
17+ fd
18+}
19+
20 pub fn close(f: File) -> int {
21 val ~File(fd) = f
22 rt::close(fd)