@@ -70,11 +70,12 @@ impl Timer {
70
70
pub fn set_after ( & mut self , duration : Duration ) {
71
71
* self = Self :: after ( duration) ;
72
72
}
73
- pub async fn wait ( & self ) {
73
+ pub async fn wait ( & self ) -> Instant {
74
74
match & self . 0 {
75
75
Some ( pollable) => pollable. wait_for ( ) . await ,
76
76
None => std:: future:: pending ( ) . await ,
77
77
}
78
+ Instant :: now ( )
78
79
}
79
80
}
80
81
@@ -83,26 +84,33 @@ impl Future for Timer {
83
84
fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
84
85
let this = self . as_ref ( ) ;
85
86
let pinned = std:: pin:: pin!( this. wait( ) ) ;
86
- match pinned. poll ( cx) {
87
- Poll :: Pending => Poll :: Pending ,
88
- Poll :: Ready ( ( ) ) => Poll :: Ready ( Instant :: now ( ) ) ,
89
- }
87
+ pinned. poll ( cx)
90
88
}
91
89
}
92
90
93
91
#[ cfg( test) ]
94
92
mod test {
95
93
use super :: * ;
96
94
95
+ async fn debug_duration ( what : & str , f : impl Future < Output = Instant > ) {
96
+ let start = Instant :: now ( ) ;
97
+ let now = f. await ;
98
+ let d = now. duration_since ( start) ;
99
+ let d: std:: time:: Duration = d. into ( ) ;
100
+ println ! ( "{what} awaited for {} s" , d. as_secs_f32( ) ) ;
101
+ }
102
+
97
103
#[ test]
98
104
fn timer_now ( ) {
99
- crate :: runtime:: block_on ( async {
100
- let start = Instant :: now ( ) ;
101
- let timer = Timer :: at ( start) ;
102
- let now = timer. await ;
103
- let d = now. duration_since ( start) ;
104
- let d: std:: time:: Duration = d. into ( ) ;
105
- println ! ( "timer_now awaited for {} s" , d. as_secs_f32( ) ) ;
106
- } ) ;
105
+ crate :: runtime:: block_on ( debug_duration ( "timer_now" , async {
106
+ Timer :: at ( Instant :: now ( ) ) . await
107
+ } ) ) ;
108
+ }
109
+
110
+ #[ test]
111
+ fn timer_after_100_milliseconds ( ) {
112
+ crate :: runtime:: block_on ( debug_duration ( "timer_after_100_milliseconds" , async {
113
+ Timer :: after ( Duration :: from_millis ( 100 ) ) . await
114
+ } ) ) ;
107
115
}
108
116
}
0 commit comments