summaryrefslogtreecommitdiff
path: root/lib/bio
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-06-12 09:51:51 -0700
committerOri Bernstein <ori@eigenstate.org>2017-06-12 09:51:51 -0700
commit99e1677b6bf577fb4db18f50a92df79ea47e9e0e (patch)
tree3f0c880ada7c545f9e1ab09e94f1b526777f4cf6 /lib/bio
parentc505373d1bbcad1e7861f6db054eddcfd5cac354 (diff)
downloadmc-99e1677b6bf577fb4db18f50a92df79ea47e9e0e.tar.gz
Add missing file.
Diffstat (limited to 'lib/bio')
-rw-r--r--lib/bio/mem.myr45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/bio/mem.myr b/lib/bio/mem.myr
new file mode 100644
index 0000000..dcafc82
--- /dev/null
+++ b/lib/bio/mem.myr
@@ -0,0 +1,45 @@
+use std
+use "bio"
+
+pkg bio =
+ const mkmem : (buf : byte[:] -> file#)
+;;
+
+type mem = struct
+ buf : byte[:]
+ off : std.size
+;;
+
+const mkmem = {buf
+ var mem : mem#
+
+ mem = std.mk([
+ .buf = buf,
+ .off = 0
+ ])
+
+ -> mk(Rd, [
+ .read = {buf; -> memread(mem, buf)},
+ .write = {buf; -> memwrite(mem, buf)},
+ .seek = {off; mem.off = std.clamp(off, 0, mem.buf.len); -> `std.Ok mem.off},
+ .close = {; std.free(mem)},
+ ])
+}
+
+const memread = {mem, dst
+ var n
+
+ n = std.min(mem.buf.len - mem.off, dst.len)
+ std.slcp(dst[:n], mem.buf[mem.off:mem.off+n])
+ mem.off += n
+ -> `std.Ok n
+}
+
+const memwrite = {mem, src
+ var n
+
+ n = std.min(mem.buf.len - mem.off, src.len)
+ std.slcp(mem.buf[mem.off:mem.off+n], src[:n])
+ mem.off += n
+ -> `std.Ok n
+}