zng

Macro async_clmv_fn_once

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

Async clone move closure that can only be called once.

The macro syntax is exactly the same as async_clmv_fn!, but it does not clone variables again inside the call to move to the returned future. Because it moves the captured variables to the returned Future, the closure can only be FnOnce.

ยงExamples

In the example bar is cloned into the closure and then moved to the future generated by the closure.

async fn foo<F: Future<Output=()>, H: FnOnce(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| async move {
        std::future::ready(()).await;
        if p { println!("cloned: {bar}") }
    }
});