Module mpsc

Module mpsc 

Source
Expand description

Multi producer single customer remote channel.

The sender and receiver can both be sent to remote endpoints. The channel also works if both halves are local. Forwarding over multiple connections is supported.

This has similar functionality as tokio::sync::mpsc with the additional ability to work over remote connections.

§Example

In the following example the client sends a number and an MPSC channel sender to the server. The server counts to the number and sends each value to the client over the MPSC channel.

use remoc::prelude::*;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct CountReq {
    up_to: u32,
    seq_tx: rch::mpsc::Sender<u32>,
}

// This would be run on the client.
async fn client(mut tx: rch::base::Sender<CountReq>) {
    let (seq_tx, mut seq_rx) = rch::mpsc::channel(1);
    tx.send(CountReq { up_to: 4, seq_tx }).await.unwrap();

    assert_eq!(seq_rx.recv().await.unwrap(), Some(0));
    assert_eq!(seq_rx.recv().await.unwrap(), Some(1));
    assert_eq!(seq_rx.recv().await.unwrap(), Some(2));
    assert_eq!(seq_rx.recv().await.unwrap(), Some(3));
    assert_eq!(seq_rx.recv().await.unwrap(), None);
}

// This would be run on the server.
async fn server(mut rx: rch::base::Receiver<CountReq>) {
    while let Some(CountReq { up_to, seq_tx }) = rx.recv().await.unwrap() {
        for i in 0..up_to {
            seq_tx.send(i).await.unwrap();
        }
    }
}

Structs§

DistributedReceiverHandle
A handle to a receiver that receives its values from a distributor.
Distributor
Distributes items of an mpsc channel over multiple receivers.
Permit
Owned permit to send one value into the channel.
Receiver
Receive values from the associated Sender, which may be located on a remote endpoint.
Sender
Send values to the associated Receiver, which may be located on a remote endpoint.

Enums§

RecvError
An error occurred during receiving over an mpsc channel.
SendError
An error occurred during sending over an mpsc channel.
TryRecvError
An error occurred during trying to receive over an mpsc channel.
TrySendError
An error occurred during trying to send over an mpsc channel.

Traits§

MpscExt
Extensions for MPSC channels.

Functions§

channel
Creates a bounded channel for communicating between asynchronous tasks with back pressure.