@@ -70,11 +70,12 @@ impl Timer {
7070 pub fn set_after ( & mut self , duration : Duration ) {
7171 * self = Self :: after ( duration) ;
7272 }
73- pub async fn wait ( & self ) {
73+ pub async fn wait ( & self ) -> Instant {
7474 match & self . 0 {
7575 Some ( pollable) => pollable. wait_for ( ) . await ,
7676 None => std:: future:: pending ( ) . await ,
7777 }
78+ Instant :: now ( )
7879 }
7980}
8081
@@ -83,26 +84,33 @@ impl Future for Timer {
8384 fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
8485 let this = self . as_ref ( ) ;
8586 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)
9088 }
9189}
9290
9391#[ cfg( test) ]
9492mod test {
9593 use super :: * ;
9694
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+
97103 #[ test]
98104 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+ } ) ) ;
107115 }
108116}
0 commit comments