Macro zng_app::handler::async_hn_once

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

Declare an async clone-move event handler that is only called once.

The macro input is a closure with optional clone-move variables, internally it uses async_clmv_fn_once! so the input is the same syntax.

§Examples

The example captures data by move and then moves it again to another thread. This is not something you can do using async_hn! because that handler expects to be called many times. We expect on_open to only be called once, so we can don’t need to capture by clone-move here just to use data.

let data = vec![1, 2, 3];
on_open = async_hn_once!(|_| {
    task::run(async move {
         for i in data {
             print!("{i}, ");
         }    
    }).await;

    println!("Done!");
});

You can still clone-move to have access to the variable after creating the handler, in this case the data will be cloned into the handler but will just be moved to the other thread, avoiding a needless clone.

let data = vec![1, 2, 3];
on_open = async_hn_once!(data, |_| {
    task::run(async move {
         for i in data {
             print!("{i}, ");
         }    
    }).await;

    println!("Done!");
});
println!("{data:?}");