Rust 宏:单位类型?

Rust macros: unit type?

下面的宏:

macro_rules! generate_parse_expression_ast_data {
    ($lit:literal) => ();
}

enum Ast {
    Foo (generate_parse_expression_ast_data!("bar")),
}

出现此错误:

error: macro expansion ends with an incomplete expression: expected type
 --> src/main.rs:6:10
  |
2 |     ($lit:literal) => ();
  |                       -- in this macro arm
...
6 |     Foo (generate_parse_expression_ast_data!("bar")),
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |          |
  |          expected type
  |          this macro call doesn't expand to a type

error: aborting due to previous error

error: could not compile `playground`.

Playground link

我特别想对其中一些枚举情况使用 unit type,根据定义,这些情况应该是有效类型。我将如何做到这一点?

看起来像 known issue

除了发出 zero-sized 类型(如单元)之外,我现在可以建议的最好的方法是重构宏,以便它生成完整的变体。例如:

macro_rules! generate_parse_expression_ast_data {(
    enum $name:ident {
        $( $variant:ident($lit:literal), )+
    }
) => (
    enum $name {
        $( $variant(), )+
    }
)}

generate_parse_expression_ast_data! {
    enum Ast {
        Foo("bar"),
    }
}