Skip to content

Commit

Permalink
Replace gloo-timers with futures-timer
Browse files Browse the repository at this point in the history
- 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 Xuanwo#153
  • Loading branch information
NumberFour8 committed Oct 17, 2024
1 parent 781b367 commit 736d881
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
8 changes: 5 additions & 3 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
17 changes: 10 additions & 7 deletions backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand Down Expand Up @@ -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;
Expand Down
28 changes: 14 additions & 14 deletions backon/src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@ impl<F: Fn(Duration) -> Fut + 'static, Fut: Future<Output = ()>> 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`].
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 736d881

Please sign in to comment.