derive_more
derive_more copied to clipboard
Implement binary operator traits for &T too
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!
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
I think this makes sense. I think this could be implemented using an add #[add(ref)] attribute, like we do for other derives.