Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

System

These modules are thin. Most are a named wrapper over one intrinsic, and the value is the name: std::os::env("HOME") says what it does where .env_var("HOME") says where it comes from. Prefer the wrappers — they are where shared behaviour accumulates, and std::fs’s streaming decoder is already an example of a wrapper doing real work.

std::io — console

pro[exp] write(value: str): str
pro[exp] write_err(value: str): str
pro[exp] read_key(): int
pro[exp] read_key_ms(timeout: int): int
pro[exp] echo_int(value: int): int
pro[exp] echo_str(value: str): str
pro[exp] echo_bool(value: bol): bol
pro[exp] echo_chr(value: chr): chr

write adds no newline and flushes, and returns its argument unchanged so it can sit inside an expression. The echo_* family prints a value in its FOL form and forwards it, which suits tracing.

read_key blocks for one byte and yields -1 at end of input. read_key_ms gives up after the timeout, yielding -2 — a distinct value, so a timeout is not mistaken for a closed stream.

std::fs — files

Whole-file access:

pro[exp] read_file(path: str): str
pro[exp] write_file(path: str, contents: str): int
pro[exp] dir_list(path: str): str

read_file returns "" for a missing file, an empty file, and a file that is not valid UTF-8. When those need telling apart, or the file is not text, read it as bytes with the .read_bytes(...) intrinsic and convert with .str_from_bytes(...).

Streaming

A file larger than memory, or one still being written, cannot be handled by the whole-file routines at all.

con[exp] MODE_READ: int = 0;
con[exp] MODE_WRITE: int = 1;
con[exp] MODE_APPEND: int = 2;
con[exp] MODE_UPDATE: int = 3;

con[exp] FROM_START: int = 0;
con[exp] FROM_HERE: int = 1;
con[exp] FROM_END: int = 2;

pro[exp] open_read(path: str): int
pro[exp] pipe(): vec[int]
pro[exp] open_write(path: str): int
pro[exp] open_append(path: str): int
pro[exp] open_update(path: str): int
pro[exp] read_chunk(handle: int, count: int): vec[int]
pro[exp] write_chunk(handle: int, bytes: vec[int]): int
pro[exp] write_text(handle: int, text: str): int
pro[exp] seek(handle: int, offset: int, whence: int): int
pro[exp] rewind(handle: int): int
pro[exp] flush(handle: int): int
pro[exp] sync(handle: int): int
pro[exp] close(handle: int): int

A handle is an integer. The open_* routines exist because the underlying .file_open(path, mode) takes an integer mode, matching the selector arguments elsewhere on that surface; these give the modes names.

Reads and writes are bytes, which keeps the surface binary-safe.

pipe() creates an anonymous binary pipe and returns [read_handle, write_handle]. The handles use the same read/write/close operations as files. Failure returns [-1, -1] and sets .os_error_kind(). Close both handles when finished; a reader cannot observe EOF while any copy of the write end remains open. Pipe creation requires hosted std and is a procedure, not a pure function.

A positive-sized read_chunk returning no bytes means EOF only when .os_error_kind() is zero. Read/write/open/close failures set that error channel, and successful operations clear it. Check it immediately, before another host operation can replace it. A short nonempty read is not EOF.

write_chunk and write_text answer the number of bytes written, not a status code – -1 is the failure value, so test for that rather than for zero. A short write is possible on a pipe or a full disk, which is why the count is returned at all. flush, seek, rewind and close do answer 0 on success.

Decoding a chunked read

This is the trap, and it is worth stating plainly. A fixed-size read splits multi-byte characters across chunk boundaries, and a chunk that ends mid-character is not valid UTF-8 — so converting each chunk on its own silently loses those characters. Reading héllo two bytes at a time yields lo.

Decoder holds the partial character until the rest of it arrives:

typ[exp] Decoder: rec = { carry: vec[int], text: str };

fun[exp] decoder(): Decoder
fun[exp] feed(state: Decoder, chunk: vec[int]): Decoder
pro[exp] read_streamed(path: str, chunk: int): str

feed returns the new state, whose text is what became decodable from that chunk. read_streamed wraps the whole pattern and is lossless at every chunk size:

var whole: str = std::fs::read_streamed(path, 4096);

Use it in preference to hand-rolling the loop.

Paths, atomic writes, and locking

pro[exp] resolve(path: str): str
pro[exp] temp_path(prefix: str): str
pro[exp] write_atomic(path: str, contents: str): int
pro[exp] make_link(target: str, link: str): int
pro[exp] lock_exclusive(handle: int): int
pro[exp] try_lock_exclusive(handle: int): int
pro[exp] lock_shared(handle: int): int
pro[exp] unlock(handle: int): int

resolve asks the filesystem, so symlinks and .. come out resolved as they really are. std::path::normalize does it textually and is a different answer whenever a symlink is involved — use resolve for any decision about whether a path stays inside an allowed directory.

