[英]How to pass a macro containing multiple items into a macro?
鉴于这个扩展多个项目的简单宏,如何将宏作为参数?
macro_rules! print_structs {
($($t:ty)*) => ($(
println!("{:?}", TypeId::of::<$t>());
)*)
}
// expands one println per type!
print_structs! { i8 i16 usize String }
如何传入预定义的类型宏?
非工作宏的示例:
macro_rules! some_types {
() => {
i8 i16 usize String
}
}
print_structs! { some_types!() }
请参阅play.rust-lang.org示例,取消注释UNCOMMENT TO TEST
行以查看问题。
给出错误: macro expansion ignores token `i16` and any following
我还尝试将列表放在一个文件中,例如:
print_structs! {
include!("some_types.in")
}
...但是这给出了一个错误: expected type, found `include!("../struct_list.rs")`
从这看起来,似乎不可能使用宏或include
扩展宏内的列表。
虽然代码生成是一种选择,但它非常复杂,所以会将其排除在此答案之外。
可以通过交换宏使用来获得类似的功能,而不是将列表传递到宏中,将宏名称传递给通用宏,使用列表扩展它。
下面是一个有效的例子:
macro_rules! print_structs {
($($t:ty)*) => ($(
println!("{:?}", ::std::any::TypeId::of::<$t>());
)*)
}
macro_rules! apply_macro_to_structs {
($macro_id:ident) => {
$macro_id! {
i8 i16 usize String
}
}
}
fn test_a() {
// expands one println per type!
print_structs! { i8 i16 usize String }
}
fn test_b() {
// expand using a macro
apply_macro_to_structs!(print_structs);
}
fn main() {
test_a();
test_b();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.