coord_2d/
coord_arithmetic.rs

1use crate::coord::{ICoord, UCoord};
2use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
3
4impl Neg for ICoord {
5    type Output = ICoord;
6    fn neg(self) -> Self::Output {
7        ICoord {
8            x: -self.x,
9            y: -self.y,
10        }
11    }
12}
13
14impl<'a> Neg for &'a ICoord {
15    type Output = ICoord;
16    fn neg(self) -> Self::Output {
17        ICoord {
18            x: -self.x,
19            y: -self.y,
20        }
21    }
22}
23
24impl Add for ICoord {
25    type Output = ICoord;
26    fn add(self, ICoord { x, y }: ICoord) -> Self::Output {
27        ICoord {
28            x: self.x + x,
29            y: self.y + y,
30        }
31    }
32}
33
34impl<'a> Add<ICoord> for &'a ICoord {
35    type Output = ICoord;
36    fn add(self, ICoord { x, y }: ICoord) -> Self::Output {
37        ICoord {
38            x: self.x + x,
39            y: self.y + y,
40        }
41    }
42}
43
44impl<'a> Add<&'a ICoord> for ICoord {
45    type Output = ICoord;
46    fn add(self, &ICoord { x, y }: &'a ICoord) -> Self::Output {
47        ICoord {
48            x: self.x + x,
49            y: self.y + y,
50        }
51    }
52}
53
54impl<'a, 'b> Add<&'a ICoord> for &'b ICoord {
55    type Output = ICoord;
56    fn add(self, &ICoord { x, y }: &'a ICoord) -> Self::Output {
57        ICoord {
58            x: self.x + x,
59            y: self.y + y,
60        }
61    }
62}
63
64impl Add<UCoord> for ICoord {
65    type Output = ICoord;
66    fn add(self, ucoord: UCoord) -> Self::Output {
67        ICoord {
68            x: self.x + ucoord.width() as i32,
69            y: self.y + ucoord.height() as i32,
70        }
71    }
72}
73
74impl<'a> Add<UCoord> for &'a ICoord {
75    type Output = ICoord;
76    fn add(self, ucoord: UCoord) -> Self::Output {
77        ICoord {
78            x: self.x + ucoord.width() as i32,
79            y: self.y + ucoord.height() as i32,
80        }
81    }
82}
83
84impl<'a> Add<&'a UCoord> for ICoord {
85    type Output = ICoord;
86    fn add(self, ucoord: &'a UCoord) -> Self::Output {
87        ICoord {
88            x: self.x + ucoord.width() as i32,
89            y: self.y + ucoord.height() as i32,
90        }
91    }
92}
93
94impl<'a, 'b> Add<&'a UCoord> for &'b ICoord {
95    type Output = ICoord;
96    fn add(self, ucoord: &'a UCoord) -> Self::Output {
97        ICoord {
98            x: self.x + ucoord.width() as i32,
99            y: self.y + ucoord.height() as i32,
100        }
101    }
102}
103
104impl Add<ICoord> for UCoord {
105    type Output = ICoord;
106    fn add(self, ICoord { x, y }: ICoord) -> Self::Output {
107        ICoord {
108            x: self.width() as i32 + x,
109            y: self.height() as i32 + y,
110        }
111    }
112}
113
114impl<'a> Add<ICoord> for &'a UCoord {
115    type Output = ICoord;
116    fn add(self, ICoord { x, y }: ICoord) -> Self::Output {
117        ICoord {
118            x: self.width() as i32 + x,
119            y: self.height() as i32 + y,
120        }
121    }
122}
123
124impl<'a> Add<&'a ICoord> for UCoord {
125    type Output = ICoord;
126    fn add(self, &ICoord { x, y }: &'a ICoord) -> Self::Output {
127        ICoord {
128            x: self.width() as i32 + x,
129            y: self.height() as i32 + y,
130        }
131    }
132}
133
134impl<'a, 'b> Add<&'a ICoord> for &'b UCoord {
135    type Output = ICoord;
136    fn add(self, &ICoord { x, y }: &'a ICoord) -> Self::Output {
137        ICoord {
138            x: self.width() as i32 + x,
139            y: self.height() as i32 + y,
140        }
141    }
142}
143
144impl Add for UCoord {
145    type Output = UCoord;
146    fn add(self, ucoord: UCoord) -> Self::Output {
147        UCoord::new(
148            self.width() + ucoord.width(),
149            self.height() + ucoord.height(),
150        )
151    }
152}
153
154impl<'a> Add<UCoord> for &'a UCoord {
155    type Output = UCoord;
156    fn add(self, ucoord: UCoord) -> Self::Output {
157        UCoord::new(
158            self.width() + ucoord.width(),
159            self.height() + ucoord.height(),
160        )
161    }
162}
163
164impl<'a> Add<&'a UCoord> for UCoord {
165    type Output = UCoord;
166    fn add(self, ucoord: &'a UCoord) -> Self::Output {
167        UCoord::new(
168            self.width() + ucoord.width(),
169            self.height() + ucoord.height(),
170        )
171    }
172}
173
174impl<'a, 'b> Add<&'a UCoord> for &'b UCoord {
175    type Output = UCoord;
176    fn add(self, ucoord: &'a UCoord) -> Self::Output {
177        UCoord::new(
178            self.width() + ucoord.width(),
179            self.height() + ucoord.height(),
180        )
181    }
182}
183
184impl<T> AddAssign<T> for ICoord
185where
186    ICoord: Add<T, Output = ICoord>,
187{
188    fn add_assign(&mut self, rhs: T) {
189        *self = *self + rhs;
190    }
191}
192
193impl Sub for ICoord {
194    type Output = ICoord;
195    fn sub(self, ICoord { x, y }: ICoord) -> Self::Output {
196        ICoord {
197            x: self.x - x,
198            y: self.y - y,
199        }
200    }
201}
202
203impl<'a> Sub<ICoord> for &'a ICoord {
204    type Output = ICoord;
205    fn sub(self, ICoord { x, y }: ICoord) -> Self::Output {
206        ICoord {
207            x: self.x - x,
208            y: self.y - y,
209        }
210    }
211}
212
213impl<'a> Sub<&'a ICoord> for ICoord {
214    type Output = ICoord;
215    fn sub(self, &ICoord { x, y }: &'a ICoord) -> Self::Output {
216        ICoord {
217            x: self.x - x,
218            y: self.y - y,
219        }
220    }
221}
222
223impl<'a, 'b> Sub<&'a ICoord> for &'b ICoord {
224    type Output = ICoord;
225    fn sub(self, &ICoord { x, y }: &'a ICoord) -> Self::Output {
226        ICoord {
227            x: self.x - x,
228            y: self.y - y,
229        }
230    }
231}
232
233impl Sub<UCoord> for ICoord {
234    type Output = ICoord;
235    fn sub(self, ucoord: UCoord) -> Self::Output {
236        ICoord {
237            x: self.x - ucoord.width() as i32,
238            y: self.y - ucoord.height() as i32,
239        }
240    }
241}
242
243impl<'a> Sub<UCoord> for &'a ICoord {
244    type Output = ICoord;
245    fn sub(self, ucoord: UCoord) -> Self::Output {
246        ICoord {
247            x: self.x - ucoord.width() as i32,
248            y: self.y - ucoord.height() as i32,
249        }
250    }
251}
252
253impl<'a> Sub<&'a UCoord> for ICoord {
254    type Output = ICoord;
255    fn sub(self, ucoord: &'a UCoord) -> Self::Output {
256        ICoord {
257            x: self.x - ucoord.width() as i32,
258            y: self.y - ucoord.height() as i32,
259        }
260    }
261}
262
263impl<'a, 'b> Sub<&'a UCoord> for &'b ICoord {
264    type Output = ICoord;
265    fn sub(self, ucoord: &'a UCoord) -> Self::Output {
266        ICoord {
267            x: self.x - ucoord.width() as i32,
268            y: self.y - ucoord.height() as i32,
269        }
270    }
271}
272
273impl Sub<ICoord> for UCoord {
274    type Output = ICoord;
275    fn sub(self, ICoord { x, y }: ICoord) -> Self::Output {
276        ICoord {
277            x: self.width() as i32 - x,
278            y: self.height() as i32 - y,
279        }
280    }
281}
282
283impl<'a> Sub<ICoord> for &'a UCoord {
284    type Output = ICoord;
285    fn sub(self, ICoord { x, y }: ICoord) -> Self::Output {
286        ICoord {
287            x: self.width() as i32 - x,
288            y: self.height() as i32 - y,
289        }
290    }
291}
292
293impl<'a> Sub<&'a ICoord> for UCoord {
294    type Output = ICoord;
295    fn sub(self, &ICoord { x, y }: &'a ICoord) -> Self::Output {
296        ICoord {
297            x: self.width() as i32 - x,
298            y: self.height() as i32 - y,
299        }
300    }
301}
302
303impl<'a, 'b> Sub<&'a ICoord> for &'b UCoord {
304    type Output = ICoord;
305    fn sub(self, &ICoord { x, y }: &'a ICoord) -> Self::Output {
306        ICoord {
307            x: self.width() as i32 - x,
308            y: self.height() as i32 - y,
309        }
310    }
311}
312
313impl Sub for UCoord {
314    type Output = UCoord;
315    fn sub(self, ucoord: UCoord) -> Self::Output {
316        UCoord::new(
317            self.width() - ucoord.width(),
318            self.height() - ucoord.height(),
319        )
320    }
321}
322
323impl<'a> Sub<UCoord> for &'a UCoord {
324    type Output = UCoord;
325    fn sub(self, ucoord: UCoord) -> Self::Output {
326        UCoord::new(
327            self.width() - ucoord.width(),
328            self.height() - ucoord.height(),
329        )
330    }
331}
332
333impl<'a> Sub<&'a UCoord> for UCoord {
334    type Output = UCoord;
335    fn sub(self, ucoord: &'a UCoord) -> Self::Output {
336        UCoord::new(
337            self.width() - ucoord.width(),
338            self.height() - ucoord.height(),
339        )
340    }
341}
342
343impl<'a, 'b> Sub<&'a UCoord> for &'b UCoord {
344    type Output = UCoord;
345    fn sub(self, ucoord: &'a UCoord) -> Self::Output {
346        UCoord::new(
347            self.width() - ucoord.width(),
348            self.height() - ucoord.height(),
349        )
350    }
351}
352
353impl<T> SubAssign<T> for ICoord
354where
355    ICoord: Sub<T, Output = ICoord>,
356{
357    fn sub_assign(&mut self, rhs: T) {
358        *self = *self - rhs;
359    }
360}
361
362impl Mul<i32> for ICoord {
363    type Output = ICoord;
364    fn mul(self, rhs: i32) -> Self::Output {
365        ICoord {
366            x: self.x * rhs,
367            y: self.y * rhs,
368        }
369    }
370}
371
372impl<'a> Mul<i32> for &'a ICoord {
373    type Output = ICoord;
374    fn mul(self, rhs: i32) -> Self::Output {
375        ICoord {
376            x: self.x * rhs,
377            y: self.y * rhs,
378        }
379    }
380}
381
382impl<T> MulAssign<T> for ICoord
383where
384    ICoord: Mul<T, Output = ICoord>,
385{
386    fn mul_assign(&mut self, rhs: T) {
387        *self = *self * rhs;
388    }
389}
390
391impl Mul<u32> for UCoord {
392    type Output = UCoord;
393    fn mul(self, rhs: u32) -> Self::Output {
394        UCoord::new(self.width() * rhs, self.height() * rhs)
395    }
396}
397
398impl<'a> Mul<u32> for &'a UCoord {
399    type Output = UCoord;
400    fn mul(self, rhs: u32) -> Self::Output {
401        UCoord::new(self.width() * rhs, self.height() * rhs)
402    }
403}
404
405impl<T> MulAssign<T> for UCoord
406where
407    UCoord: Mul<T, Output = UCoord>,
408{
409    fn mul_assign(&mut self, rhs: T) {
410        *self = *self * rhs;
411    }
412}
413
414impl Div<i32> for ICoord {
415    type Output = ICoord;
416    fn div(self, rhs: i32) -> Self::Output {
417        ICoord {
418            x: self.x / rhs,
419            y: self.y / rhs,
420        }
421    }
422}
423
424impl<'a> Div<i32> for &'a ICoord {
425    type Output = ICoord;
426    fn div(self, rhs: i32) -> Self::Output {
427        ICoord {
428            x: self.x / rhs,
429            y: self.y / rhs,
430        }
431    }
432}
433
434impl<T> DivAssign<T> for ICoord
435where
436    ICoord: Div<T, Output = ICoord>,
437{
438    fn div_assign(&mut self, rhs: T) {
439        *self = *self / rhs;
440    }
441}
442
443impl Div<u32> for UCoord {
444    type Output = UCoord;
445    fn div(self, rhs: u32) -> Self::Output {
446        UCoord::new(self.width() / rhs, self.height() / rhs)
447    }
448}
449
450impl<'a> Div<u32> for &'a UCoord {
451    type Output = UCoord;
452    fn div(self, rhs: u32) -> Self::Output {
453        UCoord::new(self.width() / rhs, self.height() / rhs)
454    }
455}
456
457impl<T> DivAssign<T> for UCoord
458where
459    UCoord: Div<T, Output = UCoord>,
460{
461    fn div_assign(&mut self, rhs: T) {
462        *self = *self / rhs;
463    }
464}
465
466#[cfg(test)]
467mod test {
468    use crate::coord::{ICoord, UCoord};
469
470    #[test]
471    fn arithmetic() {
472        let mut a = ICoord::new(0, 0);
473        let _ = a + ICoord::new(0, 0);
474        let _ = &a + ICoord::new(0, 0);
475        let _ = a + &ICoord::new(0, 0);
476        let _ = &a + &ICoord::new(0, 0);
477        let _ = a + UCoord::new(0, 0);
478        let _ = &a + UCoord::new(0, 0);
479        let _ = a + &UCoord::new(0, 0);
480        let _ = &a + &UCoord::new(0, 0);
481        let _ = UCoord::new(0, 0) + a;
482        let _ = &UCoord::new(0, 0) + a;
483        let _ = UCoord::new(0, 0) + &a;
484        let _ = &UCoord::new(0, 0) + &a;
485        let _ = a += UCoord::new(0, 0);
486        let _ = a += ICoord::new(0, 0);
487        let _ = a += &UCoord::new(0, 0);
488        let _ = a += &ICoord::new(0, 0);
489    }
490}