I am trying to produce a struct that holds references to each of the readers and writers of stdin/stdout/stderr
I am stuck at trying to declare the appropriate types however. My simplest code:
const std = @import("std");
const IoTrio = struct {
in: std.io.GenericReader,
out: std.io.GenericWriter,
err: std.io.GenericWriter,
};
pub fn giveIo() !IoTrio {
const stdout = std.io.getStdOut().writer();
var stdin_bo = std.io.bufferedReader(std.io.getStdIn().reader());
const stdin = stdin_bo.reader();
const stderr = std.debug;
@compileLog(@TypeOf(stdin ));
@compileLog(@TypeOf(stdout));
@compileLog(@TypeOf(stderr));
return IoTrio{
.in = stdin,
.out = stdout,
.err = std.debug,
};
}
pub fn main() !void {
_ = giveIo();
}
The struct fails compilation with:
Produces error:
types.zig:5:15: error: expected type 'type', found 'fn (comptime type, comptime type, comptime anytype) type'
in: std.io.GenericReader,
~~~~~~^~~~~~~~~~~~~~
I looked at this answer but unfortunately that doesn't really help - as noted by the author of the answer, the output is convoluted, and isn't easy to make sense of - and the discussion ends there.
Indeed, the output is rather confusing:
@as(type, io.GenericReader(*io.buffered_reader.BufferedReader(4096,io.GenericReader(fs.File,error{AccessDenied,Unexpected,InputOutput,BrokenPipe,SystemResources,OperationAborted,WouldBlock,ConnectionResetByPeer,IsDir,ConnectionTimedOut,NotOpenForReading,SocketNotConnected},(function 'read'))),error{AccessDenied,Unexpected,InputOutput,BrokenPipe,SystemResources,OperationAborted,WouldBlock,ConnectionResetByPeer,IsDir,ConnectionTimedOut,NotOpenForReading,SocketNotConnected},(function 'read')))
@as(type, io.GenericWriter(fs.File,error{AccessDenied,Unexpected,DiskQuota,FileTooBig,InputOutput,NoSpaceLeft,DeviceBusy,InvalidArgument,BrokenPipe,SystemResources,OperationAborted,NotOpenForWriting,LockViolation,WouldBlock,ConnectionResetByPeer},(function 'write')))
@as(type, type)
It seems to imply that each is a function? But I cannot insert these values into my struct declaration, nor do I understand what this is saying to allow me to guess at something more sensible.
How do I read these outputs, to derive the needed type notations for my structure ?
Storing readers and writers in a struct is hard.
First problem is that the function GenericWriter
is an unfortunate name. It is function that constructs a generic writer from a write function and some context for the write function implementation.
Notice that even if your code compiled, you have a use-after-free. The buffered reader creates a buffer on the stack but this stack gets invalidated on function exit, leaving you with a reader from invalid memory.
Where should this buffer then live? It can't be inside your struct, sice then all generic readers would have to have enough space to store the biggest buffer used in your program. It also can't live on the heap, since Zig doesn't allocate implicitly. You would also have to then free it, which you don't.
If you don't want to rely on specific reader/writer implementation, it is easy to create all readers and writer you need in main and then pass them down to other functions as a "writer: anytype" argument, instead of passing them up like you do here.
If you really need to store them in structs, there is AnyWriter
and AnyReader
. But using these correctly is hard, since you have to manage their states on your own. Here is an example of how you could do it:
const std = @import("std");
const IoTrio = struct {
in: std.io.AnyReader,
out: std.io.AnyWriter,
err: std.io.AnyWriter,
// here you store the implementation-specific data
state: union(enum) {
std: struct {
// for example, the buffered stdin reader looks like this
stdin_buffer: *std.io.BufferedReader(4096, std.fs.File.Reader),
},
something_else: struct {
// something
},
},
fn fromSomethingElse() IoTrio {
return .{};
}
fn fromStd(a: std.mem.Allocator) !IoTrio {
// stdout and stderr are easy, they don't need to be cleaned up
const stdout = std.io.getStdOut().writer();
const stderr = std.io.getStdErr().writer();
// buffered stdin is harder. You have to rememeber that you allocated
// the buffer and later free the buffer.
const buffer_impl = std.io.bufferedReader(std.io.getStdIn().reader());
const buffer_impl_on_heap = try a.create(@TypeOf(buffer_impl));
buffer_impl_on_heap.* = buffer_impl;
const stdin = buffer_impl_on_heap.reader();
return IoTrio{
.in = stdin.any(),
.out = stdout.any(),
.err = stderr.any(),
.state = .{ .std = .{ .stdin_buffer = buffer_impl_on_heap } },
};
}
fn deinit(self: *const IoTrio, a: std.mem.Allocator) void {
switch (self.state) {
.std => |s| {
a.destroy(s.stdin_buffer);
},
.something_else => {
// something
},
}
}
};
pub fn main() !void {
const a = std.heap.page_allocator; // slideware, get a better allocator!
const io = try IoTrio.fromStd(a);
defer io.deinit(a);
try io.out.print("Hello {s}!", .{"world"});
}
This code creates the readers and writers, allocates their state on the heap and cleans up after them when you call deinit. All this happens automatically if you get to create them on the stack and bound their lifetime to some local scope:
const std = @import("std");
pub fn main() !void {
const out = std.io.getStdOut().writer();
const err = std.io.getStdErr().writer();
var in_impl = std.io.bufferedReader(std.io.getStdIn().reader());
const in = in_impl.reader();
try takeIo(in, out, err);
}
fn takeIo(in: anytype, out: anytype, err: anytype) !void {
_ = in;
_ = err;
try out.print("Hello {s}!\n", .{"world"});
}