2020-12-26 23:44:53 +01:00
|
|
|
use core::future::Future;
|
|
|
|
use core::pin::Pin;
|
2022-08-17 23:40:16 +02:00
|
|
|
use core::task::{Context, Poll, Waker};
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2022-07-29 21:58:35 +02:00
|
|
|
use futures_util::future::{select, Either};
|
|
|
|
use futures_util::{pin_mut, Stream};
|
2020-12-26 23:44:53 +01:00
|
|
|
|
2022-08-17 23:40:16 +02:00
|
|
|
use crate::{Duration, Instant};
|
2020-12-26 23:44:53 +01:00
|
|
|
|
2021-08-24 22:46:07 +02:00
|
|
|
/// Error returned by [`with_timeout`] on timeout.
|
2022-05-03 01:08:34 +02:00
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2021-03-29 22:48:57 +02:00
|
|
|
pub struct TimeoutError;
|
2021-08-24 22:46:07 +02:00
|
|
|
|
|
|
|
/// Runs a given future with a timeout.
|
|
|
|
///
|
|
|
|
/// If the future completes before the timeout, its output is returned. Otherwise, on timeout,
|
|
|
|
/// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned.
|
2021-03-29 22:48:57 +02:00
|
|
|
pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> {
|
|
|
|
let timeout_fut = Timer::after(timeout);
|
|
|
|
pin_mut!(fut);
|
|
|
|
match select(fut, timeout_fut).await {
|
|
|
|
Either::Left((r, _)) => Ok(r),
|
|
|
|
Either::Right(_) => Err(TimeoutError),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:30:03 +01:00
|
|
|
/// A future that completes at a specified [Instant](struct.Instant.html).
|
2020-12-26 23:44:53 +01:00
|
|
|
pub struct Timer {
|
|
|
|
expires_at: Instant,
|
2020-12-30 00:56:32 +01:00
|
|
|
yielded_once: bool,
|
2020-12-26 23:44:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Timer {
|
2021-03-22 02:30:03 +01:00
|
|
|
/// Expire at specified [Instant](struct.Instant.html)
|
2020-12-26 23:44:53 +01:00
|
|
|
pub fn at(expires_at: Instant) -> Self {
|
2020-12-30 00:56:32 +01:00
|
|
|
Self {
|
|
|
|
expires_at,
|
|
|
|
yielded_once: false,
|
|
|
|
}
|
2020-12-26 23:44:53 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:30:03 +01:00
|
|
|
/// Expire after specified [Duration](struct.Duration.html).
|
|
|
|
/// This can be used as a `sleep` abstraction.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// ``` no_run
|
|
|
|
/// # #![feature(type_alias_impl_trait)]
|
|
|
|
/// #
|
|
|
|
/// # fn foo() {}
|
2022-08-17 23:40:16 +02:00
|
|
|
/// use embassy_time::{Duration, Timer};
|
2021-03-22 02:30:03 +01:00
|
|
|
///
|
2022-07-29 21:58:35 +02:00
|
|
|
/// #[embassy_executor::task]
|
2021-03-22 02:30:03 +01:00
|
|
|
/// async fn demo_sleep_seconds() {
|
|
|
|
/// // suspend this task for one second.
|
|
|
|
/// Timer::after(Duration::from_secs(1)).await;
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-12-26 23:44:53 +01:00
|
|
|
pub fn after(duration: Duration) -> Self {
|
|
|
|
Self {
|
|
|
|
expires_at: Instant::now() + duration,
|
2020-12-30 00:56:32 +01:00
|
|
|
yielded_once: false,
|
2020-12-26 23:44:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Unpin for Timer {}
|
|
|
|
|
|
|
|
impl Future for Timer {
|
|
|
|
type Output = ();
|
2020-12-30 00:56:32 +01:00
|
|
|
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
|
|
|
if self.yielded_once && self.expires_at <= Instant::now() {
|
2020-12-26 23:44:53 +01:00
|
|
|
Poll::Ready(())
|
|
|
|
} else {
|
2022-08-17 23:40:16 +02:00
|
|
|
schedule_wake(self.expires_at, cx.waker());
|
2020-12-30 00:56:32 +01:00
|
|
|
self.yielded_once = true;
|
2020-12-26 23:44:53 +01:00
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-22 02:30:03 +01:00
|
|
|
/// Asynchronous stream that yields every Duration, indefinitely.
|
|
|
|
///
|
|
|
|
/// This stream will tick at uniform intervals, even if blocking work is performed between ticks.
|
|
|
|
///
|
|
|
|
/// For instance, consider the following code fragment.
|
|
|
|
/// ``` no_run
|
|
|
|
/// # #![feature(type_alias_impl_trait)]
|
|
|
|
/// #
|
2022-08-17 23:40:16 +02:00
|
|
|
/// use embassy_time::{Duration, Timer};
|
2021-03-22 02:30:03 +01:00
|
|
|
/// # fn foo() {}
|
|
|
|
///
|
2022-07-29 21:58:35 +02:00
|
|
|
/// #[embassy_executor::task]
|
2021-03-22 02:30:03 +01:00
|
|
|
/// async fn ticker_example_0() {
|
|
|
|
/// loop {
|
|
|
|
/// foo();
|
|
|
|
/// Timer::after(Duration::from_secs(1)).await;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// This fragment will not call `foo` every second.
|
|
|
|
/// Instead, it will call it every second + the time it took to previously call `foo`.
|
|
|
|
///
|
|
|
|
/// Example using ticker, which will consistently call `foo` once a second.
|
|
|
|
///
|
|
|
|
/// ``` no_run
|
|
|
|
/// # #![feature(type_alias_impl_trait)]
|
|
|
|
/// #
|
2022-08-17 23:40:16 +02:00
|
|
|
/// use embassy_time::{Duration, Ticker};
|
2021-03-22 02:30:03 +01:00
|
|
|
/// use futures::StreamExt;
|
|
|
|
/// # fn foo(){}
|
|
|
|
///
|
2022-07-29 21:58:35 +02:00
|
|
|
/// #[embassy_executor::task]
|
2021-03-22 02:30:03 +01:00
|
|
|
/// async fn ticker_example_1() {
|
|
|
|
/// let mut ticker = Ticker::every(Duration::from_secs(1));
|
|
|
|
/// loop {
|
|
|
|
/// foo();
|
|
|
|
/// ticker.next().await;
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-12-26 23:44:53 +01:00
|
|
|
pub struct Ticker {
|
|
|
|
expires_at: Instant,
|
|
|
|
duration: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Ticker {
|
2021-03-22 02:30:03 +01:00
|
|
|
/// Creates a new ticker that ticks at the specified duration interval.
|
2020-12-26 23:44:53 +01:00
|
|
|
pub fn every(duration: Duration) -> Self {
|
|
|
|
let expires_at = Instant::now() + duration;
|
2022-06-12 22:15:44 +02:00
|
|
|
Self { expires_at, duration }
|
2020-12-26 23:44:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Unpin for Ticker {}
|
|
|
|
|
|
|
|
impl Stream for Ticker {
|
|
|
|
type Item = ();
|
|
|
|
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
|
|
|
if self.expires_at <= Instant::now() {
|
|
|
|
let dur = self.duration;
|
|
|
|
self.expires_at += dur;
|
|
|
|
Poll::Ready(Some(()))
|
|
|
|
} else {
|
2022-08-17 23:40:16 +02:00
|
|
|
schedule_wake(self.expires_at, cx.waker());
|
2020-12-26 23:44:53 +01:00
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 23:40:16 +02:00
|
|
|
|
|
|
|
extern "Rust" {
|
|
|
|
fn _embassy_time_schedule_wake(at: Instant, waker: &Waker);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn schedule_wake(at: Instant, waker: &Waker) {
|
|
|
|
unsafe { _embassy_time_schedule_wake(at, waker) }
|
|
|
|
}
|