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