Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

init

Preopens.init
pub fn init(arena: Allocator) InitError!Preopens

File

lib/std/process/Preopens.zig:49

Code

pub fn init(arena: Allocator) InitError!Preopens {
    if (native_os != .wasi) return .{ .map = {} };
    const wasi = std.os.wasi;
    var map: Map = .empty;

    try map.ensureUnusedCapacity(arena, 3);

    map.putAssumeCapacityNoClobber("stdin", {}); // 0
    map.putAssumeCapacityNoClobber("stdout", {}); // 1
    map.putAssumeCapacityNoClobber("stderr", {}); // 2
    while (true) {
        const fd: wasi.fd_t = @intCast(map.entries.len);
        var prestat: wasi.prestat_t = undefined;
        switch (wasi.fd_prestat_get(fd, &prestat)) {
            .SUCCESS => {},
            .OPNOTSUPP, .BADF => return .{ .map = map },
            else => return error.Unexpected,
        }
        try map.ensureUnusedCapacity(arena, 1);
        // This length does not include a null byte. Let's keep it this way to
        // gently encourage WASI implementations to behave properly.
        const name_len = prestat.u.dir.pr_name_len;
        const name = try arena.alloc(u8, name_len);
        switch (wasi.fd_prestat_dir_name(fd, name.ptr, name.len)) {
            .SUCCESS => {},
            else => return error.Unexpected,
        }
        map.putAssumeCapacityNoClobber(name, {});
    }
}