Skip to content

Commit 54fab10

Browse files
authored
feat: impl MakeDelayExt (#23)
Signed-off-by: tison <[email protected]>
1 parent 13c9795 commit 54fab10

File tree

4 files changed

+44
-11
lines changed

4 files changed

+44
-11
lines changed

fastimer/src/lib.rs

+40-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn make_instant_from_now(dur: Duration) -> Instant {
7373
}
7474

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

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

95+
/// Provides extension methods for [`MakeDelay`] implementors.
96+
pub trait MakeDelayExt: MakeDelay {
97+
/// Requires a `Future` to complete before the specified duration has elapsed.
98+
fn timeout<F: Future>(&self, duration: Duration, fut: F) -> Timeout<F, Self::Delay> {
99+
timeout(duration, fut, self)
100+
}
101+
102+
/// Requires a `Future` to complete before the specified instant in time.
103+
fn timeout_at<F: Future>(&self, deadline: Instant, fut: F) -> Timeout<F, Self::Delay> {
104+
timeout_at(deadline, fut, self)
105+
}
106+
107+
/// Creates new [`Interval`] that yields with interval of `period`.
108+
///
109+
/// See [`interval`] for more details.
110+
fn interval(self, period: Duration) -> Interval<Self>
111+
where
112+
Self: Sized,
113+
{
114+
interval(period, self)
115+
}
116+
117+
/// Creates new [`Interval`] that yields with interval of `period` and starts at `at`.
118+
///
119+
/// See [`interval_at`] for more details.
120+
fn interval_at(self, at: Instant, period: Duration) -> Interval<Self>
121+
where
122+
Self: Sized,
123+
{
124+
interval_at(at, period, self)
125+
}
126+
}
127+
128+
impl<T: MakeDelay> MakeDelayExt for T {}
129+
95130
/// Errors returned by [`Timeout`].
96131
///
97132
/// This error is returned when a timeout expires before the function was able
@@ -154,11 +189,11 @@ where
154189
pub fn timeout<F, D>(
155190
duration: Duration,
156191
future: F,
157-
make_delay: D,
192+
make_delay: &D,
158193
) -> Timeout<F::IntoFuture, D::Delay>
159194
where
160195
F: IntoFuture,
161-
D: MakeDelay,
196+
D: MakeDelay + ?Sized,
162197
{
163198
let delay = make_delay.delay(duration);
164199
Timeout {
@@ -171,11 +206,11 @@ where
171206
pub fn timeout_at<F, D>(
172207
deadline: Instant,
173208
future: F,
174-
make_delay: D,
209+
make_delay: &D,
175210
) -> Timeout<F::IntoFuture, D::Delay>
176211
where
177212
F: IntoFuture,
178-
D: MakeDelay,
213+
D: MakeDelay + ?Sized,
179214
{
180215
let delay = make_delay.delay_util(deadline);
181216
Timeout {

fastimer/src/schedule/arbitrary.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub trait ArbitraryDelayActionExt: ArbitraryDelayAction {
4545
) where
4646
Self: Sized,
4747
S: Spawn,
48-
D: MakeDelay,
48+
D: MakeDelay + Send + 'static,
4949
{
5050
spawn.spawn(async move {
5151
debug!(
@@ -60,8 +60,6 @@ pub trait ArbitraryDelayActionExt: ArbitraryDelayAction {
6060
None => return,
6161
};
6262

63-
let make_delay = make_delay;
64-
6563
loop {
6664
debug!("executing scheduled task {}", self.name());
6765
let next = self.run().await;

fastimer/src/schedule/notify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub trait NotifyActionExt: NotifyAction {
5454
where
5555
Self: Sized,
5656
S: Spawn,
57-
D: MakeDelay,
57+
D: MakeDelay + Send + 'static,
5858
{
5959
spawn.spawn(async move {
6060
debug!(

fastimer/src/schedule/simple.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub trait SimpleActionExt: SimpleAction {
5050
) where
5151
Self: Sized,
5252
S: Spawn,
53-
D: MakeDelay,
53+
D: MakeDelay + Send + 'static,
5454
{
5555
spawn.spawn(async move {
5656
debug!(
@@ -95,7 +95,7 @@ pub trait SimpleActionExt: SimpleAction {
9595
) where
9696
Self: Sized,
9797
S: Spawn,
98-
D: MakeDelay,
98+
D: MakeDelay + Send + 'static,
9999
{
100100
assert!(period > Duration::new(0, 0), "`period` must be non-zero.");
101101

0 commit comments

Comments
 (0)