[][src]Macro embedded_timeout_macros::block_timeout

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

Blocks on a non-blocking operation until a timer times out

Expects two arguments:

  • A timer that implements embedded_hal::timer::CountDown
  • An expression that evaluates to nb::Result<T, E>

Evaluates the expression and returns Result<T, TimeoutError<E>>.

Example

use embedded_timeout_macros::{
    block_timeout,
    TimeoutError,
};

let result: Result<(), TimeoutError<()>> = block_timeout!(
    &mut timer,
    {
        // The macro will keep evaluation this expression repeatedly until
        // it returns `Ok` or until the timer times out.
        //
        // We can do anything that returns `nb::Result` here. For this
        // simple example, we just return `Ok`.
        Ok(())
    }
);

match result {
    Ok(()) => {
        // success
    }
    Err(TimeoutError::Timeout) => {
        // the operation timed out
    }
    Err(TimeoutError::Other(error)) => {
        // the operation returned another error
    }
}