[][src]Macro embedded_timeout_macros::repeat_timeout

macro_rules! repeat_timeout {
    (
        $timer:expr,
        $op:expr,
        ($result:ident) $on_success:expr;
        ($error:ident) $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 pseudo-closure that will be called every time the operation succeeds This pseudo-closure is expected to take an argument of type T. The return value is ignored.
  • A pseudo-closure that will be called every time the operation fails This pseudo-closure is expected to take an argument of type E. The return value is ignored.

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

It uses a loop to do that, which is breaks from when the timer runs out. Any of the expressions passed into the macro, the main expression, as well as the two pseudo-closures, can employ break and continue to manipulate that loop.

Example

use embedded_timeout_macros::{
    repeat_timeout,
    TimeoutError,
};

repeat_timeout!(
    &mut timer,
    {
        // The macro will keep evaluating 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")
    },
    // Here's a pseudo-closure with an argument in parentheses, which we can
    // name freely, followed by an expression whose return value is ignored.
    (result) {
        // The macro will evaluate this expression, if the main expression
        // above returns `Ok`. `result`, which we've named in the
        // parentheses above, will be whatever the contents of the `Ok` are.
        let result: () = result;
    };
    (error) {
        // will be called by the macro, if the expression returns `Err`
        let error: &'static str = error;
    };
);