@@ -7,6 +7,7 @@ mod instant;
77pub use duration:: Duration ;
88pub use instant:: Instant ;
99
10+ use pin_project_lite:: pin_project;
1011use std:: future:: Future ;
1112use std:: pin:: Pin ;
1213use std:: task:: { Context , Poll } ;
@@ -48,7 +49,7 @@ impl AsyncIterator for Interval {
4849 type Item = Instant ;
4950
5051 async fn next ( & mut self ) -> Option < Self :: Item > {
51- Some ( Timer :: after ( self . duration ) . await )
52+ Some ( Timer :: after ( self . duration ) . wait ( ) . await )
5253 }
5354}
5455
@@ -70,21 +71,33 @@ impl Timer {
7071 pub fn set_after ( & mut self , duration : Duration ) {
7172 * self = Self :: after ( duration) ;
7273 }
73- pub async fn wait ( & self ) -> Instant {
74- match & self . 0 {
75- Some ( pollable) => pollable. wait_for ( ) . await ,
76- None => std:: future:: pending ( ) . await ,
77- }
78- Instant :: now ( )
74+ pub fn wait ( & self ) -> Wait {
75+ let wait_for = self . 0 . as_ref ( ) . map ( |pollable| pollable. wait_for ( ) ) ;
76+ Wait { wait_for }
77+ }
78+ }
79+
80+ pin_project ! {
81+ /// Future created by [`Timer::wait`]
82+ #[ must_use = "futures do nothing unless polled or .awaited" ]
83+ pub struct Wait {
84+ #[ pin]
85+ wait_for: Option <crate :: runtime:: WaitFor >
7986 }
8087}
8188
82- impl Future for Timer {
89+ impl Future for Wait {
8390 type Output = Instant ;
91+
8492 fn poll ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
85- let this = self . as_ref ( ) ;
86- let pinned = std:: pin:: pin!( this. wait( ) ) ;
87- pinned. poll ( cx)
93+ let this = self . project ( ) ;
94+ match this. wait_for . as_pin_mut ( ) {
95+ None => Poll :: Pending ,
96+ Some ( f) => match f. poll ( cx) {
97+ Poll :: Pending => Poll :: Pending ,
98+ Poll :: Ready ( ( ) ) => Poll :: Ready ( Instant :: now ( ) ) ,
99+ } ,
100+ }
88101 }
89102}
90103
@@ -103,14 +116,14 @@ mod test {
103116 #[ test]
104117 fn timer_now ( ) {
105118 crate :: runtime:: block_on ( debug_duration ( "timer_now" , async {
106- Timer :: at ( Instant :: now ( ) ) . await
119+ Timer :: at ( Instant :: now ( ) ) . wait ( ) . await
107120 } ) ) ;
108121 }
109122
110123 #[ test]
111124 fn timer_after_100_milliseconds ( ) {
112125 crate :: runtime:: block_on ( debug_duration ( "timer_after_100_milliseconds" , async {
113- Timer :: after ( Duration :: from_millis ( 100 ) ) . await
126+ Timer :: after ( Duration :: from_millis ( 100 ) ) . wait ( ) . await
114127 } ) ) ;
115128 }
116129}
0 commit comments