diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-04-26 23:18:28 +0200 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2024-04-26 23:22:25 +0200 |
| commit | 5732ee7ca975c0dd1afa83ade1667a2599d20985 (patch) | |
| tree | f627117733d3fcd5bbb4eb664559c387a8e81f33 /embassy-time/src | |
| parent | 597315873dbef394ae4cefe0af740fcb1bb951e0 (diff) | |
Reduce use of the full `futures` crate.
Diffstat (limited to 'embassy-time/src')
| -rw-r--r-- | embassy-time/src/timer.rs | 10 |
1 files changed, 4 insertions, 6 deletions
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index 757c3ff00..bc39d8bc7 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs | |||
| @@ -1,10 +1,10 @@ | |||
| 1 | use core::future::{poll_fn, Future}; | 1 | use core::future::{poll_fn, Future}; |
| 2 | use core::pin::Pin; | 2 | use core::pin::{pin, Pin}; |
| 3 | use core::task::{Context, Poll}; | 3 | use core::task::{Context, Poll}; |
| 4 | 4 | ||
| 5 | use futures_util::future::{select, Either}; | 5 | use futures_util::future::{select, Either}; |
| 6 | use futures_util::stream::FusedStream; | 6 | use futures_util::stream::FusedStream; |
| 7 | use futures_util::{pin_mut, Stream}; | 7 | use futures_util::Stream; |
| 8 | 8 | ||
| 9 | use crate::{Duration, Instant}; | 9 | use crate::{Duration, Instant}; |
| 10 | 10 | ||
| @@ -19,8 +19,7 @@ pub struct TimeoutError; | |||
| 19 | /// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned. | 19 | /// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned. |
| 20 | pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> { | 20 | pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Output, TimeoutError> { |
| 21 | let timeout_fut = Timer::after(timeout); | 21 | let timeout_fut = Timer::after(timeout); |
| 22 | pin_mut!(fut); | 22 | match select(pin!(fut), timeout_fut).await { |
| 23 | match select(fut, timeout_fut).await { | ||
| 24 | Either::Left((r, _)) => Ok(r), | 23 | Either::Left((r, _)) => Ok(r), |
| 25 | Either::Right(_) => Err(TimeoutError), | 24 | Either::Right(_) => Err(TimeoutError), |
| 26 | } | 25 | } |
| @@ -32,8 +31,7 @@ pub async fn with_timeout<F: Future>(timeout: Duration, fut: F) -> Result<F::Out | |||
| 32 | /// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned. | 31 | /// work on the future is stopped (`poll` is no longer called), the future is dropped and `Err(TimeoutError)` is returned. |
| 33 | pub async fn with_deadline<F: Future>(at: Instant, fut: F) -> Result<F::Output, TimeoutError> { | 32 | pub async fn with_deadline<F: Future>(at: Instant, fut: F) -> Result<F::Output, TimeoutError> { |
| 34 | let timeout_fut = Timer::at(at); | 33 | let timeout_fut = Timer::at(at); |
| 35 | pin_mut!(fut); | 34 | match select(pin!(fut), timeout_fut).await { |
| 36 | match select(fut, timeout_fut).await { | ||
| 37 | Either::Left((r, _)) => Ok(r), | 35 | Either::Left((r, _)) => Ok(r), |
| 38 | Either::Right(_) => Err(TimeoutError), | 36 | Either::Right(_) => Err(TimeoutError), |
| 39 | } | 37 | } |
