Skip to content

Commit

Permalink
Add optional futures-timer based Sleeper
Browse files Browse the repository at this point in the history
  • Loading branch information
NumberFour8 committed Dec 25, 2024
1 parent f7e3b97 commit d350da8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
5 changes: 4 additions & 1 deletion backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 6 additions & 5 deletions backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand Down
33 changes: 32 additions & 1 deletion backon/src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ 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 = "gloo-timers-sleep"),
not(feature = "futures-timer-sleep")
))]
pub type DefaultSleeper = PleaseEnableAFeatureOrProvideACustomSleeper;
/// The default implementation of `Sleeper` while feature `tokio-sleep` enabled.
///
Expand All @@ -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.
///
Expand Down Expand Up @@ -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)]
Expand Down

0 comments on commit d350da8

Please sign in to comment.