Skip to content
CryoCryo home

buffered

import std::io::buffered; · 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 DEFAULT_BUFFER_SIZE, 8 KiB, matching stdio's block-buffered default on most Unixes.

Types

BufferedWriter<W>

type struct BufferedWriter<W> where W: Write {
    private writer:   mut &W;
    private buffer:   mut u8*;
    private capacity: i64;
    private length:   i64;
}

Wrapping a sink in a BufferedWriter coalesces many small writes into one syscall per buffer-full.

import std::io::buffered::{ BufferedWriter };
import std::io::stdio::{ Stdout, stdout };

const payload: Str = "hello\n";
mut out: Stdout = stdout();
mut buffered: BufferedWriter<Stdout> = BufferedWriter::new(&out);
buffered.write(payload)?;
buffered.flush()?;

buffered() reports how much is waiting, capacity() the buffer's size, and clear() discards the waiting bytes without writing them (for error paths). If you write through get_reference(), flush first — otherwise buffered bytes land out of order.

Dropping a BufferedWriter flushes pending bytes, so let it go out of scope (or mem::drop it) before the wrapped sink does, or buffered writes are lost.

Constructors

static new(writer: mut &W) -> BufferedWriter<W> where W: Write;

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

static with_capacity(writer: mut &W, capacity: i64) -> BufferedWriter<W> where W: Write;

Wrap with an explicit buffer size. A capacity of zero or less is permitted but degenerate: no buffer is allocated and every write hits the sink directly. Panics on allocation failure.

Methods

get_reference(&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 BufferedWriter::flush first, or buffered bytes will land out of order.

buffered(&this) -> i64;

Number of bytes currently buffered but not yet flushed.

![inline]
capacity(&this) -> i64;

The size of the write buffer in bytes.

clear(mut &this) -> void;

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

LineWriter<W>

type struct LineWriter<W> {
    private inner: BufferedWriter<W>;
}

A BufferedWriter that flushes after any write containing \n. The right choice for interactive stdout on a terminal, where a prompt has to appear before the read that follows it.

Constructors

static new(writer: mut &W) -> LineWriter<W> where W: Write;

A writer over writer that flushes at the end of every line, with a buffer of the default size. writer must outlive it.

static with_capacity(writer: mut &W, capacity: i64) -> LineWriter<W> where W: Write;

As new, with a buffer of capacity bytes.

Methods

get_reference(&this) -> &W;

The writer underneath; flush this one first before writing to it, or buffered bytes land out of order.

BufferedReader<R>

type struct BufferedReader<R> where R: Read {
    private reader:   mut &R;
    private buffer:   mut u8*;
    private capacity: i64;
    private position: i64;
    private filled:   i64;
}

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.

lines() consumes the reader and iterates the rest of the source one line at a time. Each item is a Result<String, IoError> holding the line without its \n or \r\n; a last line with no line ending is still a line, and a line that is not UTF-8 is an InvalidData error, after which the next line reads as usual.

import std::fmt;
import std::fs::file::{ File };
import std::io::buffered::{ BufferedReader };

mut file: File = File::open("notes.txt")?;
for (line in BufferedReader::new(&file).lines()) {
    const text: String = line?;
    fmt::println(f"{text.as_str()}");
}

It also exposes the zero-copy parsing pair: fill_buffer() 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.

Constructors

static new(reader: mut &R) -> BufferedReader<R> where R: Read;

A reader that fills a buffer of the default size from reader, which must outlive it.

static with_capacity(reader: mut &R, capacity: i64) -> BufferedReader<R> where R: Read;

A reader with a buffer of capacity bytes. Panics when capacity is not positive.

Methods

get_reference(&this) -> &R;

The reader underneath. Reading from it directly skips the bytes this BufferedReader has already buffered.

![inline]
capacity(&this) -> i64;

The size of the read buffer in bytes.

available(&this) -> i64;

Bytes currently buffered and unread.

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

Advance the buffer cursor past amount bytes. amount must not exceed available(); a larger value is a caller bug, and a negative one panics "out of range".

fill_buffer(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.

lines(this) -> Lines<R>;

The rest of the source, one line at a time: each item is an owned String without its \n or \r\n, or the error that stopped the reading. A last line with no line ending is still a line.

Lines<R>

type struct Lines<R> where R: Read {}

The iterator BufferedReader.lines() returns.

Trait implementations

BufferedWriter

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

LineWriter

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

BufferedReader

implement<R> trait Read for struct BufferedReader<R> where R: Read
implement<R> trait Drop for struct BufferedReader<R>

Lines

implement<R> trait Iterator<Result<String, IoError>> for struct Lines<R> where R: Read

BufferedStream<S>

type struct BufferedStream<S> {
    private inner:   S;
    private rbuf:    Array<u8>;
    private rpos:    i64;
    private scratch: Array<u8>;
    private wbuf:    Array<u8>;
}

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. A fill first drops the consumed prefix of the read buffer, so a long-lived connection's buffer tracks what is live rather than everything ever read.

net::http::connection and net::ws::frame extend it with protocol readers as inherent methods; see connection for why they are methods rather than free functions.

Constructors

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

Wrap an already-connected transport.

Methods

transport(mut &this) -> mut 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 BufferedStream<S>
implement<S> trait AsyncWrite for struct BufferedStream<S>
implement<S> trait Drop for struct BufferedStream<S>

Constants

const DEFAULT_BUFFER_SIZE: i64 = 8192;