Only traits defined in the current crate


#1

So I was trying to overload some operators and then this gave me a compile error:

impl<C> PartialEq<SymMonomial> for C where C: CoefficientRequisites{
    fn eq(&self, other: &SymMonomial) -> bool {
        other.eq(self)
    }
}

The exact error is:

Compiling symints v0.0.1 (file:///media/hdd/work/metadiff/symints)
error[E0210]: type parameter `C` must be used as the type parameter for some local type (e.g. `MyStruct<T>`); only traits defined in the current crate can be implemented for a type parameter
   --> src/lib.rs:214:1
    |
214 | impl<C> PartialEq<SymMonomial> for C where C: CoefficientRequisites{
    | ^

Could someone clarify to me why I can’t implement this for C?


#2

Have you read the explanation in the error index?
https://doc.rust-lang.org/error-index.html#E0210


#3

Yes, but I was looking for an explanation as to why this is the cases that we can’t do this in the first place? This prevents ergonomic operator overloading, rather I need to implement that trait for about 10 primitive types explicitly.


#4

Looks like there are a bunch of details about which impls are allowable in RFC 1023:

As I understand it, it’s basically that you’re not allowed to do it if it might conflict with someone else doing a similar thing.

For example, what if I had done this?

impl<C> PartialEq<SymMonomial> for C where C: MyOtherTrait {
    fn eq(&self, other: &SymMonomial) -> bool {
        other.eq(self)
    }
}

That would make any type ambiguous if if was CoefficientRequisites + MyOtherTrait, as it’d have two implementations for PartialEq.