From d350da85126ccd7d8283feeea2a94591a17decd4 Mon Sep 17 00:00:00 2001 From: NumberFour8 Date: Wed, 25 Dec 2024 20:58:24 +0100 Subject: [PATCH] Add optional futures-timer based Sleeper --- backon/Cargo.toml | 5 ++++- backon/src/lib.rs | 11 ++++++----- backon/src/sleep.rs | 33 ++++++++++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/backon/Cargo.toml b/backon/Cargo.toml index d6bd83e..b5b484d 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -20,20 +20,23 @@ targets = [ ] [features] -default = ["std", "std-blocking-sleep", "tokio-sleep", "gloo-timers-sleep"] +default = ["std", "std-blocking-sleep", "tokio-sleep", "gloo-timers-sleep", "futures-timer-sleep"] std = ["fastrand/std"] std-blocking-sleep = [] gloo-timers-sleep = ["gloo-timers/futures"] tokio-sleep = ["tokio/time"] +futures-timer-sleep = ["futures-timer"] [dependencies] fastrand = { version = "2", default-features = false } [target.'cfg(not(target_arch = "wasm32"))'.dependencies] tokio = { version = "1", optional = true } +futures-timer = { version = "3.0.3", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] gloo-timers = { version = "0.3", optional = true } +futures-timer = { version = "3.0.3", optional = true, features = ["gloo-timers"]} [dev-dependencies] anyhow = "1" diff --git a/backon/src/lib.rs b/backon/src/lib.rs index d6e4366..424e5f1 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -41,11 +41,12 @@ //! environments, they are gated under their own features, which are enabled //! by default: //! -//! | `Sleeper` | feature | Environment | Asynchronous | -//! |---------------------|--------------------|-------------|---------------| -//! | [`TokioSleeper`] | tokio-sleep | non-wasm32 | Yes | -//! | [`GlooTimersSleep`] | gloo-timers-sleep | wasm32 | Yes | -//! | [`StdSleeper`] | std-blocking-sleep | std | No | +//! | `Sleeper` | feature | Environment | Asynchronous | +//! |----------------------|--------------------|-------------|---------------| +//! | [`TokioSleeper`] | tokio-sleep | non-wasm32 | Yes | +//! | [`GlooTimersSleep`] | gloo-timers-sleep | wasm32 | Yes | +//! | [`FutureTimerSleep`] | future-timer-sleep | both | Yes | +//! | [`StdSleeper`] | std-blocking-sleep | std | No | //! //! ## Custom Sleeper //! diff --git a/backon/src/sleep.rs b/backon/src/sleep.rs index 71dab5c..88ee405 100644 --- a/backon/src/sleep.rs +++ b/backon/src/sleep.rs @@ -36,7 +36,11 @@ impl Fut + 'static, Fut: Future> Sleeper for F { /// The default implementation of `Sleeper` when no features are enabled. /// /// It will fail to compile if a containing [`Retry`][crate::Retry] is `.await`ed without calling [`Retry::sleep`][crate::Retry::sleep] to provide a valid sleeper. -#[cfg(all(not(feature = "tokio-sleep"), not(feature = "gloo-timers-sleep"),))] +#[cfg(all( + not(feature = "tokio-sleep"), + not(feature = "gloo-timers-sleep"), + not(feature = "futures-timer-sleep") +))] pub type DefaultSleeper = PleaseEnableAFeatureOrProvideACustomSleeper; /// The default implementation of `Sleeper` while feature `tokio-sleep` enabled. /// @@ -48,6 +52,15 @@ pub type DefaultSleeper = TokioSleeper; /// It uses `gloo_timers::sleep::sleep`. #[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))] pub type DefaultSleeper = GlooTimersSleep; +/// The default implementation of `Sleeper` while only the feature `futures-timer-sleep` is enabled. +/// +/// It uses `futures_timer::Delay`. +#[cfg(all( + not(feature = "tokio-sleep"), + not(feature = "gloo-timers-sleep"), + feature = "futures-timer-sleep" +))] +pub type DefaultSleeper = FuturesTimerSleep; /// A placeholder type that does not implement [`Sleeper`] and will therefore fail to compile if used as one. /// @@ -78,6 +91,24 @@ impl Sleeper for TokioSleeper { } } +/// The implementation of `Sleeper` that uses `futures_timer::Delay`. +/// +/// This implementation is based on +/// the [`futures-timer`](https://docs.rs/futures-timer/latest/futures_timer/) crate. +/// It is async runtime agnostic and will also work in WASM environments. +#[cfg(feature = "futures-timer-sleep")] +#[derive(Clone, Copy, Debug, Default)] +pub struct FuturesTimerSleep; + +#[cfg(feature = "futures-timer-sleep")] +impl Sleeper for FuturesTimerSleep { + type Sleep = futures_timer::Delay; + + fn sleep(&self, dur: Duration) -> Self::Sleep { + futures_timer::Delay::new(dur) + } +} + /// The default implementation of `Sleeper` utilizes `gloo_timers::future::sleep`. #[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))] #[derive(Clone, Copy, Debug, Default)]