Macro forward_ref::forward_ref_binop[][src]

macro_rules! forward_ref_binop {
    (impl $imp:ident, $method:ident for $t:ty, $u:ty) => { ... };
}
Expand description

Extend a binary operator trait impl over refs.

Given an implementation of T op U where T and U are Copyable, implements the binary operators:

  • &T op U
  • T op &U
  • &T op &U

Examples

use core::ops::Add;
use forward_ref::forward_ref_binop;

#[derive(Clone, Copy, Debug, PartialEq)]
struct MyInt(i32);

impl Add for MyInt {
    type Output = Self;

    #[inline]
    fn add(self, rhs: Self) -> Self::Output {
        Self(self.0 + rhs.0)
    }
}

forward_ref_binop!(impl Add, add for MyInt, MyInt);

// Now addition will work for any combination of references and values.
let a = MyInt(1);
let b = MyInt(2);

assert_eq!(a + b, MyInt(3));
assert_eq!(&a + b, MyInt(3));
assert_eq!(a + &b, MyInt(3));
assert_eq!(&a + &b, MyInt(3));