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! {
360360 #[ doc = r#"
361361 Waits for both the future and a timeout, if the timeout completes before
362362 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+ ```
363378 "# ]
364379 #[ cfg( any( feature = "unstable" , feature = "docs" ) ) ]
365380 #[ 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 >]
367382 where Self : Sized
368383 {
369384 TimeoutFuture :: new( self , dur)
Original file line number Diff line number Diff line change @@ -51,7 +51,8 @@ pin_project! {
5151}
5252
5353impl < 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 > {
5556 TimeoutFuture { future : future, delay : Delay :: new ( dur) }
5657 }
5758}
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