futures_lite::stream

Function stop_after_future

Source
pub fn stop_after_future<S, F>(stream: S, future: F) -> StopAfterFuture<S, F>
where S: Sized + Stream, F: Future,
Expand description

Take elements from this stream until the provided future resolves.

This function will take elements from the stream until the provided stopping future fut resolves. Once the fut future becomes ready, this stream combinator will always return that the stream is done.

The stopping future may return any type. Once the stream is stopped the result of the stopping future may be accessed with StopAfterFuture::take_result(). The stream may also be resumed with StopAfterFuture::take_future(). See the documentation of StopAfterFuture for more information.

use futures_lite::stream::{self, StreamExt, stop_after_future};
use futures_lite::future;
use std::task::Poll;

let stream = stream::iter(1..=10);

let mut i = 0;
let stop_fut = future::poll_fn(|_cx| {
    i += 1;
    if i <= 5 {
        Poll::Pending
    } else {
        Poll::Ready(())
    }
});

let stream = stop_after_future(stream, stop_fut);

assert_eq!(vec![1, 2, 3, 4, 5], stream.collect::<Vec<_>>().await);