1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#[macro_export]
macro_rules! impl_add_assign_from_add {
($type:ty) => {
impl<T> AddAssign<T> for $type
where
T: Clone,
Self: Clone + Add<T, Output = Self>,
{
fn add_assign(
&mut self,
rhs: T,
) {
*self = self.clone() + rhs.clone();
}
}
};
}
#[cfg(test)]
mod tests {
#[test]
fn test() {
use std::ops::*;
#[derive(Clone)]
struct A(usize);
impl Add for A {
type Output = Self;
fn add(
self,
rhs: Self,
) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl_add_assign_from_add!(A);
}
}