Macro zng_app::handler::hn_once

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

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

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

§Examples

The example captures data by move and then destroys it in the first call, this cannot be done using hn! because the data needs to be available for all event calls. In this case the closure is only called once, subsequent events are ignored by the handler.

let data = vec![1, 2, 3];
on_click = hn_once!(|_| {
    for i in data {
        print!("{i}, ");
    }
});

Other then declaring a FnOnce this macro behaves like hn!, so the same considerations apply. You can clone-move variables, the type of the input is the event arguments and must be annotated.

let data = vec![1, 2, 3];
on_click = hn_once!(data, |args: &ClickArgs| {
    drop(data);
});

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