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 Jul 1, 2020
1 parent 720ea0e commit c9ead9e
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ impl<Type, Clock: crate::Clock, Dur: Duration> Timer<Type, Running, Clock, Dur>

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

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

/// Check whether the timer has expired
Expand Down Expand Up @@ -224,12 +226,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 c9ead9e

Please sign in to comment.