diff options
| author | Ulf Lilleengen <[email protected]> | 2024-06-22 14:14:35 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-06-22 14:14:35 +0000 |
| commit | 95d0cae897cc3557b0df4ea8cb73efb3a272b450 (patch) | |
| tree | 808204a65f7666f55587f4e1f7b63e64c889d3fb /embassy-time/src | |
| parent | 8b0c883443e975e6c43a964ed031da056f8b37ea (diff) | |
| parent | 8ec2e193e283657ebe9d29361b2b32ff253633a1 (diff) | |
Merge pull request #3079 from aurelj/with_timeout
implement with_timeout()/with_deadline() method style call on Future
Diffstat (limited to 'embassy-time/src')
| -rw-r--r-- | embassy-time/src/lib.rs | 2 | ||||
| -rw-r--r-- | embassy-time/src/timer.rs | 30 |
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}; | |||
| 32 | pub use duration::Duration; | 32 | pub use duration::Duration; |
| 33 | pub use embassy_time_driver::TICK_HZ; | 33 | pub use embassy_time_driver::TICK_HZ; |
| 34 | pub use instant::Instant; | 34 | pub use instant::Instant; |
| 35 | pub use timer::{with_deadline, with_timeout, Ticker, TimeoutError, Timer}; | 35 | pub use timer::{with_deadline, with_timeout, Ticker, TimeoutError, Timer, WithTimeout}; |
| 36 | 36 | ||
| 37 | const fn gcd(a: u64, b: u64) -> u64 { | 37 | const 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. | ||
| 41 | pub 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 | |||
| 58 | impl<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"] |
| 42 | pub struct Timer { | 72 | pub struct Timer { |
