diff --git a/backon/Cargo.toml b/backon/Cargo.toml index 8ef5d53..d23d79c 100644 --- a/backon/Cargo.toml +++ b/backon/Cargo.toml @@ -25,6 +25,7 @@ std = ["fastrand/std"] std-blocking-sleep = [] gloo-timers-sleep = ["gloo-timers/futures"] tokio-sleep = ["tokio/time"] +futures-timer-sleep = ["futures-timer"] embassy-sleep = ["embassy-time"] [dependencies] @@ -33,9 +34,11 @@ embassy-time = { version = "0.3", optional = true } [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 76b9c1c..e691daf 100644 --- a/backon/src/lib.rs +++ b/backon/src/lib.rs @@ -41,12 +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 | -//! | [`EmbassySleep`] | embassy-sleep | no_std | 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 |wasm/non-wasm| Yes | +//! | [`EmbassySleep`] | embassy-sleep | no_std | Yes | +//! | [`StdSleeper`] | std-blocking-sleep | std | No | + //! //! ## Custom Sleeper //! diff --git a/backon/src/sleep.rs b/backon/src/sleep.rs index 71dab5c..ce89402 100644 --- a/backon/src/sleep.rs +++ b/backon/src/sleep.rs @@ -78,6 +78,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)]