【发布时间】:2017-03-05 21:03:18
【问题描述】:
考虑这两个特征:
pub trait Foo {
fn new(arg: u32) -> Self;
}
pub trait Bar<P>: Foo {
fn with_parameter(arg: u32, parameter: P) -> Self;
}
我想添加一揽子 impl:
impl<T: Bar<P>, P: Default> Foo for T {
fn new(arg: u32) -> Self {
Self::with_parameter(arg, P::default())
}
}
但我得到编译器错误:
error[E0207]: the type parameter `P` is not constrained by the impl trait, self type, or predicates
--> src/lib.rs:9:17
|
9 | impl<T: Bar<P>, P: Default> Foo for T {
| ^ unconstrained type parameter
我认为我收到此错误是因为我违反了 trait coherence 规则,但我不明白这会破坏什么规则。为什么不允许这种模式?而且,更重要的是,我可以在不出错的情况下实现我想要的吗?
【问题讨论】:
标签: generics rust traits type-parameter