stateful_macro_rules

Crates.iostateful_macro_rules
lib.rsstateful_macro_rules
version0.2.0
sourcesrc
created_at2021-01-17 03:03:48.72896
updated_at2021-01-17 03:03:48.72896
descriptionGenerate macro_rules macros that have states.
homepage
repositoryhttps://github.com/quark-zju/stateful_macro_rules
max_upload_size
id343011
size33,324
sig-rust-libs (github:mia-platform:sig-rust-libs)

documentation

README

stateful_macro_rules

Documentation build

Rust macro_rules! with states.

Example

Define a stateful macro:

stateful_macro_rules! {
    /// Auto-incremental i32 constants from 0.
    constants(
        next: ($next:expr) = (0),
        body: ($($body:tt)*),
    ) {
        $($body)*
    };

    ($i:ident = $v:expr, ...) => {
        body.append(const $i: i32 = $v;);
        next.set($v + 1);
    };

    ($i:ident, ...) => {
        body.append(const $i: i32 = $next;);
        next.set($next + 1);
    };
}

Use the macro:

constants! { A, B, C, D = 10, E, F, }

assert_eq!(A, 0);
assert_eq!(C, 2);
assert_eq!(F, 12);

Refer to the documentation and macro_examples.rs for more examples.

Commit count: 5

cargo fmt