Traits used to wait and timeout in a no-std embedded system.
std: Disabled by default.
cargo add waiter-traitSee crate
- New a
WaiterorTimedWaiterimplementation. - Call
start()to get aWaiterStatusimplementation. - Call
timeout()to check if the time limit expires.Interval::intervalis usually called intimeout()before the time limit expires. It also depends on your implementation.
- Call
restart()to reset the timeout condition if necessary.
use waiter_trait::prelude::*;
fn foo(waiter: impl Waiter) {
let mut t = waiter.start();
loop {
// Wait for something.
// Reset if it's necessary.
t.restart();
if t.timeout() {
break;
}
}
}
StdWaiterandStdInterval: Need thestdfeature enabled.NonInterval: implementsIntervalthat does nothing.TickDelay: implementsDelayNs
For developers, you can choose one of the following options.
- Implement
WaiterorTimedWaiter, andWaiterStatusthen use them. - Implement
TickInstantthen useTickWaiterorTimedTickWaiter.- Simply give
NonIntervaltoWaiter, If you don't need interval. In this way, you can also useDelayNsorsleepseparately. - Or you can implement
Intervaland use it.
- Simply give
- Using
Counter, if you don't have any tick source.