derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Implement binary operator traits for &T too

Open nbigaouette opened this issue 4 years ago • 3 comments

I derive derive_more::Add for my struct, allowing me to add two owned values:

#[derive(derive_more::Add)]
struct MyStruct(i64);

fn main() {
    let a = MyStruct(1);
    let b = MyStruct(2);

   let c = a + b;

But I cannot add references. The following fails to compile:

#[derive(derive_more::Add)]
struct MyStruct(i64);

fn main() {
    let a = MyStruct(1);
    let b = MyStruct(2);

   let c = &a + b;
   let d = a + &b;
   let e = &a + &b;

which is unfortunate :(.

Is it possible already? I couldn't find in the docs...

Thank you!

nbigaouette avatar Mar 23 '21 01:03 nbigaouette

I was looking for the same thing. Maybe it could be a separate derive like AddRef to avoid bloating current users of derive_more with additional code they don't need

nico-abram avatar Mar 29 '21 03:03 nico-abram

I think this makes sense. I think this could be implemented using an add #[add(ref)] attribute, like we do for other derives.

JelteF avatar Dec 21 '23 23:12 JelteF