File tree Expand file tree Collapse file tree 3 files changed +45
-2
lines changed Expand file tree Collapse file tree 3 files changed +45
-2
lines changed Original file line number Diff line number Diff line change @@ -360,10 +360,25 @@ extension_trait! {
360
360
#[ doc = r#"
361
361
Waits for both the future and a timeout, if the timeout completes before
362
362
the future, it returns an TimeoutError.
363
+
364
+ # Example
365
+ ```
366
+ #async_std::task::block_on(async {
367
+ let fut = future::ready(0);
368
+ let dur = Duration::from_millis(100);
369
+ let res = fut.timeout(dur).await;
370
+ assert!(res.is_ok());
371
+
372
+ let fut = future::ready(0);
373
+ let dur = Duration::from_millis(100);
374
+ let res = fut.timeout(dur).await;
375
+ assert!(res.is_ok())
376
+ # });
377
+ ```
363
378
"# ]
364
379
#[ cfg( any( feature = "unstable" , feature = "docs" ) ) ]
365
380
#[ cfg_attr( feature = "docs" , doc( cfg( unstable) ) ) ]
366
- fn timeout< F , T > ( self , dur: Duration ) -> impl Future <Output = Self :: Output > [ TimeoutFuture <Self >]
381
+ fn timeout( self , dur: Duration ) -> impl Future <Output = Self :: Output > [ TimeoutFuture <Self >]
367
382
where Self : Sized
368
383
{
369
384
TimeoutFuture :: new( self , dur)
Original file line number Diff line number Diff line change @@ -51,7 +51,8 @@ pin_project! {
51
51
}
52
52
53
53
impl < F > TimeoutFuture < F > {
54
- pub fn new ( future : F , dur : Duration ) -> TimeoutFuture < F > {
54
+ #[ allow( dead_code) ]
55
+ pub ( super ) fn new ( future : F , dur : Duration ) -> TimeoutFuture < F > {
55
56
TimeoutFuture { future : future, delay : Delay :: new ( dur) }
56
57
}
57
58
}
Original file line number Diff line number Diff line change
1
+ #![ cfg( feature = "unstable" ) ]
2
+
3
+ use std:: time:: Duration ;
4
+
5
+ use async_std:: future;
6
+ use async_std:: prelude:: * ;
7
+ use async_std:: task;
8
+
9
+ #[ test]
10
+ fn should_timeout ( ) {
11
+ task:: block_on ( async {
12
+ let fut = future:: pending :: < ( ) > ( ) ;
13
+ let dur = Duration :: from_millis ( 100 ) ;
14
+ let res = fut. timeout ( dur) . await ;
15
+ assert ! ( res. is_err( ) ) ;
16
+ } ) ;
17
+ }
18
+
19
+ #[ test]
20
+ fn should_not_timeout ( ) {
21
+ task:: block_on ( async {
22
+ let fut = future:: ready ( 0 ) ;
23
+ let dur = Duration :: from_millis ( 100 ) ;
24
+ let res = fut. timeout ( dur) . await ;
25
+ assert ! ( res. is_ok( ) ) ;
26
+ } ) ;
27
+ }
You can’t perform that action at this time.
0 commit comments