【发布时间】:2018-07-29 11:04:17
【问题描述】:
我正在尝试匹配泛型类型的特征边界:
macro_rules! test {
(
where $(
$bounded_type:ident: $( $bound:tt )++,
)+
) => {
// Dummy expansion for test:
struct Foo<T, U>
where $(
$bounded_type : $( $bound )++,
)+
{
t: T,
u: U
}
}
}
test! {
where
T: PartialEq + Clone,
U: PartialEq,
}
fn main() {}
不幸的是,如果我理解得很好,匹配 trait 的唯一方法是 tt 片段,但是这个片段几乎可以匹配任何东西,所以无论我做什么,都会出错:
error: local ambiguity: multiple parsing options: built-in NTs tt ('bound') or 1 other option.
如何匹配这段代码?
请注意,我不需要非常优雅的东西(我不需要公共用户),但当然,越优雅越好。
【问题讨论】:
-
我在匹配和发射类型边界方面遇到了很多问题。那是不久前的事了,但我想我最终只是做了其他事情......
-
@PeterHall Rust 团队应该为此做点什么。 IMO,宏系统应该允许轻松匹配和发出语言语法。