macro-stateful 0.1.0

A library to help record state in a global scope
Documentation
  • Coverage
  • 0%
    0 out of 5 items documented0 out of 0 items with examples
  • Size
  • Source code size: 4.94 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.11 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • xmh0511

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:?}");
}