Module std::channel

Generic MPSC (multi-producer, single-consumer) channels.

Bounded channels for passing typed messages between producers and a single consumer. Sender<T> is cloneable for multi-producer use; Receiver<T> drains the channel.

Both recv() and try_recv() return Option<T>, where None indicates the channel is closed (all senders dropped) or empty (for try_recv only).

Receiving

Supported element types: primitives (i64, f64, bool, char, ...), string, bytes, value records/enums/tuples, and monomorphic machine values (state snapshots — generic machine instantiations are refused). Container elements (Vec<T>, HashMap<K, V>) and handle types are not supported yet.

Examples

import std::channel::channel;

fn main() {
    let (tx, rx) = channel.new(8);
    tx.send("hello");
    tx.close();

    match rx.recv() {
        Some(msg) => println(msg),  // hello
        None => println("closed"),
    }
    rx.close();
}

Contents

Functions

Function new

pub fn new(capacity: i64) -> (Sender, Receiver)

Create a bounded MPSC channel with the given capacity.

Returns a (Sender<T>, Receiver<T>) pair. The sender can be cloned for multi-producer use. The element type T is inferred from usage.

Examples

let (tx, rx) = channel.new(16);
tx.send("hello");       // infers Sender<string>
println(rx.recv());     // infers Receiver<string>

Types

Struct Sender

An opaque sender handle.

Created by channel.new(capacity). Can be cloned for multi-producer use. Must be closed with close() when no longer needed.

The type parameter is managed by the compiler — the underlying runtime handle is type-erased.

Struct Receiver

An opaque receiver handle.

Created by channel.new(capacity). Must be closed with close() when no longer needed.

The type parameter is managed by the compiler — the underlying runtime handle is type-erased.

Struct ChannelPair

Internal paired channel handle.

Traits

Trait SenderMethods

Methods available on a Sender<T>.

Methods

fn send(self: Self, data: string)

Send a message through the channel.

Blocks if the channel is full (backpressure).

fn clone(self: Self) -> Sender

Clone this sender for multi-producer use.

fn close(self: Self)

Close this sender and release resources.

When the last sender is closed the receiver sees channel closure.

Trait ReceiverMethods

Methods available on a Receiver<T>.

Methods

fn recv(self: Self) -> Option<string>

Receive the next message, suspending if necessary.

await rx.recv() from an actor handler / closure / task entry suspends the calling coroutine worker-free until a message arrives or the channel closes; from main/a free function it blocks the foreign thread. Returns Some(value) on a message, or None when the channel is closed (all senders dropped).

fn try_recv(self: Self) -> Option<string>

Try to receive a message without blocking.

Returns Some(value) if a message was available, or None if the channel is empty or closed.

fn close(self: Self)

Close this receiver and release resources.