Skip to content

Derive AddRef, SubRef, ..., binary ops with reference &rhs types #492

@ibarrond

Description

@ibarrond

Currently we have derives for Add,Sub, etc...

What would it take to implement derives for traits with &rhs? E.g.,

#[derive(Add, AddRef)]
pub struct MyType{
   a: usize,
   b: u128,
}

// Expansion of Add macro
// =================================

#[automatically_derived]
impl derive_more::Add for MyType {
    type Output = MyType;
    #[inline]
    #[track_caller]
    fn add(self, rhs: MyType) -> MyType {
        MyType {
            a: self.a.add(rhs.a),
            b: self.b.add(rhs.b),
        }
    }
}

// Potential expansion of AddRef macro
// =================================

#[automatically_derived]
impl derive_more::Add<&MyType> for MyType {
    type Output = MyType;
    #[inline]
    #[track_caller]
    fn add(self, rhs: &MyType) -> MyType {
        MyType {
            a: self.a.add(&rhs.a),
            b: self.b.add(&rhs.b),
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions