BufferedWriter structv0.1.0

Source
A writer that gathers small writes into a buffer and sends it in one piece.
pub struct BufferedWriter {
    // private fields
}

Move-only: it owns its buffer. Its destructor flushes as a safety net and discards the outcome, because a destructor has nowhere to report; the explicit Flush is where errors are seen, and the caller who cares calls it before letting go.

Associated functions

New

pub func New(
    allocator: Allocator,
    sink: Writer,
    capacity: uint
) -> BufferedWriter ! IoError

A buffered writer over sink with capacity bytes of buffer.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
sinkWriterwhere the bytes go
capacityuinthow many bytes the buffer holds

Returns: the buffered stream; Other when the allocator refused the buffer

Source

Methods

Pending

pub func Pending(self: &BufferedWriter) -> uint

How many bytes wait unsent.

Returns: how many bytes are written but not yet flushed

Source

Flush

pub func Flush(self: &var BufferedWriter) -> ! IoError

Sends everything gathered, then asks the sink to flush too.

Returns: success when everything buffered reached the sink

Source

Write

pub func Write(
    self: &var BufferedWriter,
    bytes: char8[..]
) -> uint ! IoError

Takes bytes: into the buffer while they fit, around it in one piece when they never would.

NameTypeDescription
byteschar8[..]the bytes to write

Returns: how many bytes were taken, which is all of them

Source

Abandon

pub func Abandon(self: &var BufferedWriter)

Gives the buffer back without sending what it holds. For the caller who has decided the bytes must not go — the one case where losing them is the point.

Source

Destructor

func ~BufferedWriter(self: &var BufferedWriter)

Flushes as a safety net — the outcome has nowhere to go, so a caller who cares flushed already — then gives the buffer back.

Source