buf
import std::io::buf; · source
The three blocking adapters here borrow the inner reader or writer through a pointer. You keep ownership of the underlying sink and remain responsible for dropping it; wrapping and unwrapping are free. The default buffer is 8 KiB, matching stdio's block-buffered default on most Unixes.
const DEFAULT_BUFFER_SIZE: u64 = 8192;
BufWriter<W>
type struct BufWriter<W> {
writer: W*;
buffer: u8*;
capacity: u64;
length: u64;
static new(writer: W*) -> BufWriter<W>
where W: Write;
static with_capacity(writer: W*, capacity: u64) -> BufWriter<W>
where W: Write;
get_ref(&this) -> W*;
buffered(&this) -> u64;
capacity(&this) -> u64;
clear(mut &this) -> void;
flush_buffer(mut &this) -> Result<(), IoError>;
}
Wrapping a sink in a BufWriter coalesces many small writes into one syscall per buffer-full.
import std::io::buf;
mut out: Stdout = stdout();
mut buffered: BufWriter<Stdout> = BufWriter<Stdout>::new(&out);
buffered.write(payload);
buffered.flush()?;
buffered() reports how much is waiting, clear() discards it without writing (for error paths), and flush_buffer pushes it to the sink without asking the sink to flush in turn. If you mutate through get_ref(), flush first — otherwise buffered bytes land out of order.
BufWriter::dropflushes pending bytes, so call it before the wrapped sink goes away or buffered writes are lost.
LineWriter<W>
type struct LineWriter<W> {
inner: BufWriter<W>;
static new(writer: W*) -> LineWriter<W>
where W: Write;
static with_capacity(writer: W*, capacity: u64) -> LineWriter<W>
where W: Write;
get_ref(&this) -> W*;
}
A BufWriter that flushes after any write containing \n. The right choice for interactive stdout on a TTY, where a prompt has to appear before the read that follows it.
BufReader<R>
type struct BufReader<R> {
reader: R*;
buffer: u8*;
capacity: u64;
position: u64;
filled: u64;
static new(reader: R*) -> BufReader<R>
where R: Read;
static with_capacity(reader: R*, capacity: u64) -> BufReader<R>
where R: Read;
get_ref(&this) -> R*;
capacity(&this) -> u64;
available(&this) -> u64;
consume(mut &this, amount: u64) -> void;
fill_buf(mut &this) -> Result<Slice<u8>, IoError>;
}
Pulls in buffer-sized chunks and serves reads from memory, and overrides read_line with a scan-and-copy fast path — the byte-at-a-time default on the trait is exact but slow on a socket or a file.
It also exposes the zero-copy parsing pair: fill_buf() returns the filled region as a Slice<u8> (empty at EOF), and consume(amount) advances past however many bytes you actually wanted. available() reports what is buffered and unread.
Trait implementations
implement<W> trait Write for struct BufWriter<W>
where W: Write
implement<W> trait Drop for struct BufWriter<W>
implement<W> trait Write for struct LineWriter<W>
where W: Write
implement<W> trait Drop for struct LineWriter<W>
implement<R> trait Read for struct BufReader<R>
where R: Read
implement<R> trait Drop for struct BufReader<R>
BufStream<S>
type struct BufStream<S> {
inner: S;
rbuf: Array<u8>;
rpos: u64;
scratch: Array<u8>;
wbuf: Array<u8>;
static of(inner: S) -> BufStream<S>;
compact(mut &this) -> void;
transport(mut &this) -> S*;
}
The non-blocking counterpart, implementing AsyncRead and AsyncWrite over any AsyncTransport. Unlike the adapters above it owns its transport, because a borrowed transport cannot be held across a suspension point. of(inner) wraps an already-connected transport, and transport() surfaces it for socket options or a negotiated ALPN protocol — not for reading or writing, which would desynchronize the buffers. compact slides unconsumed bytes to the front of the read buffer; the defaults call it when they need room.
net::http::conn and net::ws::frame extend it with protocol readers as inherent methods; see conn for why they are methods rather than free functions.
Trait implementations
implement trait AsyncRead for struct BufStream<S>
implement trait AsyncWrite for struct BufStream<S>
implement trait Drop for struct BufStream<S>