当试图回答时这个问题 https://stackoverflow.com/questions/55628334/trait-function-conditional-default-implementation,我写了这段代码:
trait MyTrait {
type A;
type B;
}
trait WithFoo: MyTrait {
fn foo(a: Self::A) -> Self::B;
}
impl<T, U: MyTrait<A = T, B = T>> WithFoo for U {
fn foo(a: T) -> T {
a
}
}
struct S1;
impl MyTrait for S1 {
type A = u32;
type B = f32;
}
impl WithFoo for S1 {
fn foo<T>(a: Self::A) -> Self::B {
a as f32
}
}
struct S2;
impl MyTrait for S2 {
type A = u32;
type B = u32;
}
fn main() {
S1::foo(42);
S2::foo(42);
}
无法编译并出现以下错误:
error[E0119]: conflicting implementations of trait `WithFoo` for type `S1`:
--> src/main.rs:23:1
|
10 | impl<T, U: MyTrait<A = T, B = T>> WithFoo for U {
| ----------------------------------------------- first implementation here
...
23 | impl WithFoo for S1 {
| ^^^^^^^^^^^^^^^^^^^ conflicting implementation for `S1`
根据these https://stackoverflow.com/a/39161143/5397009 answers https://stackoverflow.com/a/45034077/5397009即使类型发生此错误S1
不适合impl
当编译器无法确定某些未来的毯子时,特征界限impl
会导致S1
突然匹配边界。
但在这种情况下不可能S1
可以实施MyTrait
with A == B
因为它已经实现了MyTrait
with A != B
。该错误是否是当前编译器实现的限制,可能会在以后的某个时刻被解除,或者我还缺少其他东西吗?
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)