diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-03-05 23:13:22 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-03-05 23:13:22 +0100 |
| commit | c88bbaa5ecc340e14fd540e3025a8635456e5405 (patch) | |
| tree | 3e0face57cef071a5bd66019680fabf27aa0359e | |
| parent | 6dfda69cc45dfc259ee03f25342c729a3f3315c8 (diff) | |
time/ticker: make sure the future for .next() is Unpin.
| -rw-r--r-- | embassy-time/src/timer.rs | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 416830a7c..52620d233 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs | |||
| @@ -1,9 +1,9 @@ | |||
| 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 | ||
| 5 | use futures_util::future::{select, Either}; | 5 | use futures_util::future::{select, Either}; |
| 6 | use futures_util::{pin_mut, Stream, StreamExt}; | 6 | use futures_util::{pin_mut, Stream}; |
| 7 | 7 | ||
| 8 | use crate::{Duration, Instant}; | 8 | use crate::{Duration, Instant}; |
| 9 | 9 | ||
| @@ -134,8 +134,17 @@ impl Ticker { | |||
| 134 | } | 134 | } |
| 135 | 135 | ||
| 136 | /// Waits for the next tick | 136 | /// Waits for the next tick |
| 137 | pub async fn next(&mut self) { | 137 | pub fn next(&mut self) -> impl Future<Output = ()> + '_ { |
| 138 | <Self as StreamExt>::next(self).await; | 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 | }) | ||
| 139 | } | 148 | } |
| 140 | } | 149 | } |
| 141 | 150 | ||
