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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/// Add operations: `x += y`, `x -= y`, `x *= y` and `x /= y`.
pub trait OperationsExtension {
type Output;
/**
Additional assignment method
`x += y <=> x.addition(y)`
```
use claudiofsr_lib::OperationsExtension;
let mut count: u64 = 0;
while count.addition(1) <= 5 {
println!("do something");
println!("count = {count}\n");
}
let result = 2 + count.addition(4);
assert_eq!(count, 10);
assert_eq!(result, 12);
```
*/
fn addition(&mut self, other: Self) -> Self;
/**
Subtraction assignment method
`x -= y <=> x.subtraction(y)`
```
use claudiofsr_lib::OperationsExtension;
let mut count: i16 = 5;
while count.subtraction(1) > 0 {
println!("do something");
println!("count = {count}\n");
}
let result = 8 + count;
assert_eq!(count, 0);
assert_eq!(result, 8);
```
*/
fn subtraction(&mut self, other: Self) -> Self;
/**
Multiplication assignment method
`x *= y <=> x.multiply(y)`
```
use claudiofsr_lib::OperationsExtension;
let mut number = 2;
let result = 3 + number.multiply(5);
assert_eq!(number, 10);
assert_eq!(result, 13);
```
*/
fn multiply(&mut self, other: Self) -> Self;
/**
Division assignment method
`x /= y <=> x.divide(y)`
```
use claudiofsr_lib::OperationsExtension;
let mut number = 16;
let result = 7 + number.divide(5);
assert_eq!(number, 3);
assert_eq!(result, 10);
```
*/
fn divide(&mut self, other: Self) -> Self;
}
impl<T> OperationsExtension for T
where
T: Copy + std::ops::AddAssign + std::ops::SubAssign + std::ops::MulAssign + std::ops::DivAssign,
{
type Output = Self;
fn addition(&mut self, other: Self) -> Self {
*self += other;
*self
}
fn subtraction(&mut self, other: Self) -> Self {
*self -= other;
*self
}
fn multiply(&mut self, other: Self) -> Self {
*self *= other;
*self
}
fn divide(&mut self, other: Self) -> Self {
*self /= other;
*self
}
}