Using Sinks
Sinks are the sending-side of channels, sockets, pipes, and so on, in which messages can be sent from the sink asynchronously. Sinks communicate by initiating a send signal, and then the rest is polled. One thing to watch out for when using sinks is that they can run out of sending space, which will prevent more messages from being sent.
How to do it...
- Inside the
bin
folder, create a new file calledsinks.rs
. - Add the following code and run it with
cargo run --bin sinks
:
1 extern crate futures; 2 3 use futures::prelude::*; 4 use futures::future::poll_fn; 5 use futures::executor::block_on; 6 use futures::sink::flush; 7 use futures::stream::iter_ok; 8 use futures::task::{Waker, Context}; 9 10 use std::mem;
- Let's add our examples with using vectors as
sinks
:
12 fn vector_sinks() { 13 let mut vector = Vec::new(); 14 let result = vector.start_send(0); 15 let result2 = vector.start_send(7); 16 17 println!("vector_sink: results of sending should both be...