[][src]Macro cb_fut::once

macro_rules! once {
    ($func_name: ident($($params: expr),*, ->($($c_params: ident),*))) => { ... };
    ($func_name: ident(->($($c_params: ident),*), $($params: expr),*)) => { ... };
    ($func_name: ident($($params: expr),+, ->($($c_params: ident),*), $($more_params: expr),+)) => { ... };
}

Turn a function call that take a single callback function and return nothing into a function call without callback that return a future value.

This macro can be used when the function invoke a callback only once to return a value. If the callback will be called multiple times, use macro stream instead.

The function call signature need to have a placeholder for macro to identify a callback parameter. To make it reflect to typical Rust syntax, the callback placeholder is ->(a) for a callback that take single parameter. The reason to choose (a) instead of |a| is because the return Future from this macro will return a (a) tuple thus ->(a) just like regular function return signature but with identifier instead of type.

Example usage:

 
fn func(v: i32, cb: impl FnOnce(i32, i32)) {
   std::thread::sleep(std::time::Duration::from_secs(2));
   cb(v, v * 2)
}
 
let (a, b) = futures::executor::block_on(cb_fut::once!(func(2 + 3, ->(a, b))));
assert_eq!(5, a);
assert_eq!(10, b);