aboutsummaryrefslogtreecommitdiff
path: root/embassy-time
diff options
context:
space:
mode:
authorAurĂ©lien Jacobs <[email protected]>2024-06-14 17:42:56 +0200
committerAurĂ©lien Jacobs <[email protected]>2024-06-14 17:46:32 +0200
commit8ec2e193e283657ebe9d29361b2b32ff253633a1 (patch)
treee8a522d7227e614012deeedeccd357cfd4a15863 /embassy-time
parent74739997bd70d3c23b5c58d25aa5c9ba4db55f35 (diff)
implement with_timeout()/with_deadline() method style call on Future
Diffstat (limited to 'embassy-time')
-rw-r--r--embassy-time/src/lib.rs2
-rw-r--r--embassy-time/src/timer.rs30
2 files changed, 31 insertions, 1 deletions
diff --git a/embassy-time/src/lib.rs b/embassy-time/src/lib.rs
index 24ee51be7..8d0648ce5 100644
--- a/embassy-time/src/lib.rs
+++ b/embassy-time/src/lib.rs
@@ -32,7 +32,7 @@ pub use delay::{block_for, Delay};
32pub use duration::Duration; 32pub use duration::Duration;
33pub use embassy_time_driver::TICK_HZ; 33pub use embassy_time_driver::TICK_HZ;
34pub use instant::Instant; 34pub use instant::Instant;
35pub use timer::{with_deadline, with_timeout, Ticker, TimeoutError, Timer}; 35pub use timer::{with_deadline, with_timeout, Ticker, TimeoutError, Timer, WithTimeout};
36 36
37const fn gcd(a: u64, b: u64) -> u64 { 37const fn gcd(a: u64, b: u64) -> u64 {
38 if b == 0 { 38 if b == 0 {
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs
index bc39d8bc7..4d7194b20 100644
--- a/embassy-time/src/timer.rs
+++ b/embassy-time/src/timer.rs
@@ -37,6 +37,36 @@ pub async fn with_deadline<F: Future>(at: Instant, fut: F) -> Result<F::Output,
37 } 37 }
38} 38}
39 39
40/// Provides functions to run a given future with a timeout or a deadline.
41pub trait WithTimeout {
42 /// Output type of the future.
43 type Output;
44
45 /// Runs a given future with a timeout.
46 ///
47 /// If the future completes before the timeout, its output is returned. Otherwise, on timeout,
48 /// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned.
49 async fn with_timeout(self, timeout: Duration) -> Result<Self::Output, TimeoutError>;
50
51 /// Runs a given future with a deadline time.
52 ///
53 /// If the future completes before the deadline, its output is returned. Otherwise, on timeout,
54 /// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned.
55 async fn with_deadline(self, at: Instant) -> Result<Self::Output, TimeoutError>;
56}
57
58impl<F: Future> WithTimeout for F {
59 type Output = F::Output;
60
61 async fn with_timeout(self, timeout: Duration) -> Result<Self::Output, TimeoutError> {
62 with_timeout(timeout, self).await
63 }
64
65 async fn with_deadline(self, at: Instant) -> Result<Self::Output, TimeoutError> {
66 with_deadline(at, self).await
67 }
68}
69
40/// A future that completes at a specified [Instant](struct.Instant.html). 70/// A future that completes at a specified [Instant](struct.Instant.html).
41#[must_use = "futures do nothing unless you `.await` or poll them"] 71#[must_use = "futures do nothing unless you `.await` or poll them"]
42pub struct Timer { 72pub struct Timer {