Module compiler

io

Everything in io is a Stream: a handle to a file, a standard stream or a socket. The functions here are the low-level, non-blocking layer. read and write move as many bytes as are ready and never spin; buffers are Vec<byte>. Errors are IoError values in a Result.

Most programs use the blocking helpers on top of this layer from coil-stdlib: io::sync (println, write_all, read_to_end, read_line) and io::file (read_text, write_text):

use io::sync::{println};
use io::file::{read_text};

fn main() {
    let text = read_text("notes.txt")?;
    println(text)?;
}

To do several IO operations at once, run them as tasks: inside a task::scope a task that waits for IO lets the others run. Text conversion is in string (to_bytes, from_bytes). File and network access needs a grant when the program runs (--allow-read, --allow-write, --allow-net); see Permissions. TLS is the coil-tls package.

Built into the compiler.

Import what you use (use path::* is not allowed):

use io::{Stream, IoError};

Modules#

ModuleDescription
io::fsPaths and the file system: metadata, directories, rename, copy, symlinks.
io::fileRead and write whole files in one call.
io::syncBlocking IO helpers: println, read_line, write_all, read_to_end.
io::netTCP and UDP sockets.

Types#

NameDescription
StreamAn open file, socket or standard stream.
IoErrorThe error type of every io, io::fs and io::net operation.

Traits#

NameDescription
ReadTypes that bytes can be read from; implemented for Stream.
WriteTypes that bytes can be written to; implemented for Stream.

Functions#

NameDescription
stdinReturns a new stream on the process’s standard input.
stdoutReturns a new stream on the process’s standard output.
stderrReturns a new stream on the process’s standard error.
capture_beginStarts keeping this thread’s stdout and stderr writes in memory instead of writing them, until the matching capture_end().
capture_endEnds the innermost capture_begin() and returns what it kept, as (stdout, stderr).
openOpens the file at path with mode and returns a stream on it.
closeCloses s.
readReads bytes from s into the front of buf without waiting.
writeWrites bytes from buf to s without waiting and returns how many were written.
write_fromWrites buf[offset..] to s without waiting and returns how many bytes were written.
wait_readableWaits until s is readable.
wait_writableWaits until s is writable.
await_readableOlder name of wait_readable; behaves exactly the same.
await_writableOlder name of wait_writable; behaves exactly the same.
driveDeprecated: polls the registered IO waiters once and returns how many became ready.
wait_readyDeprecated: waits until any registered IO waiter is ready and returns how many became ready.
result_unit_probeReturns Ok(()) when n >= 0 and Err(IoError::InvalidInput) otherwise.
attachInstalls package IO hooks on s in place; the free-function form of Stream.attach.
parkWaits until the descriptor of s is ready; the free-function form of Stream.park.
↑ ↓ navigate ↵ open