time/ticker: make sure the future for .next() is Unpin.

This commit is contained in:
Dario Nieuwenhuis 2023-03-05 23:13:22 +01:00
parent 6dfda69cc4
commit c88bbaa5ec

View File

@ -1,9 +1,9 @@
use core::future::Future; use core::future::{poll_fn, Future};
use core::pin::Pin; use core::pin::Pin;
use core::task::{Context, Poll, Waker}; use core::task::{Context, Poll, Waker};
use futures_util::future::{select, Either}; use futures_util::future::{select, Either};
use futures_util::{pin_mut, Stream, StreamExt}; use futures_util::{pin_mut, Stream};
use crate::{Duration, Instant}; use crate::{Duration, Instant};
@ -134,8 +134,17 @@ impl Ticker {
} }
/// Waits for the next tick /// Waits for the next tick
pub async fn next(&mut self) { pub fn next(&mut self) -> impl Future<Output = ()> + '_ {
<Self as StreamExt>::next(self).await; poll_fn(|cx| {
if self.expires_at <= Instant::now() {
let dur = self.duration;
self.expires_at += dur;
Poll::Ready(())
} else {
schedule_wake(self.expires_at, cx.waker());
Poll::Pending
}
})
} }
} }