diff options
Diffstat (limited to 'embassy-time/src/timer.rs')
| -rw-r--r-- | embassy-time/src/timer.rs | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index bd791b817..52620d233 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | use core::future::Future; | 1 | use core::future::{poll_fn, Future}; |
| 2 | use core::pin::Pin; | 2 | use core::pin::Pin; |
| 3 | use core::task::{Context, Poll, Waker}; | 3 | use core::task::{Context, Poll, Waker}; |
| 4 | 4 | ||
| @@ -26,6 +26,7 @@ pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Out | |||
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | /// A future that completes at a specified [Instant](struct.Instant.html). | 28 | /// A future that completes at a specified [Instant](struct.Instant.html). |
| 29 | #[must_use = "futures do nothing unless you `.await` or poll them"] | ||
| 29 | pub struct Timer { | 30 | pub struct Timer { |
| 30 | expires_at: Instant, | 31 | expires_at: Instant, |
| 31 | yielded_once: bool, | 32 | yielded_once: bool, |
| @@ -131,6 +132,20 @@ impl Ticker { | |||
| 131 | let expires_at = Instant::now() + duration; | 132 | let expires_at = Instant::now() + duration; |
| 132 | Self { expires_at, duration } | 133 | Self { expires_at, duration } |
| 133 | } | 134 | } |
| 135 | |||
| 136 | /// Waits for the next tick | ||
| 137 | pub fn next(&mut self) -> impl Future<Output = ()> + '_ { | ||
| 138 | poll_fn(|cx| { | ||
| 139 | if self.expires_at <= Instant::now() { | ||
| 140 | let dur = self.duration; | ||
| 141 | self.expires_at += dur; | ||
| 142 | Poll::Ready(()) | ||
| 143 | } else { | ||
| 144 | schedule_wake(self.expires_at, cx.waker()); | ||
| 145 | Poll::Pending | ||
| 146 | } | ||
| 147 | }) | ||
| 148 | } | ||
| 134 | } | 149 | } |
| 135 | 150 | ||
| 136 | impl Unpin for Ticker {} | 151 | impl Unpin for Ticker {} |
