@@ -5,6 +5,7 @@ use futures_core::Future;
5
5
6
6
use super :: join:: tuple:: Join2 ;
7
7
use super :: race:: tuple:: Race2 ;
8
+ use super :: WaitUntil ;
8
9
9
10
/// An extension trait for the `Future` trait.
10
11
pub trait FutureExt : Future {
@@ -19,6 +20,40 @@ pub trait FutureExt: Future {
19
20
where
20
21
Self : Future < Output = T > + Sized ,
21
22
S2 : IntoFuture < Output = T > ;
23
+
24
+ /// Delay resolving the future until the given deadline.
25
+ ///
26
+ /// The underlying future will not be polled until the deadline has expired. In addition
27
+ /// to using a time source as a deadline, any future can be used as a
28
+ /// deadline too. When used in combination with a multi-consumer channel,
29
+ /// this method can be used to synchronize the start of multiple futures and streams.
30
+ ///
31
+ /// # Example
32
+ ///
33
+ /// ```
34
+ /// use async_io::Timer;
35
+ /// use futures_concurrency::prelude::*;
36
+ /// use futures_lite::future::block_on;
37
+ /// use std::time::{Duration, Instant};
38
+ ///
39
+ /// block_on(async {
40
+ /// let now = Instant::now();
41
+ /// let duration = Duration::from_millis(100);
42
+ ///
43
+ /// async { "meow" }
44
+ /// .wait_until(Timer::after(duration))
45
+ /// .await;
46
+ ///
47
+ /// assert!(now.elapsed() >= duration);
48
+ /// });
49
+ /// ```
50
+ fn wait_until < D > ( self , deadline : D ) -> WaitUntil < Self , D :: IntoFuture >
51
+ where
52
+ Self : Sized ,
53
+ D : IntoFuture ,
54
+ {
55
+ WaitUntil :: new ( self , deadline. into_future ( ) )
56
+ }
22
57
}
23
58
24
59
impl < F1 > FutureExt for F1
0 commit comments