Skip to content

Commit

Permalink
feat: add Sleeper based on futures-timer (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
NumberFour8 authored Dec 27, 2024
1 parent 1582dbc commit 45e9170
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
Expand Down
14 changes: 8 additions & 6 deletions backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
//!
Expand Down
18 changes: 18 additions & 0 deletions backon/src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit 45e9170

Please sign in to comment.