Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace gloo-timers with futures-timer #154

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 10 additions & 5 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,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;
Expand Down
36 changes: 23 additions & 13 deletions backon/src/sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,21 @@ 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 +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;
NumberFour8 marked this conversation as resolved.
Show resolved Hide resolved

#[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;