I’m trying to understand the behavior of the new writer. The following code is not supposed to work, since stdout is not seekable, yet it does:
const std = @import("std");
pub fn main() !void {
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;
std.debug.print("mode = {}\n", .{stdout_writer.mode});
try stdout.print("Hello world\n", .{});
try stdout.flush();
// Posix call
_ = try std.posix.pwrite(2, "Hello world", 0);
}
mode = .positional
Hello world
error: Unseekable
std.File.Writers that start in .positional mode will transition to .streaming mode and retry whenever any seeking operations fail.
You can observe this behavior if you check the mode again after flushing:
const std = @import("std");
pub fn main() !void {
var stdout_buffer: [1024]u8 = undefined;
var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
const stdout = &stdout_writer.interface;
std.debug.print("mode = {}\n", .{stdout_writer.mode});
try stdout.print("Hello world\n", .{});
std.debug.print("mode = {}\n", .{stdout_writer.mode});
try stdout.flush();
std.debug.print("mode = {}\n", .{stdout_writer.mode});
}
mode = .positional
mode = .positional
Hello world
mode = .streaming
(This is the output you get on Linux/POSIX, Windows has a slightly different behavior.)
1 Like
Ah, I see. My wasi handler for pwrite() wasn’t returning ESPIPE so I was getting different behavior there.