macro-stateful

Crates.iomacro-stateful
lib.rsmacro-stateful
version0.1.0
created_at2024-12-25 03:17:20.192482+00
updated_at2024-12-25 03:17:20.192482+00
descriptionA library to help record state in a global scope
homepage
repository
max_upload_size
id1494716
size5,411
XMH (xmh0511)

documentation

README

Recording state via the global scope variable

Usage:

Using the define_state macro, define the state with a unique name and specify its type. Using set_state to modify the state, the parameter type in the callback is Option<T> where T is the type specified for the state.

use macro_stateful::{define_state, set_state,take_out};

set_state! {UniqueState,|v|{
    let v = v.as_mut().unwrap();
    println!("invoke dynamically 1 times, previous: {:?}",v);
    *v = 1;
}}

set_state! {UniqueState,|v|{
    let v = v.as_mut().unwrap();
    println!("invoke dynamically 2 times, previous: {:?}",v);
    *v = 2;
}}

set_state! {UniqueState,|v|{
    let v = v.as_mut().unwrap();
    println!("invoke dynamically 3 times, previous: {:?}",v);
    *v = 3;
}}

define_state! {UniqueState:i32 = {
    println!("init");
    0
}}

fn main() {
    let r = take_out!(UniqueState);
    println!("{r:?}");
}

Commit count: 0

cargo fmt