Skip to content
CryoCryo home

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.

Types

BufWriter<W>

type struct BufWriter<W> where W: Write {
    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::drop flushes pending bytes, so call it before the wrapped sink goes away or buffered writes are lost.

Properties

PropertyType
writerW*
bufferu8*
capacityu64
lengthu64

Constructors

static new(writer: W*) -> BufWriter<W> where W: Write;

Wrap writer with a DEFAULT_BUFFER_SIZE-byte buffer. Panics on allocation failure.

static with_capacity(writer: W*, capacity: u64) -> BufWriter<W> where W: Write;

Wrap with an explicit buffer size. capacity == 0 is permitted but degenerate: every write hits the sink directly. Panics on allocation failure.

Methods

get_ref(&this) -> W*;

Borrow the inner writer. Useful for operations that bypass buffering (e.g., a direct flush on the underlying fd). Callers mutating through this must call BufWriter::flush first, or buffered bytes will land out of order.

buffered(&this) -> u64;

Number of bytes currently buffered but not yet flushed.

capacity(&this) -> u64;
clear(mut &this) -> void;

Discard buffered bytes without writing them. Useful on error paths where the caller knows the output is garbage.

flush_buffer(mut &this) -> Result<(), IoError>;

Write buffered bytes to the sink and reset. Retries are delegated to the sink's write_all.

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.

Properties

PropertyType
innerBufWriter<W>

Constructors

static new(writer: W*) -> LineWriter<W> where W: Write;
static with_capacity(writer: W*, capacity: u64) -> LineWriter<W> where W: Write;

Methods

get_ref(&this) -> W*;

BufReader<R>

type struct BufReader<R> where R: Read {
    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.

Properties

PropertyTypeNotes
readerR*
bufferu8*
capacityu64
positionu64First unread byte in the buffer.
filledu64One past the last filled byte.

Constructors

static new(reader: R*) -> BufReader<R> where R: Read;
static with_capacity(reader: R*, capacity: u64) -> BufReader<R> where R: Read;

Methods

get_ref(&this) -> R*;
capacity(&this) -> u64;
available(&this) -> u64;

Bytes currently buffered and unread.

consume(mut &this, amount: u64) -> void;

Advance the buffer cursor past amount bytes. amount must not exceed available(); a larger value is a caller bug.

fill_buf(mut &this) -> Result<Slice<u8>, IoError>;

Fill the buffer from the source if empty, then surface the filled region. Returns an empty slice at EOF. Paired with consume for zero-copy parsing: read a prefix, decide how many bytes are actually wanted, consume that many.

Trait implementations

BufWriter

implement<W> trait Write for struct BufWriter<W> where W: Write
implement<W> trait Drop for struct BufWriter<W>

LineWriter

implement<W> trait Write for struct LineWriter<W> where W: Write
implement<W> trait Drop for struct LineWriter<W>

BufReader

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.

Properties

PropertyType
innerS
rbufArray<u8>
rposu64
scratchArray<u8>
wbufArray<u8>

Constructors

static of(inner: S) -> BufStream<S>;

Wrap an already-connected transport.

Methods

compact(mut &this) -> void;

Drop the consumed prefix so a long-lived connection's buffer tracks what is live rather than everything ever read. Called only at the start of a fill: moving bytes at any other time would shift buffered() under a caller mid-parse. (Internal to the connection, but not private — the trait impl blocks are separate scopes and could not reach it.)

transport(mut &this) -> S*;

The transport, for callers that need it directly (socket options, the negotiated ALPN protocol). Not for reading or writing — that would desynchronize the buffers.

Trait implementations

implement<S> trait AsyncRead for struct BufStream<S>
implement<S> trait AsyncWrite for struct BufStream<S>
implement<S> trait Drop for struct BufStream<S>

Constants

const DEFAULT_BUFFER_SIZE: u64 = 8192;
const BYTE_LF:             u8  = 0x0A;
const FILL_CHUNK:          u64 = 4096;