Skip to content

Commit

Permalink
fix(runtime): accept Instance in create_timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Dec 2, 2024
1 parent b382495 commit d31c1a4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
8 changes: 2 additions & 6 deletions compio-runtime/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,8 @@ impl Runtime {
}

#[cfg(feature = "time")]
pub(crate) fn create_timer(&self, delay: std::time::Duration) -> impl Future<Output = ()> {
if delay.is_zero() {
Either::Right(std::future::ready(()))
} else {
Either::Left(TimerFuture::new(std::time::Instant::now() + delay))
}
pub(crate) fn create_timer(&self, instant: std::time::Instant) -> impl Future<Output = ()> {
TimerFuture::new(instant)
}

pub(crate) fn cancel_op<T: OpCode>(&self, op: Key<T>) {
Expand Down
2 changes: 1 addition & 1 deletion compio-runtime/src/runtime/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl TimerRuntime {

pub fn insert(&mut self, instant: Instant) -> Option<usize> {
let delay = instant - self.time;
if delay.is_zero() {
if delay <= self.time.elapsed() {
return None;
}
let key = self.tasks.insert(FutureState::Active(None));
Expand Down
4 changes: 2 additions & 2 deletions compio-runtime/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::Runtime;
/// # })
/// ```
pub async fn sleep(duration: Duration) {
Runtime::with_current(|r| r.create_timer(duration)).await
sleep_until(Instant::now() + duration).await
}

/// Waits until `deadline` is reached.
Expand All @@ -55,7 +55,7 @@ pub async fn sleep(duration: Duration) {
/// # })
/// ```
pub async fn sleep_until(deadline: Instant) {
sleep(deadline - Instant::now()).await
Runtime::with_current(|r| r.create_timer(deadline)).await
}

/// Error returned by [`timeout`] or [`timeout_at`].
Expand Down

0 comments on commit d31c1a4

Please sign in to comment.