Module watch

Module watch 

Source
Expand description

A single-producer, multi-consumer remote channel that only retains the last sent value.

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::watch with the additional ability to work over remote connections.

§Alternatives

If your endpoints need the ability to change the value and synchronize the changes with other endpoints, consider using an read/write lock instead.

§Example

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

use remoc::prelude::*;

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

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

    // Intermediate values may be missed.
    while *watch_rx.borrow_and_update().unwrap() != 3 {
        watch_rx.changed().await;
    }
}

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

Structs§

Receiver
Receive values from the associated Sender, which may be located on a remote endpoint.
ReceiverStream
A wrapper around a watch Receiver that implements Stream.
Ref
Returns a reference to the inner value.
Sender
Send values to the associated Receiver, which may be located on a remote endpoint.

Enums§

ChangedError
An error occurred during waiting for a change on a watch channel.
RecvError
An error occurred during receiving over a watch channel.
SendError
An error occurred during sending over an mpsc channel.

Traits§

WatchExt
Extensions for watch channels.

Functions§

channel
Creates a new watch channel, returning the sender and receiver.