#3. Std tier
On this page
- Location
- Dependencies
- User import policy
- Module index
- Argument conventions
- Core design principles
- 1. Blocking API + *_async - two sets of traits
- 2. I/O backend - runtime detail
- 3. TLS 1.3 in std - HTTP out
- 4. Capability-based fs - Dir handles
- 5. UTF-8 Path (Python-style)
- 6. All I/O throws (IoError)
- Target availability
The standard library of the target-dependent area. It covers the file system, networking, time, OS threads, environment variables, processes, paths, the NABION format, the OS random source, OS-specific items, plugins, the panic hook and hot reload, and availability varies with the user's environment (operating system / platform).
#Location
The spec body lives under spec/en/refs-standard-libraries/300-std/.
#Dependencies
Upward, it depends on core and bare. Downward, user code stands in the dependent position, but std is pulled in only by an explicit import.
#User import policy
The implicit prelude does not include std items; std::* requires a separate explicit import. Because availability differs by target, the design makes users express platform dependencies explicitly.
use std::fs;
use std::time::SystemTime;
use std::thread::Builder;
#Module index
This tier consists of the following thirteen modules.
- 3.1.
fs::- file system (atomic ops, Dir capability, reflink copy) - 3.2.
net::- TCP / UDP / Unix + TLS 1.3 - 3.3.
time::- clocks, calendar dates and time zones (withInstant/Durationfromcore::time) - 3.4.
thread::- OS threads (1:1, separate fromtask::) - 3.5.
env::- environment variables / args / current_dir / home_dir - 3.6.
process::-Command/Child/ pidfd / kill_on_drop - 3.7.
path::- UTF-8Path(Python-style) +RawPathescape hatch - 3.8.
nabion::- the NABION data format (.nabion) - 3.9.
random::- random bytes, generators and distributions - 3.10.
os::- what only some OSes have:unix/linux/windows - 3.11.
plugin::- loading plugins into the program - 3.12.
panic::- the panic hook (set_hook/take_hook) - 3.13.
reload::- hot reload hooks and state (point/on_reload/State) wit::- the component model's async ABI that generated WIT bindings call (1.8). Bindings-internal: public so the generated code can reach it, not meant to be called directly, and not specified here.
#Argument conventions
Two shorthands hold in every signature of this tier.
- A path parameter written
Pathstands for a type parameterP: AsPath, so it accepts bothstrandPath(3.7.3a). Two path parameters are independent (P: AsPath, Q: AsPath). - The data of a write (
Write::write/write_all,send,send_to,send_with_fd, their_asyncforms, thefswrite functions) isB: AsBytes, sostr,List<u8>,Slice<u8>and[u8; N]can be passed (2.23.3).write_zerocopytakes aList<u8>.
A read buffer is a SliceMut<u8>: read(buf) fills buf[0..n] and returns n, 0 at the end of the stream. A List<u8> passes as a view of the whole list, buf[a:b] as a part of it.
#Core design principles
#1. Blocking API + *_async - two sets of traits
std I/O functions are sync and block the calling OS thread. To give up the worker while waiting inside a task, await the *_async method of the same operation (3.2.0). With stackless coroutines an ordinary function cannot suspend a task, so a sync call never becomes async on its own.
// blocking: on an OS thread (thread::spawn) or outside a task
fn fetch(addr: str) throws (IoError) -> List<u8> {
using conn = try net::tcp_connect(addr) {
try conn.read_to_end()
}
}
// async: suspends only the task
async fn fetch_async(addr: str) throws (IoError) -> List<u8> {
using conn = try await net::tcp_connect_async(addr) {
try await conn.read_to_end_async()
}
}
Stream I/O goes through the traits of core::io (2.23): Read / Write block, AsyncRead / AsyncWrite suspend the task, and the method names keep the _async suffix, so both are in scope at once. Operations that are not stream I/O (connect_async, accept_async, open_async, ..) are inherent methods and functions of the types. Sockets wait for I/O driver readiness (3.2.0); regular files run the blocking call on another OS thread (3.1.4a).
#2. I/O backend - runtime detail
Waiting for readiness is the runtime's responsibility (refs-lang 7.7). User code does not need to know which backend is running.
| Platform | Backend |
|---|---|
| Linux | epoll (edge-triggered) |
| macOS / FreeBSD | kqueue (EV_CLEAR) |
| Windows | IOCP |
| WASI | wasi:io/poll (wasm32-wasip2), poll_oneoff (wasm32-wasip1-threads) |
#3. TLS 1.3 in std - HTTP out
TLS 1.3 is security-critical. If every application implemented TLS itself, security vulnerabilities would multiply, so it is integrated into std.
use std::net::tls;
let conn = try net::tcp_connect("example.com:443");
let tls_conn = try tls::Client::new(conn, "example.com");
let req = b"GET / HTTP/1.1\r\nHost: example.com\r\n\r\n";
try tls_conn.write_all(req);
let resp = try tls_conn.read_to_end();
try tls_conn.close();
The stack is Nabi's own, written on core::crypto (2.21); no OS or third-party TLS library is used (3.2.5).
HTTP/1, HTTP/2 and HTTP/3 (QUIC), on the other hand, live outside std, in separate packages (http covers HTTP/1.1 and HTTP/2). This is the lesson from Go's net/http being frozen at 1.0.
#4. Capability-based fs - Dir handles
Follows the pre-opened directory handle model of WASI Preview 2. Every fs op goes through a Dir.
use std::fs::{Dir, File};
let root: Dir = Dir::root(); // capability, at process start
let data_dir: Dir = try root.open_dir("data"); // sub-cap
let f: File = try data_dir.open("config.toml");
let bytes = try f.read_to_end();
try f.close();
The model has three advantages. First, it avoids TOCTOU: open after open_dir is race-free. Second, sandboxing comes naturally: pass only a narrow Dir to a child task. Third, it is WASI-compatible: the same code works on native and WASI.
For convenience, fs::open(absolute_path) is also allowed. Internally it expands to Dir::root().open(...).
#5. UTF-8 Path (Python-style)
A decision to avoid the ergonomic burden of Rust's Path / PathBuf + OsStr.
use std::path::Path;
let p: Path = Path::new("data/config.toml");
let parent: Path = p.parent()?;
let with_ext: Path = p.with_extension("bak");
// `/` operator (Python pathlib style)
let full = Path::new("/etc") / "nginx" / "nginx.conf";
Path is a UTF-8 newtype. Non-UTF-8 file names (which occasionally exist on Unix) are handled separately with RawPath (bytes). Path alone should cover 99% of code.
The detailed comparison and rationale are in 3.7 path.
#6. All I/O throws (IoError)
Panics almost never occur (except OOM). File not found, permission denied and network failure are all thrown with a throws (IoError) signature and propagate by marking the call with try. There is no automatic conversion between error types; to turn one into another error type, wrap it in catch and rethrow.
fn load_config() throws (ConfigError) -> Config {
let bytes = try fs::read("config.toml") catch e: IoError {
throw ConfigError::Io(e); // IoError → ConfigError (explicit conversion)
};
try parse(bytes)
}
#Target availability
Module availability per platform:
| Module | linux/macos/windows | wasm32-wasip2 | wasm32-wasip1-threads | wasm32 (browser) |
|---|---|---|---|---|
std::fs | ✓ | ✓ (cap-based) | ✓ (cap-based) | ✗ (every call Unsupported) |
std::net | ✓ (+ TLS 1.3) | ✓ (wasi:sockets) | listeners passed in by the host | ✗ (use fetch) |
std::time | ✓ | ✓ | ✓ | ✓ (Date.now) |
std::thread | ✓ | one thread (Builder::spawn Unsupported) | ✓ (wasi-threads) | one thread (Builder::spawn Unsupported) |
std::env | ✓ | ✓ | ✓ | ✗ |
std::process | ✓ | ✗ | ✗ | ✗ |
std::path | ✓ | ✓ | ✓ | ✓ |
std::nabion | ✓ | ✓ | ✓ | ✓ |
std::random | ✓ | ✓ (random_get) | ✓ (random_get) | ✓ (crypto.getRandomValues, 3.9) |
std::wit | empty | ✓ | ✓ | empty |
std::os | its OS only (3.10) | absent | absent | absent |
std::panic | ✓ | ✓ | ✓ | ✓ |
✗ means the module builds but its operations fail at run time with IoErrorKind::Unsupported; it is not a compile-time absence. os::unix, os::linux and os::windows are the exception: they exist only on their targets. wit exists everywhere but has items only on the WASI targets ("empty"). In the browser, stdout / stderr go to the host console and stdin is at EOF. The availability section of each module spec gives per-platform behavior in more detail.
Found a mistake? Edit this page on GitHub. Last updated