| Crates.io | macro-stateful |
| lib.rs | macro-stateful |
| version | 0.1.0 |
| created_at | 2024-12-25 03:17:20.192482+00 |
| updated_at | 2024-12-25 03:17:20.192482+00 |
| description | A library to help record state in a global scope |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1494716 |
| size | 5,411 |
Recording state via the global scope variable
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:?}");
}