|
| 1 | +use core::future::IntoFuture; |
| 2 | + |
1 | 3 | use crate::stream::{IntoStream, Merge};
|
2 | 4 | use futures_core::Stream;
|
3 | 5 |
|
4 | 6 | #[cfg(feature = "alloc")]
|
5 | 7 | use crate::concurrent_stream::FromStream;
|
6 | 8 |
|
7 |
| -use super::{chain::tuple::Chain2, merge::tuple::Merge2, zip::tuple::Zip2, Chain, Zip}; |
| 9 | +use super::{chain::tuple::Chain2, merge::tuple::Merge2, zip::tuple::Zip2, Chain, WaitUntil, Zip}; |
8 | 10 |
|
9 | 11 | /// An extension trait for the `Stream` trait.
|
10 | 12 | pub trait StreamExt: Stream {
|
@@ -34,6 +36,45 @@ pub trait StreamExt: Stream {
|
34 | 36 | {
|
35 | 37 | FromStream::new(self)
|
36 | 38 | }
|
| 39 | + |
| 40 | + /// Delay the yielding of items from the stream until the given deadline. |
| 41 | + /// |
| 42 | + /// The underlying stream will not be polled until the deadline has expired. In addition |
| 43 | + /// to using a time source as a deadline, any future can be used as a |
| 44 | + /// deadline too. When used in combination with a multi-consumer channel, |
| 45 | + /// this method can be used to synchronize the start of multiple streams and futures. |
| 46 | + /// |
| 47 | + /// # Example |
| 48 | + /// ``` |
| 49 | + /// # #[cfg(miri)] fn main() {} |
| 50 | + /// # #[cfg(not(miri))] |
| 51 | + /// # fn main() { |
| 52 | + /// use async_io::Timer; |
| 53 | + /// use futures_concurrency::prelude::*; |
| 54 | + /// use futures_lite::{future::block_on, stream}; |
| 55 | + /// use futures_lite::prelude::*; |
| 56 | + /// use std::time::{Duration, Instant}; |
| 57 | + /// |
| 58 | + /// block_on(async { |
| 59 | + /// let now = Instant::now(); |
| 60 | + /// let duration = Duration::from_millis(100); |
| 61 | + /// |
| 62 | + /// stream::once("meow") |
| 63 | + /// .wait_until(Timer::after(duration)) |
| 64 | + /// .next() |
| 65 | + /// .await; |
| 66 | + /// |
| 67 | + /// assert!(now.elapsed() >= duration); |
| 68 | + /// }); |
| 69 | + /// # } |
| 70 | + /// ``` |
| 71 | + fn wait_until<D>(self, deadline: D) -> WaitUntil<Self, D::IntoFuture> |
| 72 | + where |
| 73 | + Self: Sized, |
| 74 | + D: IntoFuture, |
| 75 | + { |
| 76 | + WaitUntil::new(self, deadline.into_future()) |
| 77 | + } |
37 | 78 | }
|
38 | 79 |
|
39 | 80 | impl<S1> StreamExt for S1
|
|
0 commit comments