Suppose I have following code
trait TraitA {
type Marker;
}
struct MarkerA;
struct MarkerB;
struct Wrapper<T>(T);
impl <T: TraitA<Marker = MarkerA>> SomeOtherTrait for Wrapper<T>{}
impl <T: TraitA<Marker = MarkerB>> SomeOtherTrait for Wrapper<T>{}
It fails to compile Rust Playground
Standard Error
Compiling playground v0.0.1 (/playground)
error[E0119]: conflicting implementations of trait `SomeOtherTrait` for type `Wrapper<_>`:
--> src/lib.rs:12:1
|
11 | impl <T: TraitA<Marker = MarkerA>> SomeOtherTrait for Wrapper<T>{}
| ---------------------------------------------------------------- first implementation here
12 | impl <T: TraitA<Marker = MarkerB>> SomeOtherTrait for Wrapper<T>{}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Wrapper<_>`
error: aborting due to previous error
But my understand is as long as the type T
should have only one implementation for TraitA
, so that any T: TraitA<Marker = MarkerA>
should not have T: TraitA<Marker = MarkerB>
satisified at the same time. So there shouldn't be any conflict impls.
So I am wondering if this is a current type cheker limitation or I was missing something? If this is some current type checker's limit, will chalk helps on this?
I know there's an work around by introducing generic parameter to SomeOtherTrait
impl <T: TraitA<Marker = MarkerA>> SomeOtherTrait<MarkerA> for Wrapper<T>{}
impl <T: TraitA<Marker = MarkerB>> SomeOtherTrait<MarkerB> for Wrapper<T>{}
But do we have any solution other than this, as the work around seems logically weird.
Thanks in advance!