如何将可见性令牌传递给位标志!宏



我使用的是bitflags机箱。我想为我的类型自动生成一些其他实现,所以我尝试从另一个宏调用bitflags!宏。在我希望能够处理可见性令牌之前,这一直很好。

我尝试了以下代码。

游乐场

use bitflags::bitflags;
macro_rules! my_bitflags {
(
$(#[$outer:meta])*
$vis:vis struct $name:ident: $ty:ty {
$(
$(#[$inner:ident $($args:tt)*])*
const $flag:ident = $value:expr;
)+
}
) => {
bitflags! {
$(#[$outer])*
$vis struct $name: $ty {
$(
$(#[$inner $($args)*])*
const $flag = $value;
)+
}
}

// other implementations on $name here
// 
}
}

my_bitflags! {
pub struct Flags: u32 {
const A = 0x1;
const B = 0x2;
const C = 0x4;
}
}

我本以为这会起作用,但我得到了以下错误

error: no rules expected the token `pub `

这是bitflags!宏的问题吗?还是我传递可见性令牌不正确?

bitflags!中的$vis被定义为令牌树(tt(,而不是可见性(vis(。能见度只有";最近";引入到rustc中,并且CCD_ 7宏通过不重新定义CCD_。

您需要在my_bitflags!中将$vis:vis更改为$vis:tt,以便它可以扩展为bitflags!

最新更新