From 1c1c168e1b55e01f932a38bf0629ec7467bf6162 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Thu, 18 Jun 2020 12:37:14 +0200 Subject: [PATCH] fix(timer): ensure the runtime is working for timers --- src/future/future/delay.rs | 4 ++-- src/future/timeout.rs | 4 ++-- src/io/timeout.rs | 4 ++-- src/stream/interval.rs | 6 +++--- src/stream/stream/delay.rs | 4 ++-- src/stream/stream/throttle.rs | 6 +++--- src/stream/stream/timeout.rs | 4 ++-- src/utils.rs | 7 +++++++ 8 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/future/future/delay.rs b/src/future/future/delay.rs index b6c30bc..092639d 100644 --- a/src/future/future/delay.rs +++ b/src/future/future/delay.rs @@ -5,7 +5,7 @@ use std::time::Duration; use pin_project_lite::pin_project; use crate::task::{Context, Poll}; -use crate::utils::Timer; +use crate::utils::{timer_after, Timer}; pin_project! { #[doc(hidden)] @@ -20,7 +20,7 @@ pin_project! { impl DelayFuture { pub fn new(future: F, dur: Duration) -> DelayFuture { - let delay = Timer::after(dur); + let delay = timer_after(dur); DelayFuture { future, delay } } diff --git a/src/future/timeout.rs b/src/future/timeout.rs index 4a9d93c..3846621 100644 --- a/src/future/timeout.rs +++ b/src/future/timeout.rs @@ -7,7 +7,7 @@ use std::time::Duration; use pin_project_lite::pin_project; use crate::task::{Context, Poll}; -use crate::utils::Timer; +use crate::utils::{timer_after, Timer}; /// Awaits a future or times out after a duration of time. /// @@ -51,7 +51,7 @@ impl TimeoutFuture { pub(super) fn new(future: F, dur: Duration) -> TimeoutFuture { TimeoutFuture { future, - delay: Timer::after(dur), + delay: timer_after(dur), } } } diff --git a/src/io/timeout.rs b/src/io/timeout.rs index ce33fea..073c2f6 100644 --- a/src/io/timeout.rs +++ b/src/io/timeout.rs @@ -6,7 +6,7 @@ use std::time::Duration; use pin_project_lite::pin_project; use crate::io; -use crate::utils::Timer; +use crate::utils::{timer_after, Timer}; /// Awaits an I/O future or times out after a duration of time. /// @@ -37,7 +37,7 @@ where F: Future>, { Timeout { - timeout: Timer::after(dur), + timeout: timer_after(dur), future: f, } .await diff --git a/src/stream/interval.rs b/src/stream/interval.rs index 4e5c92b..0a7eb48 100644 --- a/src/stream/interval.rs +++ b/src/stream/interval.rs @@ -4,7 +4,7 @@ use std::task::{Context, Poll}; use std::time::Duration; use crate::stream::Stream; -use crate::utils::Timer; +use crate::utils::{timer_after, Timer}; /// Creates a new stream that yields at a set interval. /// @@ -45,7 +45,7 @@ use crate::utils::Timer; #[cfg_attr(feature = "docs", doc(cfg(unstable)))] pub fn interval(dur: Duration) -> Interval { Interval { - delay: Timer::after(dur), + delay: timer_after(dur), interval: dur, } } @@ -72,7 +72,7 @@ impl Stream for Interval { return Poll::Pending; } let interval = self.interval; - let _ = std::mem::replace(&mut self.delay, Timer::after(interval)); + let _ = std::mem::replace(&mut self.delay, timer_after(interval)); Poll::Ready(Some(())) } } diff --git a/src/stream/stream/delay.rs b/src/stream/stream/delay.rs index 0ba42b0..9a7f947 100644 --- a/src/stream/stream/delay.rs +++ b/src/stream/stream/delay.rs @@ -6,7 +6,7 @@ use pin_project_lite::pin_project; use crate::stream::Stream; use crate::task::{Context, Poll}; -use crate::utils::Timer; +use crate::utils::{timer_after, Timer}; pin_project! { #[doc(hidden)] @@ -24,7 +24,7 @@ impl Delay { pub(super) fn new(stream: S, dur: Duration) -> Self { Delay { stream, - delay: Timer::after(dur), + delay: timer_after(dur), delay_done: false, } } diff --git a/src/stream/stream/throttle.rs b/src/stream/stream/throttle.rs index 2f9333a..d0e2cdd 100644 --- a/src/stream/stream/throttle.rs +++ b/src/stream/stream/throttle.rs @@ -6,7 +6,7 @@ use pin_project_lite::pin_project; use crate::stream::Stream; use crate::task::{Context, Poll}; -use crate::utils::Timer; +use crate::utils::{timer_after, Timer}; pin_project! { /// A stream that only yields one element once every `duration`. @@ -35,7 +35,7 @@ impl Throttle { stream, duration, blocked: false, - delay: Timer::after(Duration::default()), + delay: timer_after(Duration::default()), } } } @@ -59,7 +59,7 @@ impl Stream for Throttle { Poll::Ready(None) => Poll::Ready(None), Poll::Ready(Some(v)) => { *this.blocked = true; - let _ = std::mem::replace(&mut *this.delay, Timer::after(*this.duration)); + let _ = std::mem::replace(&mut *this.delay, timer_after(*this.duration)); Poll::Ready(Some(v)) } } diff --git a/src/stream/stream/timeout.rs b/src/stream/stream/timeout.rs index 28e52ae..0e0ee91 100644 --- a/src/stream/stream/timeout.rs +++ b/src/stream/stream/timeout.rs @@ -8,7 +8,7 @@ use pin_project_lite::pin_project; use crate::stream::Stream; use crate::task::{Context, Poll}; -use crate::utils::Timer; +use crate::utils::{timer_after, Timer}; pin_project! { /// A stream with timeout time set @@ -23,7 +23,7 @@ pin_project! { impl Timeout { pub(crate) fn new(stream: S, dur: Duration) -> Self { - let delay = Timer::after(dur); + let delay = timer_after(dur); Self { stream, delay } } diff --git a/src/utils.rs b/src/utils.rs index e064570..3ca9d15 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -64,6 +64,13 @@ mod timer { pub type Timer = smol::Timer; } +pub(crate) fn timer_after(dur: std::time::Duration) -> timer::Timer { + #[cfg(not(target_os = "unknown"))] + once_cell::sync::Lazy::force(&crate::rt::RUNTIME); + + Timer::after(dur) +} + #[cfg(any( all(target_arch = "wasm32", feature = "default"), all(feature = "unstable", not(feature = "default"))