【发布时间】:2021-04-10 19:29:50
【问题描述】:
我正在尝试编写一个宏来在 rayon 的 par_iter 和 std 的 iter 之间切换,具体取决于构建功能(可能超出了我自己,因为我还没有阅读很多关于宏的内容)。在这里,宏似乎比函数好一点,因为函数可能需要一些相对复杂的类型才能使其工作;如果我想在与如何运行迭代器有关的构建功能中添加更多变体,那么将来宏可能会更加灵活。
#[macro_export]
macro_rules! par_iter {
($($tokens:tt)*) => {
#[cfg(feature = "threaded")]
$($tokens)*.par_iter()
#[cfg(not(feature = "threaded"))]
$($tokens)*.iter()
}
}
我看到以下错误:
error: macro expansion ignores token `b_slice` and any following
--> src/util.rs:28:8
|
28 | $($tokens)*.iter();
| ^^^^^^^^^
|
::: src/counting.rs:219:9
|
219 | par_iter!(b_slice).map(WordCount::from)
| ------------------- help: you might be missing a semicolon here: `;`
| |
| caused by the macro expansion here
|
= note: the usage of `par_iter!` is likely invalid in expression context
虽然我不知道第一个错误,但我很好奇为什么需要 ; - 如何使其在表达式上下文中有效?
【问题讨论】:
-
我猜你不是想评论
#[cfg(not(feature = "threaded"))]? -
抱歉,是的,这是实验中的错误。现已修复。
标签: rust rust-macros