ruut_functions/ops/
mod.rs

1use crate::{F1D, F2D, F3D};
2use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
3mod add;
4mod div;
5mod mul;
6mod pow;
7
8// Cross operations
9macro_rules! cross_ops {
10    ($t1:ty, $t2: ty) => {
11        impl Add<$t1> for $t2 {
12            type Output = Self;
13            fn add(self, rhs: $t1) -> Self::Output {
14                Self(self.0 + rhs.0)
15            }
16        }
17        impl Sub<$t1> for $t2 {
18            type Output = Self;
19            fn sub(self, rhs: $t1) -> Self::Output {
20                Self(self.0 - rhs.0)
21            }
22        }
23        impl Mul<$t1> for $t2 {
24            type Output = Self;
25            fn mul(self, rhs: $t1) -> Self::Output {
26                Self(self.0 * rhs.0)
27            }
28        }
29        impl Div<$t1> for $t2 {
30            type Output = Self;
31            fn div(self, rhs: $t1) -> Self::Output {
32                Self(self.0 / rhs.0)
33            }
34        }
35    };
36}
37
38cross_ops!(F1D, F2D);
39cross_ops!(F1D, F3D);
40cross_ops!(F2D, F3D);
41
42// Cross operations
43macro_rules! cross_ops_assign {
44    ($t1:ty, $t2: ty) => {
45        impl AddAssign<$t1> for $t2 {
46            fn add_assign(&mut self, rhs: $t1) {
47                self.0 += rhs.0
48            }
49        }
50        impl SubAssign<$t1> for $t2 {
51            fn sub_assign(&mut self, rhs: $t1) {
52                self.0 -= rhs.0
53            }
54        }
55        impl MulAssign<$t1> for $t2 {
56            fn mul_assign(&mut self, rhs: $t1) {
57                self.0 *= rhs.0
58            }
59        }
60        impl DivAssign<$t1> for $t2 {
61            fn div_assign(&mut self, rhs: $t1) {
62                self.0 /= rhs.0
63            }
64        }
65    };
66}
67
68cross_ops_assign!(F1D, F2D);
69cross_ops_assign!(F1D, F3D);
70cross_ops_assign!(F2D, F3D);
71#[test]
72fn test_cross_ops() {
73    use crate::{f1d, f2d};
74    let f1 = f1d!("x");
75    let mut f2 = f2d!("y");
76    f2 += f1;
77    assert_eq!(f2, f2d!("x+y"))
78}