我正在尝试匹配泛型类型的特征边界:
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() {}
Run Code Online (Sandbox Code Playgroud)
不幸的是,如果我理解得很好,匹配特征的唯一方法是片段tt
,但这个片段几乎可以匹配任何东西,所以无论我做什么,我都会收到错误:
error: local ambiguity: multiple parsing options: built-in NTs tt ('bound') or 1 other option.
Run Code Online (Sandbox Code Playgroud)
我该如何匹配这段代码?
请注意,我不需要非常优雅的东西(对于公共用户我不需要它),但当然,越优雅越好。
我能够通过将第一个边界与其余边界分开来使其匹配。
macro_rules! test {
(
where $(
$bounded_type:ident: $bound:tt $(+ $others:tt )*,
)+
) => {
// Dummy expansion for test:
struct Foo<T, U>
where $(
$bounded_type : $bound $(+ $others)*,
)+
{
t: T,
u: U
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,如果特征具有参数,则这将不起作用。