From 080bb49438fe770164382e2ac109dd8283600b16 Mon Sep 17 00:00:00 2001 From: Alexander Rolley Date: Fri, 14 Feb 2025 11:06:32 +0100 Subject: Start on stdio. --- src/estd/io/root.zig | 1 + src/estd/io/stdio.zig | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/estd/root.zig | 1 + 3 files changed, 67 insertions(+) create mode 100644 src/estd/io/root.zig create mode 100644 src/estd/io/stdio.zig diff --git a/src/estd/io/root.zig b/src/estd/io/root.zig new file mode 100644 index 0000000..704fdd7 --- /dev/null +++ b/src/estd/io/root.zig @@ -0,0 +1 @@ +pub const stdio = @import("stdio.zig"); 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) {} diff --git a/src/estd/root.zig b/src/estd/root.zig index 5ff292e..84f1fcb 100644 --- a/src/estd/root.zig +++ b/src/estd/root.zig @@ -3,6 +3,7 @@ const std = @import("std"); pub const cursor = @import("cursor.zig"); pub const parser = @import("parser/root.zig"); pub const graphics = @import("graphics/root.zig"); +pub const io = @import("io/root.zig"); test { std.testing.refAllDecls(@This()); -- cgit v1.2.3-70-g09d2 From bff7daf2d430062fc37cbf3b69fe102a5b0bad58 Mon Sep 17 00:00:00 2001 From: Alexander Rolley Date: Fri, 14 Feb 2025 15:12:07 +0100 Subject: Implement (de)serialize for bool, int, float. --- src/estd/io/stdio.zig | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/estd/io/stdio.zig b/src/estd/io/stdio.zig index 8b5924e..d89b325 100644 --- a/src/estd/io/stdio.zig +++ b/src/estd/io/stdio.zig @@ -1,5 +1,6 @@ const std = @import("std"); const io = std.io; +const mem = std.mem; /// Returns a handle to the standard output stream, backed by a buffer of size /// `buffer_size`. @@ -13,18 +14,14 @@ pub fn Stdout(comptime buffer_size: usize) type { 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)) { + /// Tries to serialize the `value` to the standard output stream. + pub fn serialize(self: *Self, value: anytype) Error!void { + return switch (@typeInfo(@TypeOf(value))) { .void => {}, - .bool => { - if (T) { - try self.writer.write("true"); - } else { - try self.writer.write("false"); - } + .bool, .int, .float => { + try self.writer.write(mem.asBytes(value)); }, - else => @compileError("`T` cannot be serialized"), + else => @compileError("`value` cannot be serialized"), }; } }; @@ -50,9 +47,17 @@ pub fn Stdin(comptime buffer_size: usize) type { } || Self.reader.Error; /// Tries to deserialize the type `T` from the standard input stream. - pub fn deserialize(comptime T: type) Error!T { + pub fn deserialize(self: *Self, comptime T: type) Error!T { return switch (@typeInfo(T)) { .void => {}, + .bool, .int, .float => { + const size = @sizeOf(T); + const buffer: [size]u8 = 0; + + try self.reader.read(buffer); + + return mem.bytesToValue(T, buffer); + }, else => @compileError("`T` cannot be deserialized"), }; } -- cgit v1.2.3-70-g09d2