zng_clone_move

Macro async_clmv_fn

source
macro_rules! async_clmv_fn {
    ($($tt:tt)+) => { ... };
}
Expand description

Async clone move closure.

The macro syntax is exactly the same as clmv!, but it expands to an async closure that captures a clone of zero or more variables and moves another clone of these variables into the returned future for each call.

§Examples

In the example bar is cloned into the closure and then it is cloned again for each future generated by the closure.

async fn foo<F: Future<Output=()>, H: FnMut(bool) -> F + 'static>(mut f: H) {
    f(true).await;
}

let bar = "Cool!".to_owned();
foo(async_clmv_fn!(bar, |p| {
    std::future::ready(()).await;
    if p { println!("cloned: {bar}") }
}));

println!("original: {bar}");

Expands to:

foo({
    let bar = bar.clone();
    move |p| {
        let bar = bar.clone();
        async move {
            std::future::ready(()).await;
            if p { println!("cloned: {bar}") }
        }
    }
});

§Once

See async_clmv_fn_once! for creating FnOnce closures.