Is it possible to write impl on both general case and special case?

I am making a type wrapper MyType for internal type T, and MyType can deref to T. When doing some related impl for traits.There are three cases.

  1. impl PartialEq for MyType
  2. impl PartialEq<ExternalType> for MyType
  3. impl PartialEq<MyType> for ExternalType

I have done the first case. And due to foreign trait for non-local type rule, I had to do the third case one by one.

But for second case, apparently impl<A: PartialEq<MyType>> PartialEq<A> for MyType {} conflicts with the first case. Is there a way to make this possible, somehow rule out MyType in A?

No, this is not possible. There are lots of cases where it would be useful, but it turns out to be hard to implement correctly — the name of the general form of this idea is “specialization”.

1 Like

It's not possible in stable rust but there is still min_specialization that is sound

1 Like