Module std::stream

Stream and Sink — bounded, FIFO data channels.

Streams and sinks are language intrinsics for passing typed data between producers and consumers, including across actor boundaries.

Example

import std::stream;

fn main() {
    let (sink, input) = stream.channel(16);

    sink.write("hello");
    sink.write("world");
    sink.close();  // optional — would auto-close on drop

    for await item in input {
        println(item);
    }
}

Contents

Functions

Function channel

pub fn channel(capacity: i64) -> (Sink, Stream)

Create a bounded in-memory channel with the given capacity. Returns a (Sink, Stream) pair for writing and reading.

Function from_file

pub fn from_file(path: String) -> Result<Stream, String>

Open a file as a readable Stream.

Returns Err with the OS error message if the file cannot be opened.

Examples

let file = stream.from_file("data.txt")?;
for await line in file.lines() { println(line); }

Function to_file

pub fn to_file(path: String) -> Result<Sink, String>

Open a file as a writable Sink.

Returns Err with the OS error message if the file cannot be created.

Examples

let sink = stream.to_file("output.txt")?;
sink.write("hello");
// sink auto-closes when dropped

Function pipe

pub fn pipe(source: Stream, dest: Sink)

Pipe all items from a stream into a sink, then close both.

This is the streaming equivalent of io.Copy — it reads from the source until EOF and writes each item to the destination.

Examples

let file = stream.from_file("data.txt");
let body = req.respond_stream(200, "text/plain")?;
stream.pipe(file, body);  // streams file directly to HTTP response

Types

Struct Stream

A readable stream handle.

Struct Sink

A writable sink handle.

Struct StreamPair

Internal paired channel handle.

Traits

Trait StreamMethods

Methods available on a Stream.

Methods

fn next(self: Stream) -> String fn close(self: Stream) fn lines(self: Stream) -> Stream fn chunks(self: Stream, size: i64) -> Stream fn collect(self: Stream) -> String

Trait SinkMethods

Methods available on a Sink.

Methods

fn write(self: Sink, data: String) fn flush(self: Sink) fn close(self: Sink)