Skip to content

Commit

Permalink
feat(Timer): Add return from OneShot Timer::wait()
Browse files Browse the repository at this point in the history
Return a TimerBuilder<OneShot, Armed, ...>
  • Loading branch information
PTaylor-us committed Jun 29, 2020
1 parent 9fc4fbb commit 9ced1a8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,12 @@ impl<Type, Clock: crate::Clock, Dur: Duration, Task: FnMut()>

impl<Clock: crate::Clock, Dur: Duration, Task: FnMut()> Timer<OneShot, Running, Clock, Dur, Task> {
/// Block until the timer has expired
pub fn wait(self) {
pub fn wait(self) -> TimerBuilder<OneShot, Armed, Clock, Dur> {
// since the timer is running, _is_expired() will return a value
while !self._is_expired() {}

TimerBuilder::<param::None, param::None, Clock, Dur>::new()
.set_duration(self.duration.unwrap())
}

/// Check whether the timer has expired
Expand Down Expand Up @@ -256,12 +259,20 @@ mod test {
init_start_time();

// WHEN blocking on a timer
Clock::new_timer().set_duration(1.seconds()).start().wait();
let timer = Clock::new_timer().set_duration(1.seconds()).start().wait();

// THEN the block occurs for _at least_ the given duration
unsafe {
assert!(Seconds::<i32>::try_from(START.unwrap().elapsed()).unwrap() >= 1.seconds());
}

// WHEN blocking on a timer
timer.start().wait();

// THEN the block occurs for _at least_ the given duration
unsafe {
assert!(Seconds::<i32>::try_from(START.unwrap().elapsed()).unwrap() >= 2.seconds());
}
}

#[test]
Expand Down

0 comments on commit 9ced1a8

Please sign in to comment.