Impl on std::ops::Add // reducing repetition

I want to write:


pub trait MyAddT {
    fn my_add(&self, rhs: &Self) -> Self;}

impl<T: MyAddT> std::ops::Add<&T> for &T {
    type Output = T;
    fn add(self, rhs: &T) -> Self::Output {
        self.my_add(rhs)}}

impl<T: MyAddT> std::ops::Add<T> for &T {
    type Output = T;
    fn add(self, rhs: T) -> Self::Output {
        self.my_add(&rhs)}}

impl<T: MyAddT> std::ops::Add<&T> for T {
    type Output = T;
    fn add(self, rhs: &T) -> Self::Output {
        self.my_add(rhs)}}

impl<T: MyAddT> std::ops::Add<T> for T {
    type Output = T;
    fn add(self, rhs: T) -> Self::Output {
        self.my_add(&rhs)}}

Is there anyway to make this sorta work? I want to say: if we can do &T + &T -> T, we should get the other 3 for free.

I think a bunch of this is waiting on specialization, so that options are available for this without conflicting with all the ones that already exist.

For now one generally just makes macros to help. For example, I used this one a while ago:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.