Module remoc::rch::watch

source ·
Available on crate feature rch only.
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§

Enums§

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

Traits§

Functions§

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