diff --git a/backon/Cargo.toml b/backon/Cargo.toml index 1caf728..1953f57 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -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", "wasm-bindgen"], optional = true } [dev-dependencies] anyhow = "1" diff --git a/backon/src/lib.rs b/backon/src/lib.rs index 2cac7c7..53ec51a 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,6 +171,8 @@ pub use retry_with_context::RetryableWithContext; 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; diff --git a/backon/src/sleep.rs b/backon/src/sleep.rs index 4a5ab46..6cd48cb 100644 --- a/backon/src/sleep.rs +++ b/backon/src/sleep.rs @@ -36,19 +36,21 @@ 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 +80,24 @@ impl Sleeper for TokioSleeper { } } -/// The default implementation of `Sleeper` utilizes `gloo_timers::future::sleep`. -#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))] +/// Sleeper implementation using `futures-timer` crate. +/// +/// 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 GlooTimersSleep; +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) } } + +/// 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;