Skip to content

Commit

Permalink
Add embassy-based sleeper for no_std
Browse files Browse the repository at this point in the history
  • Loading branch information
wackazong committed Dec 18, 2024
1 parent f7e3b97 commit ad7d2c3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
2 changes: 2 additions & 0 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ std = ["fastrand/std"]
std-blocking-sleep = []
gloo-timers-sleep = ["gloo-timers/futures"]
tokio-sleep = ["tokio/time"]
embassy-sleep = ["embassy-time"]

[dependencies]
fastrand = { version = "2", default-features = false }
embassy-time = { version = "0.3", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
tokio = { version = "1", optional = true }
Expand Down
19 changes: 18 additions & 1 deletion backon/src/blocking_sleep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,18 @@ impl<F: Fn(Duration) + 'static> BlockingSleeper 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(not(feature = "std-blocking-sleep"))]
#[cfg(all(not(feature = "std-blocking-sleep"), not(feature = "embassy-sleep")))]
pub type DefaultBlockingSleeper = PleaseEnableAFeatureOrProvideACustomSleeper;
/// The default implementation of `Sleeper` while feature `std-blocking-sleep` enabled.
///
/// it uses [`std::thread::sleep`].
#[cfg(feature = "std-blocking-sleep")]
pub type DefaultBlockingSleeper = StdSleeper;
/// The default implementation of `Sleeper` while feature `embassy-sleep` enabled.
///
/// it uses [`embassy_time::block_for`].
#[cfg(all(not(feature = "std-blocking-sleep"), feature = "embassy-sleep"))]
pub type DefaultBlockingSleeper = EmbassySleeper;

/// A placeholder type that does not implement [`Sleeper`] and will therefore fail to compile if used as one.
///
Expand All @@ -53,3 +58,15 @@ impl BlockingSleeper for StdSleeper {
std::thread::sleep(dur)
}
}

/// The no_std implementation of `StdSleeper`.
#[cfg(feature = "embassy-sleep")]
#[derive(Clone, Copy, Debug, Default)]
pub struct EmbassySleeper;

#[cfg(feature = "embassy-sleep")]
impl BlockingSleeper for EmbassySleeper {
fn sleep(&self, dur: Duration) {
embassy_time::block_for(embassy_time::Duration::from_millis(dur.as_millis() as u64));
}
}
3 changes: 3 additions & 0 deletions backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
//! |---------------------|--------------------|-------------|---------------|
//! | [`TokioSleeper`] | tokio-sleep | non-wasm32 | Yes |
//! | [`GlooTimersSleep`] | gloo-timers-sleep | wasm32 | Yes |
//! | [`EmbassySleep`] | embassy-sleep | no_std | Yes |
//! | [`StdSleeper`] | std-blocking-sleep | std | No |
//!
//! ## Custom Sleeper
Expand Down Expand Up @@ -166,6 +167,8 @@ pub use retry_with_context::RetryableWithContext;

mod sleep;
pub use sleep::DefaultSleeper;
#[cfg(all(not(feature = "std"), feature = "embassy-sleep"))]
pub use sleep::EmbassySleep;
#[cfg(all(target_arch = "wasm32", feature = "gloo-timers-sleep"))]
pub use sleep::GlooTimersSleep;
pub use sleep::Sleeper;
Expand Down
25 changes: 24 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 = "embassy-sleep")
))]
pub type DefaultSleeper = PleaseEnableAFeatureOrProvideACustomSleeper;
/// The default implementation of `Sleeper` while feature `tokio-sleep` enabled.
///
Expand All @@ -48,6 +52,11 @@ 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 feature `embassy-sleep` enabled.
///
/// It uses `embassy_time::Timer`.
#[cfg(all(not(feature = "std"), feature = "embassy-sleep"))]
pub type DefaultSleeper = EmbassySleep;

/// A placeholder type that does not implement [`Sleeper`] and will therefore fail to compile if used as one.
///
Expand Down Expand Up @@ -91,3 +100,17 @@ impl Sleeper for GlooTimersSleep {
gloo_timers::future::sleep(dur)
}
}

/// The default implementation of `Sleeper` utilizes `embassy-time::Timer`.
#[cfg(all(not(feature = "std"), feature = "embassy-sleep"))]
#[derive(Clone, Copy, Debug, Default)]
pub struct EmbassySleep;

#[cfg(all(not(feature = "std"), feature = "embassy-sleep"))]
impl Sleeper for EmbassySleep {
type Sleep = embassy_time::Timer;

fn sleep(&self, dur: Duration) -> Self::Sleep {
embassy_time::Timer::after_millis(dur.as_millis() as u64)
}
}

0 comments on commit ad7d2c3

Please sign in to comment.