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.rs30
1 files changed, 30 insertions, 0 deletions
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 {