How to determine types of Reader and Writer instances in Zig?

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 ?

1

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"});
}

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật