[][src]Macro embedded_timeout_macros::repeat_timeout

macro_rules! repeat_timeout {
    ($timer:expr, $op:expr, $on_success:expr, $on_error:expr,) => { ... };
}

Repeats an operation until a timer times out

Expects four arguments:

  • A timer that implements embedded_hal::timer::CountDown
  • An expression that evaluates to Result<T, E> (the operation)
  • A closure that will be called every time the operation succeeds This closure is expected to take an argument of type T. The return value is ignored.
  • A closure that will be called every time the operation fails This closure is expected to take an argument of type E. The return value is ignored.

This will keep repeating the operation until the timer runs out, no matter whether it suceeds or fails.

Example

use embedded_timeout_macros::{
    repeat_timeout,
    TimeoutError,
};

repeat_timeout!(
    &mut timer,
    {
        // The macro will keep evaluation this expression repeatedly until
        // the timer times out.
        //
        // We can do anything that returns `Result` here. For this simple
        // example, we just return `Ok`.
        Ok(())

        // We could also return an error.
        // Err("This is an error")
    },
    |result: ()| {
        // will be called by the macro, if the expression returns `Ok`
    },
    |error: &str| {
        // will be called by the macro, if the expression returns `Err`
    },
);