Add<X> for Y for any X: ToMyNumT, Y: ToMyNumT

I have the following:

pub enum MyNum ... ;
pub trait ToMyNumT {
  fn to_my_num(&self) -> MyNum;
}

Now, I want to overload std::ops::Add where we can std::ops::Add<X> for Y for any X: ToMyNumT, Y: ToMyNumT -- is this possible?

No, you can't make a blanket impl for a trait you didn't make

1 Like

To see why this is not allowed, suppose two unrelated crates added such impls, and some type met both constraints, which would the compiler use?

Even more directly, if i32 implements ToMyNumT, then yours with X = Y = i32 conflicts with the existing Add<i32> for i32.

1 Like