Using the oneshot channel
Oneshot channels are useful for when you need to send only one message to a channel. The oneshot channel is applicable for tasks that really only need to be updated/notified once, such as whether or not a recipient has read your message, or as a final destination within a task pipeline to notify the end user that the task has been completed.
How to do it...
- Inside the
bin
folder, create a new file calledoneshot.rs
. - Add the following code and run it with
cargo run --bin oneshot
:
1 extern crate futures; 2 3 use futures::prelude::*; 4 use futures::channel::oneshot::*; 5 use futures::executor::block_on; 6 use futures::future::poll_fn; 7 use futures::stream::futures_ordered; 8 9 const FINISHED: Result<Async<()>, Never> = Ok(Async::Ready(())); 10 11 fn send_example() { 12 // First, we'll need to initiate some oneshot channels like so: 13 let (tx_1, rx_1) = channel::(); 14 let (tx_2, rx_2) = channel::(); 15 let (tx_3...