penumbra_sdk_num/fixpoint/
ops.rs

1use crate::fixpoint::{Error, U128x128};
2use std::ops::{Add, Div, Mul, Sub};
3
4// There are 8 impl variants per operation:
5//
6// (       T ,        T )
7// (       T ,       &T )
8// (      &T ,        T )
9// (      &T ,       &T )
10// (Option<T>,        T )
11// (Option<T>,       &T )
12// (       T , Option<T>)
13// (      &T , Option<T>)
14//
15// We can't do (Option, Option) because of orphan rules.
16// We don't do (Option<&>, _) because the reason to do Option
17// is to do operations on outputs, which are owned.
18
19impl Add<U128x128> for U128x128 {
20    type Output = Result<U128x128, Error>;
21    fn add(self, rhs: U128x128) -> Self::Output {
22        self.checked_add(&rhs)
23    }
24}
25
26impl Add<&U128x128> for U128x128 {
27    type Output = Result<U128x128, Error>;
28    fn add(self, rhs: &U128x128) -> Self::Output {
29        self.checked_add(rhs)
30    }
31}
32
33impl Add<U128x128> for &U128x128 {
34    type Output = Result<U128x128, Error>;
35    fn add(self, rhs: U128x128) -> Self::Output {
36        self.checked_add(&rhs)
37    }
38}
39
40impl Add<&U128x128> for &U128x128 {
41    type Output = Result<U128x128, Error>;
42    fn add(self, rhs: &U128x128) -> Self::Output {
43        self.checked_add(rhs)
44    }
45}
46
47impl Add<U128x128> for Result<U128x128, Error> {
48    type Output = Result<U128x128, Error>;
49    fn add(self, rhs: U128x128) -> Self::Output {
50        self.and_then(|lhs| lhs.checked_add(&rhs))
51    }
52}
53
54impl Add<Result<U128x128, Error>> for U128x128 {
55    type Output = Result<U128x128, Error>;
56    fn add(self, rhs: Result<U128x128, Error>) -> Self::Output {
57        rhs.and_then(|rhs| self.checked_add(&rhs))
58    }
59}
60
61impl Add<&U128x128> for Result<U128x128, Error> {
62    type Output = Result<U128x128, Error>;
63    fn add(self, rhs: &U128x128) -> Self::Output {
64        self.and_then(|lhs| lhs.checked_add(rhs))
65    }
66}
67
68impl Add<Result<U128x128, Error>> for &U128x128 {
69    type Output = Result<U128x128, Error>;
70    fn add(self, rhs: Result<U128x128, Error>) -> Self::Output {
71        rhs.and_then(|rhs| self.checked_add(&rhs))
72    }
73}
74
75impl Sub<U128x128> for U128x128 {
76    type Output = Result<U128x128, Error>;
77    fn sub(self, rhs: U128x128) -> Self::Output {
78        self.checked_sub(&rhs)
79    }
80}
81
82impl Sub<&U128x128> for U128x128 {
83    type Output = Result<U128x128, Error>;
84    fn sub(self, rhs: &U128x128) -> Self::Output {
85        self.checked_sub(rhs)
86    }
87}
88
89impl Sub<U128x128> for &U128x128 {
90    type Output = Result<U128x128, Error>;
91    fn sub(self, rhs: U128x128) -> Self::Output {
92        self.checked_sub(&rhs)
93    }
94}
95
96impl Sub<&U128x128> for &U128x128 {
97    type Output = Result<U128x128, Error>;
98    fn sub(self, rhs: &U128x128) -> Self::Output {
99        self.checked_sub(rhs)
100    }
101}
102
103impl Sub<U128x128> for Result<U128x128, Error> {
104    type Output = Result<U128x128, Error>;
105    fn sub(self, rhs: U128x128) -> Self::Output {
106        self.and_then(|lhs| lhs.checked_sub(&rhs))
107    }
108}
109
110impl Sub<Result<U128x128, Error>> for U128x128 {
111    type Output = Result<U128x128, Error>;
112    fn sub(self, rhs: Result<U128x128, Error>) -> Self::Output {
113        rhs.and_then(|rhs| self.checked_sub(&rhs))
114    }
115}
116
117impl Sub<&U128x128> for Result<U128x128, Error> {
118    type Output = Result<U128x128, Error>;
119    fn sub(self, rhs: &U128x128) -> Self::Output {
120        self.and_then(|lhs| lhs.checked_sub(rhs))
121    }
122}
123
124impl Sub<Result<U128x128, Error>> for &U128x128 {
125    type Output = Result<U128x128, Error>;
126    fn sub(self, rhs: Result<U128x128, Error>) -> Self::Output {
127        rhs.and_then(|rhs| self.checked_sub(&rhs))
128    }
129}
130
131impl Mul<U128x128> for U128x128 {
132    type Output = Result<U128x128, Error>;
133    fn mul(self, rhs: U128x128) -> Self::Output {
134        self.checked_mul(&rhs)
135    }
136}
137
138impl Mul<&U128x128> for U128x128 {
139    type Output = Result<U128x128, Error>;
140    fn mul(self, rhs: &U128x128) -> Self::Output {
141        self.checked_mul(rhs)
142    }
143}
144
145impl Mul<U128x128> for &U128x128 {
146    type Output = Result<U128x128, Error>;
147    fn mul(self, rhs: U128x128) -> Self::Output {
148        self.checked_mul(&rhs)
149    }
150}
151
152impl Mul<&U128x128> for &U128x128 {
153    type Output = Result<U128x128, Error>;
154    fn mul(self, rhs: &U128x128) -> Self::Output {
155        self.checked_mul(rhs)
156    }
157}
158
159impl Mul<U128x128> for Result<U128x128, Error> {
160    type Output = Result<U128x128, Error>;
161    fn mul(self, rhs: U128x128) -> Self::Output {
162        self.and_then(|lhs| lhs.checked_mul(&rhs))
163    }
164}
165
166impl Mul<Result<U128x128, Error>> for U128x128 {
167    type Output = Result<U128x128, Error>;
168    fn mul(self, rhs: Result<U128x128, Error>) -> Self::Output {
169        rhs.and_then(|rhs| self.checked_mul(&rhs))
170    }
171}
172
173impl Mul<&U128x128> for Result<U128x128, Error> {
174    type Output = Result<U128x128, Error>;
175    fn mul(self, rhs: &U128x128) -> Self::Output {
176        self.and_then(|lhs| lhs.checked_mul(rhs))
177    }
178}
179
180impl Mul<Result<U128x128, Error>> for &U128x128 {
181    type Output = Result<U128x128, Error>;
182    fn mul(self, rhs: Result<U128x128, Error>) -> Self::Output {
183        rhs.and_then(|rhs| self.checked_mul(&rhs))
184    }
185}
186
187impl Div<U128x128> for U128x128 {
188    type Output = Result<U128x128, Error>;
189    fn div(self, rhs: U128x128) -> Self::Output {
190        self.checked_div(&rhs)
191    }
192}
193
194impl Div<&U128x128> for U128x128 {
195    type Output = Result<U128x128, Error>;
196    fn div(self, rhs: &U128x128) -> Self::Output {
197        self.checked_div(rhs)
198    }
199}
200
201impl Div<U128x128> for &U128x128 {
202    type Output = Result<U128x128, Error>;
203    fn div(self, rhs: U128x128) -> Self::Output {
204        self.checked_div(&rhs)
205    }
206}
207
208impl Div<&U128x128> for &U128x128 {
209    type Output = Result<U128x128, Error>;
210    fn div(self, rhs: &U128x128) -> Self::Output {
211        self.checked_div(rhs)
212    }
213}
214
215impl Div<U128x128> for Result<U128x128, Error> {
216    type Output = Result<U128x128, Error>;
217    fn div(self, rhs: U128x128) -> Self::Output {
218        self.and_then(|lhs| lhs.checked_div(&rhs))
219    }
220}
221
222impl Div<Result<U128x128, Error>> for U128x128 {
223    type Output = Result<U128x128, Error>;
224    fn div(self, rhs: Result<U128x128, Error>) -> Self::Output {
225        rhs.and_then(|rhs| self.checked_div(&rhs))
226    }
227}
228
229impl Div<&U128x128> for Result<U128x128, Error> {
230    type Output = Result<U128x128, Error>;
231    fn div(self, rhs: &U128x128) -> Self::Output {
232        self.and_then(|lhs| lhs.checked_div(rhs))
233    }
234}
235
236impl Div<Result<U128x128, Error>> for &U128x128 {
237    type Output = Result<U128x128, Error>;
238    fn div(self, rhs: Result<U128x128, Error>) -> Self::Output {
239        rhs.and_then(|rhs| self.checked_div(&rhs))
240    }
241}