relp_num/rational/small/ops/
with_zero.rs

1use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
2
3use crate::{Rational128, Rational16, Rational32, Rational64, Rational8};
4use crate::zero::Zero;
5
6macro_rules! impls {
7    ($name:ty) => {
8        impl From<Zero> for $name {
9            #[inline]
10            #[must_use]
11            fn from(_: Zero) -> Self {
12                num_traits::Zero::zero()
13            }
14        }
15
16        impl From<&Zero> for $name {
17            #[inline]
18            #[must_use]
19            fn from(_: &Zero) -> Self {
20                num_traits::Zero::zero()
21            }
22        }
23
24        impl Add<Zero> for $name {
25            type Output = Self;
26
27            #[inline]
28            #[must_use]
29            fn add(mut self, _: Zero) -> Self::Output {
30                AddAssign::add_assign(&mut self, Zero);
31                self
32            }
33        }
34
35        impl Add<&Zero> for $name {
36            type Output = Self;
37
38            #[inline]
39            #[must_use]
40            fn add(mut self, _: &Zero) -> Self::Output {
41                AddAssign::add_assign(&mut self, &Zero);
42                self
43            }
44        }
45
46        impl AddAssign<&Zero> for $name {
47            #[inline]
48            fn add_assign(&mut self, _: &Zero) {
49            }
50        }
51
52        impl AddAssign<Zero> for $name {
53            #[inline]
54            fn add_assign(&mut self, _: Zero) {
55            }
56        }
57
58        impl Sub<Zero> for $name {
59            type Output = Self;
60
61            #[must_use]
62            #[inline]
63            fn sub(mut self, _: Zero) -> Self::Output {
64                SubAssign::sub_assign(&mut self, Zero);
65                self
66            }
67        }
68
69        impl Sub<&Zero> for $name {
70            type Output = Self;
71
72            #[must_use]
73            #[inline]
74            fn sub(mut self, _: &Zero) -> Self::Output {
75                SubAssign::sub_assign(&mut self, Zero);
76                self
77            }
78        }
79
80        impl SubAssign<Zero> for $name {
81            #[inline]
82            fn sub_assign(&mut self, _: Zero) {
83            }
84        }
85
86        impl Mul<Zero> for $name {
87            type Output = Self;
88
89            #[inline]
90            #[must_use]
91            fn mul(mut self, _: Zero) -> Self::Output {
92                MulAssign::mul_assign(&mut self, Zero);
93                self
94            }
95        }
96
97        impl Mul<&Zero> for $name {
98            type Output = Self;
99
100            #[inline]
101            #[must_use]
102            fn mul(mut self, _: &Zero) -> Self::Output {
103                MulAssign::mul_assign(&mut self, Zero);
104                self
105            }
106        }
107
108        impl Mul<&Zero> for &$name {
109            type Output = $name;
110
111            #[inline]
112            #[must_use]
113            fn mul(self, _: &Zero) -> Self::Output {
114                num_traits::Zero::zero()
115            }
116        }
117
118        impl Mul<Zero> for &$name {
119            type Output = $name;
120
121            #[inline]
122            #[must_use]
123            fn mul(self, _: Zero) -> Self::Output {
124                num_traits::Zero::zero()
125            }
126        }
127
128        impl MulAssign<Zero> for $name {
129            #[inline]
130            fn mul_assign(&mut self, _: Zero) {
131                MulAssign::mul_assign(self, &Zero)
132            }
133        }
134
135        impl MulAssign<&Zero> for $name {
136            #[inline]
137            fn mul_assign(&mut self, _: &Zero) {
138                num_traits::Zero::set_zero(self);
139            }
140        }
141    }
142}
143
144impls!(Rational8);
145impls!(Rational16);
146impls!(Rational32);
147impls!(Rational64);
148impls!(Rational128);