Skip to main content

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