write_atomic writes a temp file and renames it over the target, so a reader never sees a half-written file:

var wrote: int = std::fs::write_atomic("config.toml", rendered);

Rename is atomic only within a filesystem. write_atomic uses the temp directory, so writing across a mount boundary falls back to a copy and loses the guarantee; when that matters, make the temp file next to the target yourself with temp_path and call rename_file.

Atomic visibility is separate from durable publication. std::fs::flush flushes buffers; std::fs::sync(handle) asks the OS to synchronize data and metadata and returns -1 on error. To publish a staged Linux file durably, sync the staged file, rename it, and sync its containing directory using an open directory handle. Treat a directory sync failure after rename as an unconfirmed publication, not as evidence that the old file remains visible.

try_lock_exclusive returns -1 immediately instead of blocking, which is how a program reports “already running” rather than hanging:

var handle: int = std::fs::open_update("/tmp/mytool.lock");
if (std::fs::try_lock_exclusive(handle) != 0) {
    std::io::write("another instance is running\n");
    return 1;
} else {
};

The lock is advisory: it binds only processes that also ask, and does not stop an unrelated writer.

std::os — errors, process, environment

Why the last call failed

Every fallible routine in std reports failure as a sentinel — -1, or an empty string — which says that something went wrong and nothing about what. These read the reason afterwards:

pro[exp] error(): str
pro[exp] error_kind(): int
pro[exp] failed(): bol
pro[exp] not_found(): bol
pro[exp] denied(): bol
var text: str = std::fs::read_file(path);
if (std::os::not_found()) {
    std::io::write("no such file: " + path + "\n");
} else {
};

A successful call clears them, so a stale reason is never mistaken for a fresh one. Prefer the predicates and the ERR_* constants over matching error() text — the message is the operating system’s wording, not a contract.

ERR_NONE, ERR_NOT_FOUND, ERR_DENIED, ERR_EXISTS, ERR_REFUSED, ERR_RESET, ERR_ABORTED, ERR_NOT_CONNECTED, ERR_ADDR_IN_USE, ERR_ADDR_UNAVAILABLE, ERR_BROKEN_PIPE, ERR_WOULD_BLOCK, ERR_TIMED_OUT, ERR_INVALID_INPUT, ERR_INVALID_DATA, ERR_UNEXPECTED_EOF, ERR_INTERRUPTED, ERR_WRITE_ZERO, ERR_OTHER.

Byte sockets

pro[exp] tcp_connect_timeout(address: str, millis: int): int
pro[exp] tcp_read_bytes(handle: int, count: int): vec[int]
pro[exp] tcp_try_read_bytes(handle: int, count: int): vec[int]
pro[exp] tcp_write_bytes(handle: int, bytes: vec[int]): int
pro[exp] tcp_try_write_bytes(handle: int, bytes: vec[int], offset: int): int
pro[exp] udp_recv_bytes(handle: int, limit: int): vec[int]
pro[exp] udp_send_bytes(handle: int, address: str, bytes: vec[int]): int

These std::os functions work on the handles returned by the network intrinsics. TCP has byte offsets, not message boundaries. A write can be short; retry only its unwritten suffix. TCP EOF returns an empty vector and no error. The try-read operation reports ERR_WOULD_BLOCK when nothing is ready.

UDP receives exactly one datagram per call; an empty datagram is valid, while an over-limit datagram is discarded with ERR_INVALID_DATA. For these functions, inspect std::os::error_kind() immediately after the operation.

Arguments and environment

pro[exp] arg_count(): int
pro[exp] arg(index: int): str
pro[exp] program(): str
pro[exp] env(name: str): str
pro[exp] set_env(name: str, value: str): int
pro[exp] unset_env(name: str): int
pro[exp] shell(command: str): int

arg excludes the program name, and an index past the end reads as "". program() is argv[0] — what a usage message should name.

unset_env is not set_env(name, ""): an empty value is still a present variable, and a child can tell the difference.

Supervised children

con[exp] SIGNAL_KILL: int = 0;
con[exp] SIGNAL_TERM: int = 15;

fun[exp] still_running(): int          // the -2 sentinel
pro[exp] spawn(program: str, args: vec[str]): int
pro[exp] spawn_io(program: str, args: vec[str], streams: vec[int]): int
pro[exp] child_id(handle: int): int
pro[exp] child_status(handle: int): int
pro[exp] child_running(handle: int): bol
pro[exp] await_child(handle: int): int
pro[exp] signal_child(handle: int, signum: int): int

shell and the run_* intrinsics block until the child finishes. These do not, so a program can start something, watch it, and stop it:

