Skip to content

Commit 6a5dd16

Browse files
committed
refactor(Timer): Set duration upon construction
- Add duration to `Clock::new_timer()` and `Timer::new()` functions - Remove `Timer::set_duration()` method - Remove `Disarmed` `Timer` state BREAKING CHANGE: `Timer` interface
1 parent 667c0ca commit 6a5dd16

File tree

3 files changed

+27
-58
lines changed

3 files changed

+27
-58
lines changed

examples/nrf52_dk/main.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -152,20 +152,12 @@ where
152152
led2.set_high()?;
153153
led3.set_high()?;
154154
led4.set_low()?;
155-
clock
156-
.new_timer()
157-
.set_duration(250_u32.milliseconds())
158-
.start()
159-
.wait();
155+
clock.new_timer(250_u32.milliseconds()).start().wait();
160156

161157
led1.set_high()?;
162158
led2.set_low()?;
163159
led3.set_low()?;
164160
led4.set_high()?;
165-
clock
166-
.new_timer()
167-
.set_duration(250_u32.milliseconds())
168-
.start()
169-
.wait();
161+
clock.new_timer(250_u32.milliseconds()).start().wait();
170162
}
171163
}

src/clock.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ pub trait Clock: Sized {
4343
fn now(&self) -> Result<Instant<Self>, Error<Self::ImplError>>;
4444

4545
/// Spawn a new, `OneShot` [`Timer`] from this clock
46-
fn new_timer<Dur: Duration>(&self) -> Timer<param::OneShot, param::Disarmed, Self, Dur> {
47-
Timer::<param::None, param::None, Self, Dur>::new(&self)
46+
fn new_timer<Dur: Duration>(
47+
&self,
48+
duration: Dur,
49+
) -> Timer<param::OneShot, param::Armed, Self, Dur> {
50+
Timer::<param::None, param::None, Self, Dur>::new(&self, duration)
4851
}
4952
}

src/timer.rs

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ pub(crate) mod param {
77
#[derive(Debug)]
88
pub struct None;
99

10-
#[derive(Debug)]
11-
pub struct Disarmed;
12-
1310
#[derive(Debug)]
1411
pub struct Armed;
1512

@@ -28,7 +25,7 @@ pub(crate) mod param {
2825
#[derive(Debug)]
2926
pub struct Timer<'a, Type, State, Clock: crate::Clock, Dur: Duration> {
3027
clock: &'a Clock,
31-
duration: Option<Dur>,
28+
duration: Dur,
3229
expiration: Option<Instant<Clock>>,
3330
_type: PhantomData<Type>,
3431
_state: PhantomData<State>,
@@ -37,10 +34,10 @@ pub struct Timer<'a, Type, State, Clock: crate::Clock, Dur: Duration> {
3734
impl<'a, Clock: crate::Clock, Dur: Duration> Timer<'_, param::None, param::None, Clock, Dur> {
3835
/// Construct a new, `OneShot` `Timer`
3936
#[allow(clippy::new_ret_no_self)]
40-
pub fn new(clock: &Clock) -> Timer<OneShot, Disarmed, Clock, Dur> {
41-
Timer::<OneShot, Disarmed, Clock, Dur> {
37+
pub fn new(clock: &Clock, duration: Dur) -> Timer<OneShot, Armed, Clock, Dur> {
38+
Timer::<OneShot, Armed, Clock, Dur> {
4239
clock,
43-
duration: Option::None,
40+
duration,
4441
expiration: Option::None,
4542
_type: PhantomData,
4643
_state: PhantomData,
@@ -72,30 +69,16 @@ impl<'a, Type, State, Clock: crate::Clock, Dur: Duration> Timer<'a, Type, State,
7269
}
7370
}
7471

75-
impl<'a, Type, Clock: crate::Clock, Dur: Duration> Timer<'a, Type, Disarmed, Clock, Dur> {
76-
/// Set the [`Duration`] of the timer
77-
///
78-
/// This _arms_ the timer (makes it ready to run).
79-
pub fn set_duration(self, duration: Dur) -> Timer<'a, Type, Armed, Clock, Dur> {
80-
Timer::<Type, Armed, Clock, Dur> {
81-
clock: self.clock,
82-
duration: Some(duration),
83-
expiration: Option::None,
84-
_type: PhantomData,
85-
_state: PhantomData,
86-
}
87-
}
88-
}
8972
impl<'a, Type, Clock: crate::Clock, Dur: Duration> Timer<'a, Type, Armed, Clock, Dur> {
90-
/// Start the _armed_ timer from this instant
73+
/// Start the timer from this instant
9174
pub fn start(self) -> Timer<'a, Type, Running, Clock, Dur>
9275
where
9376
Instant<Clock>: Add<Dur, Output = Instant<Clock>>,
9477
{
9578
Timer::<Type, Running, Clock, Dur> {
9679
clock: self.clock,
9780
duration: self.duration,
98-
expiration: Some(self.clock.now().unwrap() + self.duration.unwrap()),
81+
expiration: Some(self.clock.now().unwrap() + self.duration),
9982
_type: PhantomData,
10083
_state: PhantomData,
10184
}
@@ -109,8 +92,9 @@ impl<Type, Clock: crate::Clock, Dur: Duration> Timer<'_, Type, Running, Clock, D
10992

11093
/// Returns the [`Duration`] of time elapsed since it was started
11194
///
112-
/// The units of the [`Duration`] are the same as that used with
113-
/// [`set_duration()`](struct.Timer.html#method.set_duration).
95+
/// **The duration is truncated, not rounded**.
96+
///
97+
/// The units of the [`Duration`] are the same as that used to construct the `Timer`.
11498
pub fn elapsed(&self) -> Dur
11599
where
116100
Dur::Rep: TryFrom<Clock::Rep>,
@@ -119,14 +103,15 @@ impl<Type, Clock: crate::Clock, Dur: Duration> Timer<'_, Type, Running, Clock, D
119103
self.clock
120104
.now()
121105
.unwrap()
122-
.duration_since(&(self.expiration.unwrap() - self.duration.unwrap()))
106+
.duration_since(&(self.expiration.unwrap() - self.duration))
123107
.unwrap()
124108
}
125109

126110
/// Returns the [`Duration`] until the expiration of the timer
127111
///
128-
/// The units of the [`Duration`] are the same as that used with
129-
/// [`set_duration()`](struct.Timer.html#method.set_duration).
112+
/// **The duration is truncated, not rounded**.
113+
///
114+
/// The units of the [`Duration`] are the same as that used to construct the `Timer`.
130115
pub fn remaining(&self) -> Dur
131116
where
132117
Dur::Rep: TryFrom<Clock::Rep>,
@@ -151,8 +136,7 @@ impl<'a, Clock: crate::Clock, Dur: Duration> Timer<'a, OneShot, Running, Clock,
151136
// since the timer is running, _is_expired() will return a value
152137
while !self._is_expired() {}
153138

154-
Timer::<param::None, param::None, Clock, Dur>::new(self.clock)
155-
.set_duration(self.duration.unwrap())
139+
Timer::<param::None, param::None, Clock, Dur>::new(self.clock, self.duration)
156140
}
157141

158142
/// Check whether the timer has expired
@@ -177,9 +161,7 @@ impl<Clock: crate::Clock, Dur: Duration> Timer<'_, Periodic, Running, Clock, Dur
177161
Self {
178162
clock: self.clock,
179163
duration: self.duration,
180-
expiration: self
181-
.expiration
182-
.map(|expiration| expiration + self.duration.unwrap()),
164+
expiration: self.expiration.map(|expiration| expiration + self.duration),
183165
_type: PhantomData,
184166
_state: PhantomData,
185167
}
@@ -194,7 +176,7 @@ impl<Clock: crate::Clock, Dur: Duration> Timer<'_, Periodic, Running, Clock, Dur
194176
{
195177
// since the timer is running, _is_expired() will return a value
196178
if self._is_expired() {
197-
self.expiration = Some(self.expiration.unwrap() + self.duration.unwrap());
179+
self.expiration = Some(self.expiration.unwrap() + self.duration);
198180

199181
true
200182
} else {
@@ -231,7 +213,7 @@ mod test {
231213
init_ticks();
232214
let clock = Clock;
233215

234-
let timer = clock.new_timer().set_duration(1_u32.seconds()).start();
216+
let timer = clock.new_timer(1_u32.seconds()).start();
235217

236218
thread::scope(|s| {
237219
let timer_handle = s.spawn(|_| timer.wait());
@@ -260,11 +242,7 @@ mod test {
260242
init_ticks();
261243
let clock = Clock;
262244

263-
let timer = clock
264-
.new_timer()
265-
.into_periodic()
266-
.set_duration(1_u32.seconds())
267-
.start();
245+
let timer = clock.new_timer(1_u32.seconds()).into_periodic().start();
268246

269247
thread::scope(|s| {
270248
let timer_handle = s.spawn(|_| timer.wait());
@@ -292,11 +270,7 @@ mod test {
292270
init_ticks();
293271
let clock = Clock;
294272

295-
let mut timer = clock
296-
.new_timer()
297-
.into_periodic()
298-
.set_duration(1_u32.seconds())
299-
.start();
273+
let mut timer = clock.new_timer(1_u32.seconds()).into_periodic().start();
300274

301275
add_to_ticks(2_u32.seconds());
302276

@@ -309,7 +283,7 @@ mod test {
309283
init_ticks();
310284
let clock = Clock;
311285

312-
let timer = clock.new_timer().set_duration(2_u32.seconds()).start();
286+
let timer = clock.new_timer(2_u32.seconds()).start();
313287

314288
add_to_ticks(1_u32.milliseconds());
315289

0 commit comments

Comments
 (0)