嵌套宏未正确扩展
Nested Macro not expanding correctly
我正在尝试嵌套宏调用以简化 rust 和 c ffi 类型之间的转换。
内部宏从类型名称生成对转换函数的调用,如下所示:
macro_rules! type_conversion {
($arg_name:ident, bool, BOOL) => (crate::type_wrappers::type_conversion::convert_rust_bool($arg_name));
($arg_name:ident, BOOL, bool) => (crate::type_wrappers::type_conversion::convert_c_bool($arg_name));
}
外部宏现在使用它来生成进一步的转换。
它基本上是这样工作的:
macro_rules! outer {
($arg_name:ident, $type1:ty, $type2:ty) => (type_conversion!($arg_name, $type1, $type2));
}
现在,当我尝试像这样使用外部宏时:
fn outer() {
outer!(hello_world, bool, BOOL);
}
我收到这个错误:
error: no rules expected the token `bool`
--> src\type_wrappers\type_conversion.rs:252:77
|
202 | macro_rules! type_conversion {
| ---------------------------- when calling this macro
...
252 | ($arg_name:ident, $type1:ty, $type2:ty) => (type_conversion!($arg_name, $type1, $type2));
| ^^^^^^ no rules expected this token in macro call
...
256 | outer!(hello_world, bool, BOOL);
| ------------------------------- in this macro invocation
我无法理解这个错误,尤其是因为 CLions 宏扩展工具将其扩展为:
type_conversion!(hello_world , bool , BOOL )
这是我所期望的,也是错误提示的内容。
这些宏的错误在哪里?
我猜想 ty
宏类型无法转换为标识符(bool
和 BOOL
参数是)并因此匹配。所以使用 ident
类型它有效 (playground):
macro_rules! outer {
($arg_name:ident, $type1:ident, $type2:ident) => {
type_conversion!($arg_name, $type1, $type2)
};
}
我正在尝试嵌套宏调用以简化 rust 和 c ffi 类型之间的转换。
内部宏从类型名称生成对转换函数的调用,如下所示:
macro_rules! type_conversion {
($arg_name:ident, bool, BOOL) => (crate::type_wrappers::type_conversion::convert_rust_bool($arg_name));
($arg_name:ident, BOOL, bool) => (crate::type_wrappers::type_conversion::convert_c_bool($arg_name));
}
外部宏现在使用它来生成进一步的转换。 它基本上是这样工作的:
macro_rules! outer {
($arg_name:ident, $type1:ty, $type2:ty) => (type_conversion!($arg_name, $type1, $type2));
}
现在,当我尝试像这样使用外部宏时:
fn outer() {
outer!(hello_world, bool, BOOL);
}
我收到这个错误:
error: no rules expected the token `bool`
--> src\type_wrappers\type_conversion.rs:252:77
|
202 | macro_rules! type_conversion {
| ---------------------------- when calling this macro
...
252 | ($arg_name:ident, $type1:ty, $type2:ty) => (type_conversion!($arg_name, $type1, $type2));
| ^^^^^^ no rules expected this token in macro call
...
256 | outer!(hello_world, bool, BOOL);
| ------------------------------- in this macro invocation
我无法理解这个错误,尤其是因为 CLions 宏扩展工具将其扩展为:
type_conversion!(hello_world , bool , BOOL )
这是我所期望的,也是错误提示的内容。
这些宏的错误在哪里?
我猜想 ty
宏类型无法转换为标识符(bool
和 BOOL
参数是)并因此匹配。所以使用 ident
类型它有效 (playground):
macro_rules! outer {
($arg_name:ident, $type1:ident, $type2:ident) => {
type_conversion!($arg_name, $type1, $type2)
};
}