Skip to content

Commit

Permalink
Decouple async-std sleep method from clock lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
FSMaxB committed Jan 8, 2025
1 parent 14d227b commit 11741cc
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions async-time-mock-async-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,18 @@ impl MockableClock {
}
}

pub async fn sleep(&self, duration: Duration) -> TimeHandlerGuard {
use MockableClock::*;
match self {
Real => {
sleep(duration).await;
TimeHandlerGuard::Real
pub fn sleep(&self, duration: Duration) -> impl Future<Output = TimeHandlerGuard> + Send + 'static {
let clock = self.clone();
async move {
use MockableClock::*;
match clock {
Real => {
sleep(duration).await;
TimeHandlerGuard::Real
}
#[cfg(feature = "mock")]
Mock(registry) => registry.sleep(duration).await.into(),
}
#[cfg(feature = "mock")]
Mock(registry) => registry.sleep(duration).await.into(),
}
}

Expand All @@ -85,6 +88,8 @@ impl MockableClock {
}
}

// NOTE: Can't currently transform to -> impl Future because whether it needs to implement `Send` or not depends on the future
// that is passed in.
pub async fn timeout<F, T>(&self, duration: Duration, future: F) -> Result<T, TimeoutError>
where
F: Future<Output = T>,
Expand Down

0 comments on commit 11741cc

Please sign in to comment.