https://github.com/quark-zju/stateful_macro_rules
Rust `macro_rules!` with states
https://github.com/quark-zju/stateful_macro_rules
macros rust
Last synced: 2 months ago
JSON representation
Rust `macro_rules!` with states
- Host: GitHub
- URL: https://github.com/quark-zju/stateful_macro_rules
- Owner: quark-zju
- License: mit
- Created: 2021-01-17T01:20:16.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-01-17T04:32:30.000Z (over 4 years ago)
- Last Synced: 2025-03-01T23:28:00.010Z (3 months ago)
- Topics: macros, rust
- Language: Rust
- Homepage:
- Size: 17.6 KB
- Stars: 1
- Watchers: 5
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# stateful_macro_rules
[](https://docs.rs/stateful_macro_rules)
Rust `macro_rules!` with states.
## Example
Define a stateful macro:
```rust
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:
```rust
constants! { A, B, C, D = 10, E, F, }assert_eq!(A, 0);
assert_eq!(C, 2);
assert_eq!(F, 12);
```Refer to [the documentation](https://docs.rs/stateful_macro_rules) and [macro_examples.rs](test_examples/macro_examples/macro_examples.rs) for more examples.