在尝试回答这个问题时,我写了这段代码:
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);
}
Run Code Online (Sandbox Code Playgroud)
这无法编译并出现以下错误:
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);
}
Run Code Online (Sandbox Code Playgroud)
根据这些 答案,即使类型S1不符合impl特征边界,当编译器无法确定某些未来的毯子impl会导致S1突然匹配边界时,也会发生此错误。
但是,在这种情况下,不可能S1实现MyTraitwithA == B因为它已经实现MyTrait了A != B。该错误是否是当前编译器实现的限制,可以想象在以后的某个时间点会被解除,还是我遗漏了其他东西?
| 归档时间: |
|
| 查看次数: |
128 次 |
| 最近记录: |