File tree 2 files changed +20
-1
lines changed
2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ cfg_unstable! {
15
15
use try_race:: TryRace ;
16
16
use join:: Join ;
17
17
use try_join:: TryJoin ;
18
+ use crate :: future:: timeout:: TimeoutFuture ;
18
19
}
19
20
20
21
extension_trait ! {
@@ -355,6 +356,18 @@ extension_trait! {
355
356
{
356
357
TryJoin :: new( self , other)
357
358
}
359
+
360
+ #[ doc = r#"
361
+ Waits for both the future and a timeout, if the timeout completes before
362
+ the future, it returns an TimeoutError.
363
+ "# ]
364
+ #[ cfg( any( feature = "unstable" , feature = "docs" ) ) ]
365
+ #[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
366
+ fn timeout<F , T >( self , dur: Duration ) -> impl Future <Output = Self :: Output > [ TimeoutFuture <Self >]
367
+ where Self : Sized
368
+ {
369
+ TimeoutFuture :: new( self , dur)
370
+ }
358
371
}
359
372
360
373
impl <F : Future + Unpin + ?Sized > Future for Box <F > {
Original file line number Diff line number Diff line change @@ -42,14 +42,20 @@ where
42
42
43
43
pin_project ! {
44
44
/// A future that times out after a duration of time.
45
- struct TimeoutFuture <F > {
45
+ pub struct TimeoutFuture <F > {
46
46
#[ pin]
47
47
future: F ,
48
48
#[ pin]
49
49
delay: Delay ,
50
50
}
51
51
}
52
52
53
+ impl < F > TimeoutFuture < F > {
54
+ pub fn new ( future : F , dur : Duration ) -> TimeoutFuture < F > {
55
+ TimeoutFuture { future : future, delay : Delay :: new ( dur) }
56
+ }
57
+ }
58
+
53
59
impl < F : Future > Future for TimeoutFuture < F > {
54
60
type Output = Result < F :: Output , TimeoutError > ;
55
61
You can’t perform that action at this time.
0 commit comments