#boilerplate #macro #variables #local #calls #retain

retained

Retain local variables between function calls

9 releases (4 breaking)

new 0.4.0 Jun 21, 2024
0.3.0 Jun 20, 2024
0.2.0 Jun 7, 2024
0.1.0 Jun 6, 2024
0.0.4 Jun 5, 2024

#86 in Caching

Download history 120/week @ 2024-05-26 704/week @ 2024-06-02 21/week @ 2024-06-09 252/week @ 2024-06-16

1,097 downloads per month

MIT/Apache

4KB

retained

Documentation

Keep local variables between repeated function calls using simple macro.

This crate is no_std.

Usage

use retained::retained;

#[retained(DrawState)]
fn draw() {
    #[retained]
    let ref mut check_box: CheckBox = CheckBox::new(/* checked */ false);
    check_box.draw();
}

fn draw_loop() {
    let mut state = DrawState::new();

    loop {
        draw(&mut state);
    }
}

Without retained, Checkbox's states would be reset on every draw call. By using retained, local variable check_box is kept inside DrawState struct. And draw function gets additional &mut DrawState argument.

Examples

See examples for simple example and egui demo ported using retained.

License

This crate is licensed under MIT OR Apache-2.0

Dependencies

~315–780KB
~19K SLoC