summaryrefslogtreecommitdiff
path: root/src/estd/io/stdio.zig
blob: d89b325b8570a7ec7ad238d412a26e28fc8a3851 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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`.
///
/// 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 `value` to the standard output stream.
		pub fn serialize(self: *Self, value: anytype) Error!void {
			return switch (@typeInfo(@TypeOf(value))) {
				.void => {},
				.bool, .int, .float => {
					try self.writer.write(mem.asBytes(value));
				},
				else => @compileError("`value` 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(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"),
			};
		}
	};
}

/// 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) {}