我正在尝试生成一个结构,该结构包含对 stdin/stdout/stderr 的每个读者和写者的引用
然而,我却无法声明适当的类型。我最简单的代码是:
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();
}
该结构编译失败:
Produces error:
types.zig:5:15: error: expected type 'type', found 'fn (comptime type, comptime type, comptime anytype) type'
in: std.io.GenericReader,
~~~~~~^~~~~~~~~~~~~~
我看了这个答案,但不幸的是,它并没有真正的帮助——正如答案的作者所指出的那样,输出是复杂的,不容易理解——讨论到此结束。
事实上,输出结果相当令人困惑:
@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)
这似乎暗示每个都是一个函数?但我无法将这些值插入到我的结构声明中,也不明白这是什么意思,让我猜测一些更合理的内容。
我如何读取这些输出来得出我的结构所需的类型符号?