Hello everyone, I'm facing a limitation from the rust compiler I do not find fair, could someone explain me why this cannot be done ?
When adding method to a generic struct specialized over a concrete type, everything works fine as usual:
struct MyStruct<T> {
content: T,
}
trait MyTrait {
type U;
}
impl MyStruct<u32> {
fn method() {}
}
impl MyStruct<u64> {
fn method() {}
}
no compilation error
What I am trying is doing the same but with a trait instead of a concrete type.
struct MyStruct<T> {
content: T,
}
trait MyTrait {
type U;
}
impl<A: MyTrait<U=u32>> MyStruct<A> {
fn method() {}
}
impl<A: MyTrait<U=u64>> MyStruct<A> {
fn method() {}
}
As you can see, MyStruct<MyTrait<U=u32>>
and MyStruct<MyTrait<U=U64>>
cannot be the same type since MyTrait<U=u32>
and MyTrait<U=u64>
cannot be implemented for the same type
One can easily check that by commenting the second impl
and try to call MyStruct<MyTrait<U=u64>>::method
. The compiler will say it doesn't exist, just as expected.
Still the compiler is complaining about duplicates
error[E0592]: duplicate definitions with name `method`
--> src/main.rs:12:5
|
12 | fn method() {}
| ^^^^^^^^^^^ duplicate definitions for `method`
...
16 | fn method() {}
| ----------- other definition for `method`
For more information about this error, try `rustc --explain E0592`.