From 1e698af05bc6e7e520d3f35ef661f34ea6ea359e Mon Sep 17 00:00:00 2001 From: Caleb Jamison Date: Wed, 31 Jan 2024 14:04:48 -0500 Subject: Add timeout_at convenience function and example. --- embassy-time/src/timer.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 565a65cb8..dbce9297c 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -8,7 +8,7 @@ use futures_util::{pin_mut, Stream}; use crate::{Duration, Instant}; -/// Error returned by [`with_timeout`] on timeout. +/// Error returned by [`with_timeout`] and [`timeout_at`] on timeout. #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct TimeoutError; @@ -26,6 +26,19 @@ pub async fn with_timeout(timeout: Duration, fut: F) -> Result(at: Instant, fut: F) -> Result { + let timeout_fut = Timer::at(at); + pin_mut!(fut); + match select(fut, timeout_fut).await { + Either::Left((r, _)) => Ok(r), + Either::Right(_) => Err(TimeoutError), + } +} + /// A future that completes at a specified [Instant](struct.Instant.html). #[must_use = "futures do nothing unless you `.await` or poll them"] pub struct Timer { -- cgit From 8b7d85619537fc20ad7ad533433d84ba4975ddc4 Mon Sep 17 00:00:00 2001 From: Caleb Jamison Date: Wed, 31 Jan 2024 16:26:11 -0500 Subject: Rename timeout_at to with_deadline --- embassy-time/src/timer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index dbce9297c..bcd6bf4f7 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -30,7 +30,7 @@ pub async fn with_timeout(timeout: Duration, fut: F) -> Result(at: Instant, fut: F) -> Result { +pub async fn with_deadline(at: Instant, fut: F) -> Result { let timeout_fut = Timer::at(at); pin_mut!(fut); match select(fut, timeout_fut).await { -- cgit From 0ab0b5590a0b07756ceaed9b26c78a6a821b34d5 Mon Sep 17 00:00:00 2001 From: Caleb Jamison Date: Wed, 31 Jan 2024 16:28:06 -0500 Subject: Fixup docs --- embassy-time/src/timer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'embassy-time/src/timer.rs') diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index bcd6bf4f7..daa4c1699 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs @@ -8,7 +8,7 @@ use futures_util::{pin_mut, Stream}; use crate::{Duration, Instant}; -/// Error returned by [`with_timeout`] and [`timeout_at`] on timeout. +/// Error returned by [`with_timeout`] and [`with_deadline`] on timeout. #[derive(Debug, Clone, PartialEq, Eq)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct TimeoutError; -- cgit