[−][src]Trait stream_cancel::StreamExt
This Stream
extension trait provides a take_until
method that terminates the stream once
the given future resolves.
Provided methods
fn take_until<U>(self, until: U) -> TakeUntil<Self, U> where
U: Future<Output = bool>,
Self: Sized,
U: Future<Output = bool>,
Self: Sized,
Take elements from this stream until the given future resolves.
This function will take elements from this stream until the given future resolves with
true
. Once it resolves, the stream will yield None
, and produce no further elements.
If the future resolves with false
, the stream will be allowed to continue indefinitely.
use stream_cancel::StreamExt; use futures::prelude::*; use tokio::prelude::*; #[tokio::main] async fn main() { let mut listener = tokio::net::TcpListener::bind("0.0.0.0:0").await.unwrap(); let (tx, rx) = tokio::sync::oneshot::channel(); tokio::spawn(async move { let mut incoming = listener.incoming().take_until(rx.map(|_| true)); while let Some(mut s) = incoming.next().await.transpose().unwrap() { tokio::spawn(async move { let (mut r, mut w) = s.split(); println!("copied {} bytes", tokio::io::copy(&mut r, &mut w).await.unwrap()); }); } }); // tell the listener to stop accepting new connections tx.send(()).unwrap(); // the spawned async block will terminate cleanly, allowing main to return }