commit b1a13927adf5ce288421079ac9f9f031769117c3
parent 978d125c6a085a60ebcd1513b445131890ca534e
Author: Drew DeVault <sir@cmpwn.com>
Date: Sun, 1 May 2022 14:25:04 +0200
io: add io::zero
A synthetic stream which always reads zeroes, equivalent to /dev/zero on
Linux.
Signed-off-by: Drew DeVault <sir@cmpwn.com>
Diffstat:
3 files changed, 34 insertions(+), 8 deletions(-)
diff --git a/io/zero.ha b/io/zero.ha
@@ -0,0 +1,20 @@
+// License: MPL-2.0
+// (c) 2022 Bor Grošelj Simić <bor.groseljsimic@telemach.net>
+// (c) 2021-2022 Drew DeVault <sir@cmpwn.com>
+use bytes;
+
+const _zero_vt: vtable = vtable {
+ reader = &zero_read,
+ writer = &empty_write,
+ ...
+};
+
+const _zero: stream = &_zero_vt;
+
+// A [[stream]] which always reads zereos and discards any writes.
+export const zero: *stream = &_zero;
+
+fn zero_read(s: *stream, buf: []u8) (size | EOF | error) = {
+ bytes::zero(buf);
+ return len(buf);
+};
diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib
@@ -768,6 +768,7 @@ gensrcs_io() {
tee.ha \
types.ha \
util.ha \
+ zero.ha \
$*
gen_srcs -pfreebsd io \
'arch+$(ARCH).ha' \
@@ -783,6 +784,7 @@ gensrcs_io() {
tee.ha \
types.ha \
util.ha \
+ zero.ha \
$*
}
@@ -795,8 +797,8 @@ io() {
+test/limit.ha \
+test/stream.ha
fi
- gen_ssa -plinux io strings errors
- gen_ssa -pfreebsd io strings errors
+ gen_ssa -plinux io strings errors bytes
+ gen_ssa -pfreebsd io strings errors bytes
}
linux() {
diff --git a/stdlib.mk b/stdlib.mk
@@ -1322,7 +1322,8 @@ stdlib_io_linux_srcs = \
$(STDLIB)/io/stream.ha \
$(STDLIB)/io/tee.ha \
$(STDLIB)/io/types.ha \
- $(STDLIB)/io/util.ha
+ $(STDLIB)/io/util.ha \
+ $(STDLIB)/io/zero.ha
# io (+freebsd)
stdlib_io_freebsd_srcs = \
@@ -1338,15 +1339,16 @@ stdlib_io_freebsd_srcs = \
$(STDLIB)/io/stream.ha \
$(STDLIB)/io/tee.ha \
$(STDLIB)/io/types.ha \
- $(STDLIB)/io/util.ha
+ $(STDLIB)/io/util.ha \
+ $(STDLIB)/io/zero.ha
-$(HARECACHE)/io/io-linux.ssa: $(stdlib_io_linux_srcs) $(stdlib_rt) $(stdlib_strings_$(PLATFORM)) $(stdlib_errors_$(PLATFORM))
+$(HARECACHE)/io/io-linux.ssa: $(stdlib_io_linux_srcs) $(stdlib_rt) $(stdlib_strings_$(PLATFORM)) $(stdlib_errors_$(PLATFORM)) $(stdlib_bytes_$(PLATFORM))
@printf 'HAREC \t$@\n'
@mkdir -p $(HARECACHE)/io
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nio \
-t$(HARECACHE)/io/io.td $(stdlib_io_linux_srcs)
-$(HARECACHE)/io/io-freebsd.ssa: $(stdlib_io_freebsd_srcs) $(stdlib_rt) $(stdlib_strings_$(PLATFORM)) $(stdlib_errors_$(PLATFORM))
+$(HARECACHE)/io/io-freebsd.ssa: $(stdlib_io_freebsd_srcs) $(stdlib_rt) $(stdlib_strings_$(PLATFORM)) $(stdlib_errors_$(PLATFORM)) $(stdlib_bytes_$(PLATFORM))
@printf 'HAREC \t$@\n'
@mkdir -p $(HARECACHE)/io
@HARECACHE=$(HARECACHE) $(HAREC) $(HAREFLAGS) -o $@ -Nio \
@@ -3382,6 +3384,7 @@ testlib_io_linux_srcs = \
$(STDLIB)/io/tee.ha \
$(STDLIB)/io/types.ha \
$(STDLIB)/io/util.ha \
+ $(STDLIB)/io/zero.ha \
$(STDLIB)/io/+test/limit.ha \
$(STDLIB)/io/+test/stream.ha
@@ -3400,16 +3403,17 @@ testlib_io_freebsd_srcs = \
$(STDLIB)/io/tee.ha \
$(STDLIB)/io/types.ha \
$(STDLIB)/io/util.ha \
+ $(STDLIB)/io/zero.ha \
$(STDLIB)/io/+test/limit.ha \
$(STDLIB)/io/+test/stream.ha
-$(TESTCACHE)/io/io-linux.ssa: $(testlib_io_linux_srcs) $(testlib_rt) $(testlib_strings_$(PLATFORM)) $(testlib_errors_$(PLATFORM))
+$(TESTCACHE)/io/io-linux.ssa: $(testlib_io_linux_srcs) $(testlib_rt) $(testlib_strings_$(PLATFORM)) $(testlib_errors_$(PLATFORM)) $(testlib_bytes_$(PLATFORM))
@printf 'HAREC \t$@\n'
@mkdir -p $(TESTCACHE)/io
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nio \
-t$(TESTCACHE)/io/io.td $(testlib_io_linux_srcs)
-$(TESTCACHE)/io/io-freebsd.ssa: $(testlib_io_freebsd_srcs) $(testlib_rt) $(testlib_strings_$(PLATFORM)) $(testlib_errors_$(PLATFORM))
+$(TESTCACHE)/io/io-freebsd.ssa: $(testlib_io_freebsd_srcs) $(testlib_rt) $(testlib_strings_$(PLATFORM)) $(testlib_errors_$(PLATFORM)) $(testlib_bytes_$(PLATFORM))
@printf 'HAREC \t$@\n'
@mkdir -p $(TESTCACHE)/io
@HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Nio \