Skip to content

Commit

Permalink
feat: impl MakeDelayExt (#23)
Browse files Browse the repository at this point in the history
Signed-off-by: tison <[email protected]>
  • Loading branch information
tisonkun authored Feb 8, 2025
1 parent 13c9795 commit 54fab10
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
45 changes: 40 additions & 5 deletions fastimer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn make_instant_from_now(dur: Duration) -> Instant {
}

/// A trait for creating delay futures.
pub trait MakeDelay: Send + 'static {
pub trait MakeDelay {
/// The future returned by the `delay`/`delay_until` method.
type Delay: Future<Output = ()> + Send;

Expand All @@ -92,6 +92,41 @@ pub trait Spawn {
fn spawn<F: Future<Output = ()> + Send + 'static>(&self, future: F);
}

/// Provides extension methods for [`MakeDelay`] implementors.
pub trait MakeDelayExt: MakeDelay {
/// Requires a `Future` to complete before the specified duration has elapsed.
fn timeout<F: Future>(&self, duration: Duration, fut: F) -> Timeout<F, Self::Delay> {
timeout(duration, fut, self)
}

/// Requires a `Future` to complete before the specified instant in time.
fn timeout_at<F: Future>(&self, deadline: Instant, fut: F) -> Timeout<F, Self::Delay> {
timeout_at(deadline, fut, self)
}

/// Creates new [`Interval`] that yields with interval of `period`.
///
/// See [`interval`] for more details.
fn interval(self, period: Duration) -> Interval<Self>
where
Self: Sized,
{
interval(period, self)
}

/// Creates new [`Interval`] that yields with interval of `period` and starts at `at`.
///
/// See [`interval_at`] for more details.
fn interval_at(self, at: Instant, period: Duration) -> Interval<Self>
where
Self: Sized,
{
interval_at(at, period, self)
}
}

impl<T: MakeDelay> MakeDelayExt for T {}

/// Errors returned by [`Timeout`].
///
/// This error is returned when a timeout expires before the function was able
Expand Down Expand Up @@ -154,11 +189,11 @@ where
pub fn timeout<F, D>(
duration: Duration,
future: F,
make_delay: D,
make_delay: &D,
) -> Timeout<F::IntoFuture, D::Delay>
where
F: IntoFuture,
D: MakeDelay,
D: MakeDelay + ?Sized,
{
let delay = make_delay.delay(duration);
Timeout {
Expand All @@ -171,11 +206,11 @@ where
pub fn timeout_at<F, D>(
deadline: Instant,
future: F,
make_delay: D,
make_delay: &D,
) -> Timeout<F::IntoFuture, D::Delay>
where
F: IntoFuture,
D: MakeDelay,
D: MakeDelay + ?Sized,
{
let delay = make_delay.delay_util(deadline);
Timeout {
Expand Down
4 changes: 1 addition & 3 deletions fastimer/src/schedule/arbitrary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub trait ArbitraryDelayActionExt: ArbitraryDelayAction {
) where
Self: Sized,
S: Spawn,
D: MakeDelay,
D: MakeDelay + Send + 'static,
{
spawn.spawn(async move {
debug!(
Expand All @@ -60,8 +60,6 @@ pub trait ArbitraryDelayActionExt: ArbitraryDelayAction {
None => return,
};

let make_delay = make_delay;

loop {
debug!("executing scheduled task {}", self.name());
let next = self.run().await;
Expand Down
2 changes: 1 addition & 1 deletion fastimer/src/schedule/notify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub trait NotifyActionExt: NotifyAction {
where
Self: Sized,
S: Spawn,
D: MakeDelay,
D: MakeDelay + Send + 'static,
{
spawn.spawn(async move {
debug!(
Expand Down
4 changes: 2 additions & 2 deletions fastimer/src/schedule/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait SimpleActionExt: SimpleAction {
) where
Self: Sized,
S: Spawn,
D: MakeDelay,
D: MakeDelay + Send + 'static,
{
spawn.spawn(async move {
debug!(
Expand Down Expand Up @@ -95,7 +95,7 @@ pub trait SimpleActionExt: SimpleAction {
) where
Self: Sized,
S: Spawn,
D: MakeDelay,
D: MakeDelay + Send + 'static,
{
assert!(period > Duration::new(0, 0), "`period` must be non-zero.");

Expand Down

0 comments on commit 54fab10

Please sign in to comment.