Skip to content

Commit

Permalink
Merge branch 'main' into feature/add-futures-timer
Browse files Browse the repository at this point in the history
  • Loading branch information
NumberFour8 authored Dec 26, 2024
2 parents d350da8 + 1582dbc commit 895dc2c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions backon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ std-blocking-sleep = []
gloo-timers-sleep = ["gloo-timers/futures"]
tokio-sleep = ["tokio/time"]
futures-timer-sleep = ["futures-timer"]
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
20 changes: 20 additions & 0 deletions backon/src/embassy_timer_sleep.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::{BlockingSleeper, Sleeper};
use core::time::Duration;

/// A no_std async sleeper based on the embassy framework (https://embassy.dev)
#[derive(Clone, Copy, Debug, Default)]
pub struct EmbassySleeper;

impl Sleeper for EmbassySleeper {
type Sleep = embassy_time::Timer;

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

impl BlockingSleeper for EmbassySleeper {
fn sleep(&self, dur: Duration) {
embassy_time::block_for(embassy_time::Duration::from_millis(dur.as_millis() as u64));
}
}
9 changes: 8 additions & 1 deletion backon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
//! |----------------------|--------------------|-------------|---------------|
//! | [`TokioSleeper`] | tokio-sleep | non-wasm32 | Yes |
//! | [`GlooTimersSleep`] | gloo-timers-sleep | wasm32 | Yes |
//! | [`FutureTimerSleep`] | future-timer-sleep | both | 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 Expand Up @@ -185,5 +187,10 @@ pub use blocking_sleep::DefaultBlockingSleeper;
#[cfg(feature = "std-blocking-sleep")]
pub use blocking_sleep::StdSleeper;

#[cfg(feature = "embassy-sleep")]
mod embassy_timer_sleep;
#[cfg(feature = "embassy-sleep")]
pub use embassy_timer_sleep::EmbassySleeper;

#[cfg(docsrs)]
pub mod docs;

0 comments on commit 895dc2c

Please sign in to comment.