项目作者: quark-zju

项目描述 :
Rust `macro_rules!` with states
高级语言: Rust
项目地址: git://github.com/quark-zju/stateful_macro_rules.git
创建时间: 2021-01-17T01:20:16Z
项目社区:https://github.com/quark-zju/stateful_macro_rules

开源协议:MIT License

下载


stateful_macro_rules

Documentation
build

Rust macro_rules! with states.

Example

Define a stateful macro:

  1. stateful_macro_rules! {
  2. /// Auto-incremental i32 constants from 0.
  3. constants(
  4. next: ($next:expr) = (0),
  5. body: ($($body:tt)*),
  6. ) {
  7. $($body)*
  8. };
  9. ($i:ident = $v:expr, ...) => {
  10. body.append(const $i: i32 = $v;);
  11. next.set($v + 1);
  12. };
  13. ($i:ident, ...) => {
  14. body.append(const $i: i32 = $next;);
  15. next.set($next + 1);
  16. };
  17. }

Use the macro:

  1. constants! { A, B, C, D = 10, E, F, }
  2. assert_eq!(A, 0);
  3. assert_eq!(C, 2);
  4. assert_eq!(F, 12);

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