1 unstable release

0.1.0 Aug 21, 2020

#2138 in Asynchronous

Apache-2.0 OR MIT

16KB
226 lines

async-watch2

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

Extracted from Tokio's tokio::sync::watch implementation, which was initially written by Carl Lerche.

License

async-watch2 is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT for details.


lib.rs:

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

Extracted from Tokio's tokio::sync::watch implementation, which was initially written by Carl Lerche.

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::recv 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::recv will always yield the latest value.

Examples

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

executor.spawn(async move {
    while let Some(value) = rx.recv().await {
        println!("received = {:?}", value);
    }
});

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

Closing

Sender::closed allows the producer to detect when all Receiver 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.

Dependencies

~38KB