var args: vec[str] = {"9000"};
var server: int = std::os::spawn("./serve", args);
loop (std::os::child_running(server)) {
    std::time::sleep_ms(100);
};
var status: int = std::os::await_child(server);

child_status returns three things — the exit status, still_running() while it is going, or -1 for an unknown handle — so a poll loop cannot confuse “not yet” with “gone”. A polled child keeps its status, which is what makes the loop above safe: await_child after the poll returns the same value rather than failing on a handle the poll consumed. await_child is the collecting call, so it releases the handle – child_status afterwards answers -1, the unknown-handle value. Poll as often as you like; await once. signal_child with SIGNAL_TERM lets the child clean up; SIGNAL_KILL cannot be caught.

spawn_io takes exactly three stream selectors: [stdin, stdout, stderr]. Zero inherits the corresponding parent stream, -1 selects the null device, and a positive value duplicates an existing file or pipe handle. Invalid selectors or a failed spawn return -1 with .os_error_kind() set. The caller retains its handles; close unused parent pipe ends after spawning. Arguments are passed directly, without a shell. Drain output while feeding input to avoid backpressure deadlocks. These are direct-child handles, not automatic process-group or descendant supervision.

still_running() is a routine rather than a constant because a global binding needs a literal initializer and -2 is a negation.

shell runs through sh -c and returns the exit status: 128 + signal when a signal killed it, 127 when the shell could not start. Because it is a shell, the command is subject to quoting and word-splitting — when the arguments come from user input, use the .run_capture(...) or .run_status(...) intrinsics instead, which execute a program directly with no shell in between.

std::path — path surgery

Pure string manipulation. It never touches the filesystem, so it works on paths that do not exist yet.

con SEP: str = "/";

fun[exp] is_absolute(value: str): bol
fun[exp] join(left: str, right: str): str
fun[exp] file_name(value: str): str
fun[exp] parent(value: str): str
fun[exp] extension(value: str): str
fun[exp] file_stem(value: str): str
fun[exp] with_extension(value: str, ext: str): str
fun[exp] components(value: str): vec[str]
fun[exp] normalize(value: str): str

normalize resolves . and .. textually. That is not the same as resolving symlinks — a .. after a symlink means something different on disk — so use it for tidying displayed paths, not for security decisions.

extension returns "" when there is no dot, and file_stem is the file name without it.

std::term — terminal

pro[exp] raw_mode(enable: bol): bol
pro[exp] cols(): int
pro[exp] rows(): int

cols and rows report 80×24 when the size cannot be determined, so a redirected program still gets usable numbers.

Column counts are not string lengths: pad with .str_width(...), since 日本 occupies four columns while .len(...) reports six bytes.

std::time — clock

pro[exp] now_ms(): int
pro[exp] sleep_ms(ms: int): int
pro[exp] local_offset(epoch_secs: int): int
pro[exp] local_parts(epoch_secs: int): vec[int]

now_ms is wall-clock, so it can jump backwards when the system clock is adjusted. To measure a duration use the .mono_ns() intrinsic, which cannot.

.time_parts(...) is UTC-only, so a timestamp shown to a reader needs the local offset. local_parts applies it for you:

var fields: vec[int] = std::time::local_parts(std::time::now_ms() / 1000);

local_offset takes the instant because the offset is not a constant — the same zone is 3600 in January and 7200 in July, and the daylight-saving rule in force at that moment decides which.

std::sync — shared counters

typ[exp] Counter: rec = { handle: int };

pro[exp] counter(initial: int): Counter
pro[exp] read(slot: Counter): int
pro[exp] write(slot: Counter, value: int): int
pro[exp] add(slot: Counter, delta: int): int
pro[exp] next(slot: Counter): int
pro[exp] compare_swap(slot: Counter, expected: int, desired: int): int
pro[exp] swapped(slot: Counter, expected: int, desired: int): bol
pro[exp] add_saturating(slot: Counter, delta: int, ceiling: int): int
pro[exp] cores(): int
pro[exp] this_thread(): int
pro[exp] relax(): int

A Counter is a handle, so copying it shares the count rather than duplicating it — which is what lets one cross a [spn] boundary with no reference threading:

var tally: std::sync::Counter = std::sync::counter(0);
[spn]worker::run(tally);

This is deliberately narrower than mux[T]. A mutex protects arbitrary compound state and costs a lock; these compile to one instruction and cannot deadlock, because there is no window in which the value is held. The moment two fields have to agree, use mux[T].

add and next return the value before the addition, so concurrent callers each receive a distinct number — that is what makes next a ticket dispenser. compare_swap returns the value it found: equal to expected means the swap happened, anything else is the current value to retry against. add_saturating shows the retry loop that is built from it.

cores is what a worker pool should size itself from; more threads than that adds scheduling cost without adding throughput. Counters are never freed — they live until the process exits, which suits program-wide tallies and not per-request state.