std::channelGeneric 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).
await rx.recv() from an actor handler / closure / task entry
SUSPENDS the calling coroutine worker-free when the channel is empty
with live senders, and resumes when a sender deposits a value or the
last sender closes (None). From a plain main/free function it
blocks the foreign thread instead (there is no parkable continuation).rx.try_recv() never suspends: it returns Some(value) if an item is
immediately available, else None.select { pat from rx.recv() => ... }.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.
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();
}
newCreate 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.
let (tx, rx) = channel.new(16);
tx.send("hello"); // infers Sender<string>
println(rx.recv()); // infers Receiver<string>
SenderAn 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.
ReceiverAn 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.
ChannelPairInternal paired channel handle.
SenderMethodsMethods available on a Sender<T>.
Send a message through the channel.
Blocks if the channel is full (backpressure).
Clone this sender for multi-producer use.
Close this sender and release resources.
When the last sender is closed the receiver sees channel closure.
ReceiverMethodsMethods available on a Receiver<T>.
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).
Try to receive a message without blocking.
Returns Some(value) if a message was available, or None if
the channel is empty or closed.
Close this receiver and release resources.