[][src]Macro cb_fut::stream

macro_rules! stream {
    ($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 and return nothing into a function call without callback but return an implementation of futures::Stream called CBStream.

If the callback will be called only once to return a value, consider using macro once 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 usecase:

use futures::stream::StreamExt;
fn func(u: i32, mut cb: impl FnMut(i32, i32), v: i32) {
    for i in 0..5 {
        cb(u + i, v * i)
    }
}
let mut counter = 0;
 
futures::executor::block_on(cb_fut::stream!(func(2 * 3, ->(a, b), 2 + 3)).enumerate().for_each(|(i, fut)| {
    counter += 1;
    async move {
        let (a, b) = fut;
        assert_eq!(2 * 3 + i as i32, a);
        assert_eq!((2 + 3) * i as i32, b);
    }
}));