aboutsummaryrefslogtreecommitdiff
path: root/embassy-time/src/timer.rs
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-time/src/timer.rs')
-rw-r--r--embassy-time/src/timer.rs18
1 files changed, 16 insertions, 2 deletions
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs
index bd791b817..d3d1f9f5f 100644
--- a/embassy-time/src/timer.rs
+++ b/embassy-time/src/timer.rs
@@ -1,4 +1,4 @@
1use core::future::Future; 1use core::future::{poll_fn, Future};
2use core::pin::Pin; 2use core::pin::Pin;
3use core::task::{Context, Poll, Waker}; 3use 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"]
29pub struct Timer { 30pub struct Timer {
30 expires_at: Instant, 31 expires_at: Instant,
31 yielded_once: bool, 32 yielded_once: bool,
@@ -108,7 +109,6 @@ impl Future for Timer {
108/// # #![feature(type_alias_impl_trait)] 109/// # #![feature(type_alias_impl_trait)]
109/// # 110/// #
110/// use embassy_time::{Duration, Ticker}; 111/// use embassy_time::{Duration, Ticker};
111/// use futures::StreamExt;
112/// # fn foo(){} 112/// # fn foo(){}
113/// 113///
114/// #[embassy_executor::task] 114/// #[embassy_executor::task]
@@ -131,6 +131,20 @@ impl Ticker {
131 let expires_at = Instant::now() + duration; 131 let expires_at = Instant::now() + duration;
132 Self { expires_at, duration } 132 Self { expires_at, duration }
133 } 133 }
134
135 /// Waits for the next tick
136 pub fn next(&mut self) -> impl Future<Output = ()> + '_ {
137 poll_fn(|cx| {
138 if self.expires_at <= Instant::now() {
139 let dur = self.duration;
140 self.expires_at += dur;
141 Poll::Ready(())
142 } else {
143 schedule_wake(self.expires_at, cx.waker());
144 Poll::Pending
145 }
146 })
147 }
134} 148}
135 149
136impl Unpin for Ticker {} 150impl Unpin for Ticker {}