我处理了几个具有关联类型的特征:
trait Foo {
type FooType;
}
trait Bar {
type BarType;
}
trait Baz {
type BazType;
}
我有一个函数,我需要绑定这些关联的类型。我可以这样做(游乐场(:
fn do_the_thing<T>(_: T)
where
T: Foo,
T::FooType: Bar,
<T::FooType as Bar>::BarType: Baz,
<<T::FooType as Bar>::BarType as Baz>::BazType: Clone,
{}
这有效,但非常冗长。一个问题是我需要使用<Type as Trait>
语法来消除一些路径的歧义,尽管这不是必需的。此问题已在此处报告。
我想知道是否可以缩短上述函数的定义。我想,也许可以将所有边界合并为一个:
fn do_the_thing<T>(_: T)
where
T: Foo<FooType: Bar<BarType: Baz<BazType: Clone>>>,
{}
但这会导致语法错误:
error: expected one of `!`, `(`, `+`, `,`, `::`, `<`, or `>`, found `:`
--> src/main.rs:16:19
|
16 | T: Foo<FooType: Bar<BarType: Baz<BazType: Clone>>>,
| ^ expected one of 7 possible tokens here
有没有办法以某种方式压缩边界?
现在可以通过不稳定功能associated_type_bounds
(跟踪问题(来实现这一点。
#![feature(associated_type_bounds)]
fn do_the_thing<T>(_: T)
where
T: Foo<FooType: Bar<BarType: Baz<BazType: Clone>>>,
{}
(游乐场(