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.
Zig › std/ › process/ › Preopens.zig › init
init
Preopens.init
pub fn init (arena : Allocator ) InitError !Preopens
File
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" , {});
map .putAssumeCapacityNoClobber ("stdout" , {});
map .putAssumeCapacityNoClobber ("stderr" , {});
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 );
// 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 , {});
}
}