Skip to content

std.io

Reader/Writer, std streams, FixedBuf, NullWriter, printing; no allocation

pub IoError :: errors {
write_failed,
short_write,
read_failed,
end_of_stream,
buffer_full,
}
  • write_failed: The underlying write call returned zero bytes
  • short_write: A partial write where a full write was required
  • read_failed: The underlying read call failed
  • end_of_stream: The reader reached the end of available data
  • buffer_full: A FixedBuf ran out of space mid-write
pub Writer :: interface {
write :: fn(self: *Self, data: []const u8) -> usize ! IoError;
flush :: fn(self: *Self) -> void ! IoError;
}

byte sink; use *Writer for dynamic dispatch

pub Reader :: interface {
read :: fn(self: *Self, buf: []u8) -> usize ! IoError;
}

byte source; read gives <= buf.len, may give fewer; 0 only at end (or for an empty buf)

pub Stdout :: struct {
marker: u8,
}
pub Stderr :: struct {
marker: u8,
}
pub stdout :: fn() -> Stdout
pub stderr :: fn() -> Stderr
pub Stdin :: struct {
marker: u8,
}

stdin as a Reader; read gives 0 at end (closed pipe, Ctrl+Z / Ctrl+D)

pub stdin :: fn() -> Stdin
pub SliceReader :: struct {
rest: []const u8,
}
  • rest: the bytes not read yet
pub slice_reader :: fn(data: []const u8) -> SliceReader
pub copy :: fn(dst: *Writer, src: *Reader, buf: []u8) -> u64 ! IoError

copy all of src to dst via buf; returns bytes copied

pub FixedBuf :: struct {
data: [256]u8,
pos: usize,
}
pub fixed_buf :: fn() -> FixedBuf
pub buf_len :: fn(self: *FixedBuf) -> usize

bytes written

pub buf_remaining :: fn(self: *FixedBuf) -> usize

bytes left

pub buf_reset :: fn(self: *FixedBuf)

clear for reuse

pub buf_flush_stdout :: fn(self: *FixedBuf) -> usize ! IoError

flush to stdout in one syscall

pub buf_flush_stderr :: fn(self: *FixedBuf) -> usize ! IoError

flush to stderr in one syscall

pub buf_flush :: fn(self: *FixedBuf, sink: *Writer) -> usize ! IoError

flush to any *Writer in one call

pub NullWriter :: struct {
count: usize,
}
pub null_writer :: fn() -> NullWriter
pub null_reset :: fn(self: *NullWriter)

zero the byte count

pub write_all :: fn(self: *Writer, data: []const u8) -> usize ! IoError

all of data or fail; a short write is short_write

pub write_line :: fn(self: *Writer, data: []const u8) -> usize ! IoError

data then “\n”

pub write_char :: fn(self: *Writer, ch: u8) -> usize ! IoError
pub write_repeat :: fn(self: *Writer, ch: u8, count: usize) -> usize ! IoError

count copies of ch

pub write_bool :: fn(self: *Writer, value: bool) -> usize ! IoError
pub write_u64 :: fn(self: *Writer, value: u64) -> usize ! IoError

decimal; recursion depth <= 20

pub write_i64 :: fn(self: *Writer, value: i64) -> usize ! IoError

decimal; i64 min is safe: abs via -(n+1)+1, no overflow

pub write_hex_byte :: fn(self: *Writer, value: u8) -> usize ! IoError

two lowercase hex digits

pub write_hex_u64 :: fn(self: *Writer, value: u64) -> usize ! IoError

lowercase hex, no leading zeros, no prefix; recursion depth <= 16

pub write_hex_u64_full :: fn(self: *Writer, value: u64) -> usize ! IoError

exactly 16 lowercase hex digits, no prefix

pub print :: fn(f: Format) -> usize ! IoError
pub println :: fn(f: Format) -> usize ! IoError
pub eprint :: fn(f: Format) -> usize ! IoError
pub eprintln :: fn(f: Format) -> usize ! IoError
pub print_u64 :: fn(value: u64) -> usize ! IoError
pub print_i64 :: fn(value: i64) -> usize ! IoError
pub print_hex_u64 :: fn(value: u64) -> usize ! IoError
pub print_bool :: fn(value: bool) -> usize ! IoError
pub eprint_u64 :: fn(value: u64) -> usize ! IoError
pub eprint_i64 :: fn(value: i64) -> usize ! IoError
pub write_fmt :: fn(self: *Writer, f: Format) -> usize ! IoError

write f through a 512-byte buffer, so a line is one write

pub write_fmt_line :: fn(self: *Writer, f: Format) -> usize ! IoError

write_fmt + newline

pub show_value :: fn(f: *Formatter, d: *const u8, id: usize, ti: TypeInfo, spec: []const u8)

Formatter.show for std.io: the value at d (type ti, type id id) per spec