Skip to main content

relp_num/rational/big/
with_binary.rs

1use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
2
3use crate::fixed::Binary;
4use crate::fixed::One;
5use crate::rational::big::Big;
6
7impl<const S: usize> From<Binary> for Big<S> {
8    #[inline]
9    fn from(from: Binary) -> Self {
10        <Self as From<&Binary>>::from(&from)
11    }
12}
13
14impl<const S: usize> From<&Binary> for Big<S> {
15    #[inline]
16    fn from(from: &Binary) -> Self {
17        match from {
18            Binary::Zero => <Self as num_traits::Zero>::zero(),
19            Binary::One => <Self as num_traits::One>::one(),
20        }
21    }
22}
23
24impl<const S: usize> Add<Binary> for Big<S> {
25    type Output = Self;
26
27    #[inline]
28    fn add(mut self, rhs: Binary) -> Self::Output {
29        AddAssign::add_assign(&mut self, rhs);
30        self
31    }
32}
33
34impl<const S: usize> Add<&Binary> for Big<S> {
35    type Output = Big<S>;
36
37    #[inline]
38    fn add(mut self, rhs: &Binary) -> Self::Output {
39        AddAssign::add_assign(&mut self, rhs);
40        self
41    }
42}
43
44impl<const S: usize> AddAssign<Binary> for Big<S> {
45    #[inline]
46    fn add_assign(&mut self, rhs: Binary) {
47        AddAssign::add_assign(self, &rhs);
48    }
49}
50
51impl<const S: usize> AddAssign<&Binary> for Big<S> {
52    #[inline]
53    fn add_assign(&mut self, rhs: &Binary) {
54        match rhs {
55            Binary::Zero => {}
56            Binary::One => *self += One,
57        }
58    }
59}
60
61impl<const S: usize> Sub<Binary> for Big<S> {
62    type Output = Self;
63
64    #[inline]
65    fn sub(mut self, rhs: Binary) -> Self::Output {
66        SubAssign::sub_assign(&mut self, rhs);
67        self
68    }
69}
70
71impl<const S: usize> Sub<&Binary> for Big<S> {
72    type Output = Big<S>;
73
74    #[inline]
75    fn sub(mut self, rhs: &Binary) -> Self::Output {
76        SubAssign::sub_assign(&mut self, rhs);
77        self
78    }
79}
80
81impl<const S: usize> SubAssign<Binary> for Big<S> {
82    #[inline]
83    fn sub_assign(&mut self, rhs: Binary) {
84        SubAssign::sub_assign(self, &rhs);
85    }
86}
87
88impl<const S: usize> SubAssign<&Binary> for Big<S> {
89    #[inline]
90    fn sub_assign(&mut self, rhs: &Binary) {
91        match rhs {
92            Binary::Zero => {}
93            Binary::One => *self -= One,
94        }
95    }
96}
97
98impl<const S: usize> Mul<Binary> for Big<S> {
99    type Output = Self;
100
101    #[inline]
102    fn mul(mut self, rhs: Binary) -> Self::Output {
103        MulAssign::mul_assign(&mut self, rhs);
104        self
105    }
106}
107
108impl<const S: usize> Mul<&Binary> for Big<S> {
109    type Output = Big<S>;
110
111    #[inline]
112    fn mul(mut self, rhs: &Binary) -> Self::Output {
113        MulAssign::mul_assign(&mut self, rhs);
114        self
115    }
116}
117
118impl<const S: usize> Mul<Binary> for &Big<S> {
119    type Output = Big<S>;
120
121    #[inline]
122    fn mul(self, rhs: Binary) -> Self::Output {
123        Mul::mul(self, &rhs)
124    }
125}
126
127impl<const S: usize> Mul<&Binary> for &Big<S> {
128    type Output = Big<S>;
129
130    #[inline]
131    fn mul(self, rhs: &Binary) -> Self::Output {
132        match rhs {
133            Binary::Zero => num_traits::Zero::zero(),
134            Binary::One => self.clone(),
135        }
136    }
137}
138
139impl<const S: usize> MulAssign<Binary> for Big<S> {
140    #[inline]
141    fn mul_assign(&mut self, rhs: Binary) {
142        MulAssign::mul_assign(self, &rhs);
143    }
144}
145
146impl<const S: usize> MulAssign<&Binary> for Big<S> {
147    #[inline]
148    fn mul_assign(&mut self, rhs: &Binary) {
149        match rhs {
150            Binary::Zero => num_traits::Zero::set_zero(self),
151            Binary::One => {},
152        }
153    }
154}
155
156impl<const S: usize> Div<Binary> for Big<S> {
157    type Output = Self;
158
159    #[inline]
160    fn div(mut self, rhs: Binary) -> Self::Output {
161        DivAssign::div_assign(&mut self, rhs);
162        self
163    }
164}
165
166impl<const S: usize> Div<&Binary> for Big<S> {
167    type Output = Big<S>;
168
169    #[inline]
170    fn div(mut self, rhs: &Binary) -> Self::Output {
171        DivAssign::div_assign(&mut self, rhs);
172        self
173    }
174}
175
176impl<const S: usize> Div<Binary> for &Big<S> {
177    type Output = Big<S>;
178
179    #[inline]
180    fn div(self, rhs: Binary) -> Self::Output {
181        Div::div(self, &rhs)
182    }
183}
184
185impl<const S: usize> Div<&Binary> for &Big<S> {
186    type Output = Big<S>;
187
188    #[inline]
189    fn div(self, rhs: &Binary) -> Self::Output {
190        match rhs {
191            Binary::Zero => panic!("attempt to divide by zero"),
192            Binary::One => self.clone(),
193        }
194    }
195}
196
197impl<const S: usize> DivAssign<Binary> for Big<S> {
198    #[inline]
199    fn div_assign(&mut self, rhs: Binary) {
200        DivAssign::div_assign(self, &rhs);
201    }
202}
203
204impl<const S: usize> DivAssign<&Binary> for Big<S> {
205    #[inline]
206    fn div_assign(&mut self, rhs: &Binary) {
207        match rhs {
208            Binary::Zero => panic!("attempt to divide by zero"),
209            Binary::One => {},
210        }
211    }
212}