@@ -73,7 +73,7 @@ pub fn make_instant_from_now(dur: Duration) -> Instant {
73
73
}
74
74
75
75
/// A trait for creating delay futures.
76
- pub trait MakeDelay : Send + ' static {
76
+ pub trait MakeDelay {
77
77
/// The future returned by the `delay`/`delay_until` method.
78
78
type Delay : Future < Output = ( ) > + Send ;
79
79
@@ -92,6 +92,41 @@ pub trait Spawn {
92
92
fn spawn < F : Future < Output = ( ) > + Send + ' static > ( & self , future : F ) ;
93
93
}
94
94
95
+ /// Provides extension methods for [`MakeDelay`] implementors.
96
+ pub trait MakeDelayExt : MakeDelay {
97
+ /// Requires a `Future` to complete before the specified duration has elapsed.
98
+ fn timeout < F : Future > ( & self , duration : Duration , fut : F ) -> Timeout < F , Self :: Delay > {
99
+ timeout ( duration, fut, self )
100
+ }
101
+
102
+ /// Requires a `Future` to complete before the specified instant in time.
103
+ fn timeout_at < F : Future > ( & self , deadline : Instant , fut : F ) -> Timeout < F , Self :: Delay > {
104
+ timeout_at ( deadline, fut, self )
105
+ }
106
+
107
+ /// Creates new [`Interval`] that yields with interval of `period`.
108
+ ///
109
+ /// See [`interval`] for more details.
110
+ fn interval ( self , period : Duration ) -> Interval < Self >
111
+ where
112
+ Self : Sized ,
113
+ {
114
+ interval ( period, self )
115
+ }
116
+
117
+ /// Creates new [`Interval`] that yields with interval of `period` and starts at `at`.
118
+ ///
119
+ /// See [`interval_at`] for more details.
120
+ fn interval_at ( self , at : Instant , period : Duration ) -> Interval < Self >
121
+ where
122
+ Self : Sized ,
123
+ {
124
+ interval_at ( at, period, self )
125
+ }
126
+ }
127
+
128
+ impl < T : MakeDelay > MakeDelayExt for T { }
129
+
95
130
/// Errors returned by [`Timeout`].
96
131
///
97
132
/// This error is returned when a timeout expires before the function was able
@@ -154,11 +189,11 @@ where
154
189
pub fn timeout < F , D > (
155
190
duration : Duration ,
156
191
future : F ,
157
- make_delay : D ,
192
+ make_delay : & D ,
158
193
) -> Timeout < F :: IntoFuture , D :: Delay >
159
194
where
160
195
F : IntoFuture ,
161
- D : MakeDelay ,
196
+ D : MakeDelay + ? Sized ,
162
197
{
163
198
let delay = make_delay. delay ( duration) ;
164
199
Timeout {
@@ -171,11 +206,11 @@ where
171
206
pub fn timeout_at < F , D > (
172
207
deadline : Instant ,
173
208
future : F ,
174
- make_delay : D ,
209
+ make_delay : & D ,
175
210
) -> Timeout < F :: IntoFuture , D :: Delay >
176
211
where
177
212
F : IntoFuture ,
178
- D : MakeDelay ,
213
+ D : MakeDelay + ? Sized ,
179
214
{
180
215
let delay = make_delay. delay_util ( deadline) ;
181
216
Timeout {
0 commit comments