Module tokio_sync::watch[][src]

Expand description

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

This channel is useful for watching for changes to a value from multiple points in the code base, for example, changes to configuration values.

Usage

channel returns a Sender / Receiver pair. These are the producer and sender halves of the channel. The channel is created with an initial value. Receiver::poll will always be ready upon creation and will yield either this initial value or the latest value that has been sent by Sender.

Calls to Receiver::poll and Receiver::poll_ref will always yield the latest value.

Examples

extern crate tokio;

use tokio::prelude::*;
use tokio::sync::watch;

let (mut tx, rx) = watch::channel("hello");

tokio::spawn(rx.for_each(|value| {
    println!("received = {:?}", value);
    Ok(())
}).map_err(|_| ()));

tx.broadcast("world").unwrap();

Closing

Sender::poll_close allows the producer to detect when all Sender handles have been dropped. This indicates that there is no further interest in the values being produced and work can be stopped.

Thread safety

Both Sender and Receiver are thread safe. They can be moved to other threads and can be used in a concurrent environment. Clones of Receiver handles may be moved to separate threads and also used concurrently.

Modules

error

Watch error types

Structs

Receiver

Receives values from the associated Sender.

Ref

Returns a reference to the inner value

Sender

Sends values to the associated Receiver.

Functions

channel

Create a new watch channel, returning the “send” and “receive” handles.