From 736d881710c4fc2f654822be4009b4ab6d18e4b6 Mon Sep 17 00:00:00 2001 From: NumberFour8 Date: Thu, 17 Oct 2024 19:37:54 +0200 Subject: [PATCH 1/5] Replace `gloo-timers` with `futures-timer` - Migrated Sleeper implementation from `gloo-timers` to async runtime-agnostic `futures-timer` - Adjusted feature flags and updated dependencies to reflect this change, ensuring seamless functionality across different environments: - `gloo-timers-sleep` feature kept for backwards compatibility, it will imply the automatically `futures-timer-sleep` - if `wasm32` architecture is detected, it automatically enables the `gloo-timers` feature on `futures-timer` crate to ensure WASM compatibility. Closes #153 --- backon/Cargo.toml | 8 +++++--- backon/src/lib.rs | 17 ++++++++++------- backon/src/sleep.rs | 28 ++++++++++++++-------------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/backon/Cargo.toml b/backon/Cargo.toml index 1caf728..3a3028e 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -4,7 +4,7 @@ documentation = "https://docs.rs/backon" name = "backon" readme = "../README.md" rust-version = "1.70" -version = "1.2.0" +version = "1.3.0" edition.workspace = true license.workspace = true @@ -22,17 +22,19 @@ targets = [ [features] default = ["std-blocking-sleep", "tokio-sleep", "gloo-timers-sleep"] std-blocking-sleep = [] -gloo-timers-sleep = ["dep:gloo-timers", "gloo-timers?/futures"] +gloo-timers-sleep = ["futures-timer-sleep"] tokio-sleep = ["dep:tokio", "tokio?/time"] +futures-timer-sleep = ["dep:futures-timer"] [dependencies] fastrand = "2" [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", features = ["gloo-timers"], optional = true } [dev-dependencies] anyhow = "1" diff --git a/backon/src/lib.rs b/backon/src/lib.rs index 2cac7c7..fd5cb40 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -41,11 +41,14 @@ //! 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 | all | No | +//! | `Sleeper` | feature | Environment | Asynchronous | +//! |-------------------------|-----------------------|-------------|---------------| +//! | [`TokioSleeper`] | tokio-sleep | non-wasm32 | Yes | +//! | [`FuturesTimerSleeper`] | future-timers-sleep | both | Yes | +//! | [`StdSleeper`] | std-blocking-sleep | all | No | +//! +//! The `gloo-timers-sleep` feature implies the `future-timers-sleep` feature and is only +//! kept for backwards compatibility. //! //! ## Custom Sleeper //! @@ -168,8 +171,8 @@ pub use retry_with_context::RetryableWithContext; mod sleep; pub use sleep::DefaultSleeper; -#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))] -pub use sleep::GlooTimersSleep; +#[cfg(all(target_arch = "wasm32", feature = "futures-timer-sleep"))] +pub use sleep::FuturesTimerSleeper; pub use sleep::Sleeper; #[cfg(all(not(target_arch = "wasm32"), feature = "tokio-sleep"))] pub use sleep::TokioSleeper; diff --git a/backon/src/sleep.rs b/backon/src/sleep.rs index 4a5ab46..6337aae 100644 --- a/backon/src/sleep.rs +++ b/backon/src/sleep.rs @@ -36,19 +36,18 @@ 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 = "futures-timer-sleep")))] pub type DefaultSleeper = PleaseEnableAFeatureOrProvideACustomSleeper; /// The default implementation of `Sleeper` while feature `tokio-sleep` enabled. /// /// it uses `tokio::time::sleep`. #[cfg(all(not(target_arch = "wasm32"), feature = "tokio-sleep"))] pub type DefaultSleeper = TokioSleeper; -/// The default implementation of `Sleeper` while feature `gloo-timers-sleep` enabled. +/// The default implementation of `Sleeper` while feature `futures-timer-sleep` enabled. /// -/// It uses `gloo_timers::sleep::sleep`. -#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))] -pub type DefaultSleeper = GlooTimersSleep; - +/// It uses `futures_timer::Delay`. +#[cfg(any(target_arch = "wasm32", all(not(feature = "tokio-sleep"), feature = "futures-timer-sleep")))] +pub type DefaultSleeper = FuturesTimerSleeper; /// A placeholder type that does not implement [`Sleeper`] and will therefore fail to compile if used as one. /// /// Users should enable a feature of this crate that provides a valid [`Sleeper`] implementation when this type appears in compilation errors. Alternatively, a custom [`Sleeper`] implementation should be provided where necessary, such as in [`crate::Retry::sleeper`]. @@ -78,16 +77,17 @@ impl Sleeper for TokioSleeper { } } -/// The default implementation of `Sleeper` utilizes `gloo_timers::future::sleep`. -#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))] -#[derive(Clone, Copy, Debug, Default)] -pub struct GlooTimersSleep; +/// Sleeper implementation using `futures-timer` crate. +/// +/// See the [`futures-timer` crate](https://docs.rs/futures-timer/latest/) for details. +#[cfg(feature = "futures-timer-sleep")] +pub struct FuturesTimerSleeper; -#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))] -impl Sleeper for GlooTimersSleep { - type Sleep = gloo_timers::future::TimeoutFuture; +#[cfg(feature = "futures-timer-sleep")] +impl Sleeper for FuturesTimerSleeper { + type Sleep = futures_timer::Delay; fn sleep(&self, dur: Duration) -> Self::Sleep { - gloo_timers::future::sleep(dur) + futures_timer::Delay::new(dur) } } From 9a43b6727a3e6164f88befe6a554c0448a95bb78 Mon Sep 17 00:00:00 2001 From: NumberFour8 Date: Thu, 17 Oct 2024 19:40:52 +0200 Subject: [PATCH 2/5] Format with --- backon/src/sleep.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/backon/src/sleep.rs b/backon/src/sleep.rs index 6337aae..05b1411 100644 --- a/backon/src/sleep.rs +++ b/backon/src/sleep.rs @@ -46,7 +46,10 @@ pub type DefaultSleeper = TokioSleeper; /// The default implementation of `Sleeper` while feature `futures-timer-sleep` enabled. /// /// It uses `futures_timer::Delay`. -#[cfg(any(target_arch = "wasm32", all(not(feature = "tokio-sleep"), feature = "futures-timer-sleep")))] +#[cfg(any( + target_arch = "wasm32", + all(not(feature = "tokio-sleep"), feature = "futures-timer-sleep") +))] pub type DefaultSleeper = FuturesTimerSleeper; /// A placeholder type that does not implement [`Sleeper`] and will therefore fail to compile if used as one. /// From c81dd8cd826088160fc79e247944c0a251839b4b Mon Sep 17 00:00:00 2001 From: NumberFour8 Date: Thu, 17 Oct 2024 20:39:56 +0200 Subject: [PATCH 3/5] Add missing derives --- backon/src/sleep.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/backon/src/sleep.rs b/backon/src/sleep.rs index 05b1411..076b1e2 100644 --- a/backon/src/sleep.rs +++ b/backon/src/sleep.rs @@ -84,6 +84,7 @@ impl Sleeper for TokioSleeper { /// /// See the [`futures-timer` crate](https://docs.rs/futures-timer/latest/) for details. #[cfg(feature = "futures-timer-sleep")] +#[derive(Clone, Copy, Debug, Default)] pub struct FuturesTimerSleeper; #[cfg(feature = "futures-timer-sleep")] From 5e2dbda266f3d5af0699911118d2297b48b37a44 Mon Sep 17 00:00:00 2001 From: NumberFour8 Date: Thu, 17 Oct 2024 20:43:22 +0200 Subject: [PATCH 4/5] Add wasm-bindgen feature --- backon/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backon/Cargo.toml b/backon/Cargo.toml index 3a3028e..23c0d52 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -34,7 +34,7 @@ tokio = { version = "1", optional = true } futures-timer = { version = "3.0.3", optional = true } [target.'cfg(target_arch = "wasm32")'.dependencies] -futures-timer = { version = "3.0.3", features = ["gloo-timers"], optional = true } +futures-timer = { version = "3.0.3", features = ["gloo-timers", "wasm-bindgen"], optional = true } [dev-dependencies] anyhow = "1" From 12102f31ce3c8bbfe334bdcbf4593f0914d56199 Mon Sep 17 00:00:00 2001 From: NumberFour8 Date: Wed, 30 Oct 2024 14:50:06 +0100 Subject: [PATCH 5/5] Restore GlooTimersSleep --- backon/Cargo.toml | 2 +- backon/src/lib.rs | 2 ++ backon/src/sleep.rs | 6 ++++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/backon/Cargo.toml b/backon/Cargo.toml index 23c0d52..1953f57 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -4,7 +4,7 @@ documentation = "https://docs.rs/backon" name = "backon" readme = "../README.md" rust-version = "1.70" -version = "1.3.0" +version = "1.2.0" edition.workspace = true license.workspace = true diff --git a/backon/src/lib.rs b/backon/src/lib.rs index fd5cb40..53ec51a 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -173,6 +173,8 @@ mod sleep; pub use sleep::DefaultSleeper; #[cfg(all(target_arch = "wasm32", feature = "futures-timer-sleep"))] pub use sleep::FuturesTimerSleeper; +#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))] +pub use sleep::GlooTimersSleep; pub use sleep::Sleeper; #[cfg(all(not(target_arch = "wasm32"), feature = "tokio-sleep"))] pub use sleep::TokioSleeper; diff --git a/backon/src/sleep.rs b/backon/src/sleep.rs index 076b1e2..6cd48cb 100644 --- a/backon/src/sleep.rs +++ b/backon/src/sleep.rs @@ -95,3 +95,9 @@ impl Sleeper for FuturesTimerSleeper { futures_timer::Delay::new(dur) } } + +/// Legacy `gloo-timers` based sleeper, which is now just an alias for [`FuturesTimerSleeper`]. +/// +/// Note that the `gloo-timers-sleep` feature automatically enables `futures-timer-sleeper`. +#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))] +pub type GlooTimersSleep = FuturesTimerSleeper;