Module channel

Module channel 

Source
Expand description

Async channels.

The channel can work across UI tasks and parallel tasks, it can be bounded or unbounded and is MPMC.

This module is a thin wrapper around the flume crate’s channel that just limits the API surface to only async methods. You can convert from/into that flume channel.

§Examples

use zng_task::{self as task, channel};

let (sender, receiver) = channel::bounded(5);

task::spawn(async move {
    task::deadline(5.secs()).await;
    if let Err(e) = sender.send("Data!").await {
        eprintln!("no receiver connected, did not send message: '{}'", e.0)
    }
});
task::spawn(async move {
    match receiver.recv().await {
        Ok(msg) => println!("{msg}"),
        Err(_) => eprintln!("no message in channel and no sender connected"),
    }
});

Structs§

Receiver
The receiving end of a channel.
SendError
An error that may be emitted when attempting to send a value into a channel on a sender when all receivers are dropped.
Sender
The transmitting end of a channel.
UnboundSender
The transmitting end of an unbounded channel.

Enums§

RecvError
An error that may be emitted when attempting to wait for a value on a receiver when all senders are dropped and there are no more messages in the channel.
RecvTimeoutError
An error that may be emitted when attempting to wait for a value on a receiver with a timeout when the receive operation times out or all senders are dropped and there are no values left in the channel.
SendTimeoutError
An error that may be emitted when sending a value into a channel on a sender with a timeout when the send operation times out or all receivers are dropped.

Functions§

bounded
Create a channel with a maximum capacity.
rendezvous
Create a bounded channel with 0 capacity.
unbounded
Create a channel with no maximum capacity.