stdio
import std::io::stdio; · source
Three thin handles over file descriptors 0, 1, and 2, obtained from stdin(), stdout(), and stderr(). Each implements Read or Write and inherits every default on that trait. There is no buffering at this layer: stdout() translates to raw write(2) calls. Compose it explicitly with buf when you want it.
function stdin() -> Stdin;
function stdout() -> Stdout;
function stderr() -> Stderr;
function is_tty(fd: i32) -> boolean;
const STDIN_FD: i32 = 0;
const STDOUT_FD: i32 = 1;
const STDERR_FD: i32 = 2;
const NEWLINE: Str = Str::new("\n");
Stdin
type struct Stdin {
is_tty(&this) -> boolean;
as_fd(&this) -> i32;
lock(&this) -> StdinLock;
line(mut &this) -> Result<Option<String>, IoError>;
prompt(mut &this, prompt_msg: Str) -> Result<Option<String>, IoError>;
}
import std::io::stdio;
mut input: Stdin = stdin();
match (input.line()) {
Result::Ok(Option::Some(text)) => { /* one line, newline stripped */ }
Result::Ok(Option::None) => { /* EOF */ }
Result::Err(e) => { /* IoError */ }
}
Every handle carries is_tty(), as_fd(), and lock(). Stdin adds two conveniences: line() reads one line into an owned String with the newline stripped, and prompt(msg) writes the prompt to stderr — so it doesn't pollute piped stdout — then reads a line.
Stdout and Stderr
type struct Stdout {
is_tty(&this) -> boolean;
as_fd(&this) -> i32;
lock(&this) -> StdoutLock;
}
type struct Stderr {
is_tty(&this) -> boolean;
as_fd(&this) -> i32;
lock(&this) -> StderrLock;
}
The two write handles, identical in shape. eprint and friends in fmt write through Stderr.
The lock guards
type struct StdinLock {}
type struct StdoutLock {}
type struct StderrLock {}
lock() returns a guard that holds the stream's process-wide mutex until it drops, so a burst of writes from one thread is not interleaved with another's. The guards implement the same trait as the stream they lock, and release the mutex when dropped.
Trait implementations
implement trait Read for struct Stdin
implement trait Write for struct Stdout
implement trait FmtWrite for struct Stdout // std::fmt::write
implement trait Write for struct Stderr
implement trait FmtWrite for struct Stderr // std::fmt::write
implement trait Read for struct StdinLock
implement trait Drop for struct StdinLock
implement trait Write for struct StdoutLock
implement trait Drop for struct StdoutLock
implement trait Write for struct StderrLock
implement trait Drop for struct StderrLock