summaryrefslogtreecommitdiff
path: root/src/estd/io/stdio.zig
diff options
context:
space:
mode:
authorAlexander Rolley <arolley35@gmail.com>2025-02-14 11:06:32 +0100
committerAlexander Rolley <arolley35@gmail.com>2025-02-14 11:06:32 +0100
commit080bb49438fe770164382e2ac109dd8283600b16 (patch)
tree63c3f518c92159d0225d7285fbab284f1704faff /src/estd/io/stdio.zig
parent7313faeb25476147208f0b03c9261f6e6bec52eb (diff)
Start on stdio.
Diffstat (limited to 'src/estd/io/stdio.zig')
-rw-r--r--src/estd/io/stdio.zig65
1 files changed, 65 insertions, 0 deletions
diff --git a/src/estd/io/stdio.zig b/src/estd/io/stdio.zig
new file mode 100644
index 0000000..8b5924e
--- /dev/null
+++ b/src/estd/io/stdio.zig
@@ -0,0 +1,65 @@
+const std = @import("std");
+const io = std.io;
+
+/// Returns a handle to the standard output stream, backed by a buffer of size
+/// `buffer_size`.
+///
+/// Note that the returned handle is not thread-safe.
+pub fn Stdout(comptime buffer_size: usize) type {
+ return struct {
+ writer: io.BufferedWriter(buffer_size, io.getStdOut()),
+
+ const Self = @This();
+
+ pub const Error = Self.writer.Error;
+
+ /// Tries to serialize the type `T` to the standard output stream.
+ pub fn serialize(self: *Self, comptime T: type) Error!void {
+ return switch (@typeInfo(T)) {
+ .void => {},
+ .bool => {
+ if (T) {
+ try self.writer.write("true");
+ } else {
+ try self.writer.write("false");
+ }
+ },
+ else => @compileError("`T` cannot be serialized"),
+ };
+ }
+ };
+}
+
+/// Returns a handle to the standard output stream, backed by an 8MiB buffer.
+///
+/// Note that the returned handle is not thread-safe.
+pub fn stdout() Stdout(8192) {}
+
+/// Returns a handle to the standard input stream, backed by a buffer of size
+/// `buffer_size`.
+///
+/// Note that the returned handle is not thread-safe.
+pub fn Stdin(comptime buffer_size: usize) type {
+ return struct {
+ reader: io.BufferedReader(buffer_size, io.getStdIn()),
+
+ const Self = @This();
+
+ pub const Error = error{
+ // TODO
+ } || Self.reader.Error;
+
+ /// Tries to deserialize the type `T` from the standard input stream.
+ pub fn deserialize(comptime T: type) Error!T {
+ return switch (@typeInfo(T)) {
+ .void => {},
+ else => @compileError("`T` cannot be deserialized"),
+ };
+ }
+ };
+}
+
+/// Returns a handle to the standard input stream, backed by an 8MiB buffer.
+///
+/// Note that the returned handle is not thread-safe.
+pub fn stdin() Stdin(8192) {}