gluesql_core/data/interval/
primitive.rs

1use {
2    super::Interval,
3    std::ops::{Div, Mul},
4};
5
6impl Mul<i8> for Interval {
7    type Output = Self;
8
9    fn mul(self, rhs: i8) -> Self {
10        match self {
11            Interval::Month(v) => Interval::Month(v * i32::from(rhs)),
12            Interval::Microsecond(v) => Interval::Microsecond(v * i64::from(rhs)),
13        }
14    }
15}
16
17impl Mul<i16> for Interval {
18    type Output = Self;
19
20    fn mul(self, rhs: i16) -> Self {
21        match self {
22            Interval::Month(v) => Interval::Month(v * i32::from(rhs)),
23            Interval::Microsecond(v) => Interval::Microsecond(v * i64::from(rhs)),
24        }
25    }
26}
27
28impl Mul<i32> for Interval {
29    type Output = Self;
30
31    fn mul(self, rhs: i32) -> Self {
32        match self {
33            Interval::Month(v) => Interval::Month(v * rhs),
34            Interval::Microsecond(v) => Interval::Microsecond(v * i64::from(rhs)),
35        }
36    }
37}
38
39impl Mul<i64> for Interval {
40    type Output = Self;
41
42    fn mul(self, rhs: i64) -> Self {
43        match self {
44            Interval::Month(v) => Interval::Month((i64::from(v) * rhs) as i32),
45            Interval::Microsecond(v) => Interval::Microsecond(v * rhs),
46        }
47    }
48}
49
50impl Mul<i128> for Interval {
51    type Output = Self;
52
53    fn mul(self, rhs: i128) -> Self {
54        match self {
55            Interval::Month(v) => Interval::Month((i128::from(v) * rhs) as i32),
56            Interval::Microsecond(v) => Interval::Microsecond((i128::from(v) * rhs) as i64),
57        }
58    }
59}
60
61impl Mul<u8> for Interval {
62    type Output = Self;
63
64    fn mul(self, rhs: u8) -> Self {
65        match self {
66            Interval::Month(v) => Interval::Month(i32::from((v as u8) * rhs)),
67            Interval::Microsecond(v) => Interval::Microsecond(i64::from((v as u8) * rhs)),
68        }
69    }
70}
71
72impl Mul<u16> for Interval {
73    type Output = Self;
74
75    fn mul(self, rhs: u16) -> Self {
76        match self {
77            Interval::Month(v) => Interval::Month(i32::from((v as u16) * rhs)),
78            Interval::Microsecond(v) => Interval::Microsecond(i64::from((v as u16) * rhs)),
79        }
80    }
81}
82
83impl Mul<u32> for Interval {
84    type Output = Self;
85
86    fn mul(self, rhs: u32) -> Self {
87        match self {
88            Interval::Month(v) => Interval::Month(((v as u32) * rhs) as i32),
89            Interval::Microsecond(v) => Interval::Microsecond(i64::from((v as u32) * rhs)),
90        }
91    }
92}
93
94impl Mul<u64> for Interval {
95    type Output = Self;
96
97    fn mul(self, rhs: u64) -> Self {
98        match self {
99            Interval::Month(v) => Interval::Month(((v as u64) * rhs) as i32),
100            Interval::Microsecond(v) => Interval::Microsecond(((v as u64) * rhs) as i64),
101        }
102    }
103}
104
105impl Mul<u128> for Interval {
106    type Output = Self;
107
108    fn mul(self, rhs: u128) -> Self {
109        match self {
110            Interval::Month(v) => Interval::Month(((v as u128) * rhs) as i32),
111            Interval::Microsecond(v) => Interval::Microsecond(((v as u128) * rhs) as i64),
112        }
113    }
114}
115
116impl Mul<f32> for Interval {
117    type Output = Self;
118
119    fn mul(self, rhs: f32) -> Self {
120        match self {
121            Interval::Month(v) => Interval::Month((v as f32 * rhs) as i32),
122            Interval::Microsecond(v) => Interval::Microsecond((v as f32 * rhs) as i64),
123        }
124    }
125}
126
127impl Mul<f64> for Interval {
128    type Output = Self;
129
130    fn mul(self, rhs: f64) -> Self {
131        match self {
132            Interval::Month(v) => Interval::Month((f64::from(v) * rhs) as i32),
133            Interval::Microsecond(v) => Interval::Microsecond((v as f64 * rhs) as i64),
134        }
135    }
136}
137
138impl Mul<Interval> for i8 {
139    type Output = Interval;
140
141    fn mul(self, rhs: Interval) -> Interval {
142        rhs * self
143    }
144}
145
146impl Mul<Interval> for i16 {
147    type Output = Interval;
148
149    fn mul(self, rhs: Interval) -> Interval {
150        rhs * self
151    }
152}
153
154impl Mul<Interval> for i32 {
155    type Output = Interval;
156
157    fn mul(self, rhs: Interval) -> Interval {
158        rhs * self
159    }
160}
161
162impl Mul<Interval> for i64 {
163    type Output = Interval;
164
165    fn mul(self, rhs: Interval) -> Interval {
166        rhs * self
167    }
168}
169
170impl Mul<Interval> for i128 {
171    type Output = Interval;
172
173    fn mul(self, rhs: Interval) -> Interval {
174        rhs * self
175    }
176}
177
178impl Mul<Interval> for u8 {
179    type Output = Interval;
180
181    fn mul(self, rhs: Interval) -> Interval {
182        rhs * self
183    }
184}
185
186impl Mul<Interval> for u16 {
187    type Output = Interval;
188
189    fn mul(self, rhs: Interval) -> Interval {
190        rhs * self
191    }
192}
193
194impl Mul<Interval> for u32 {
195    type Output = Interval;
196
197    fn mul(self, rhs: Interval) -> Interval {
198        rhs * self
199    }
200}
201
202impl Mul<Interval> for u64 {
203    type Output = Interval;
204
205    fn mul(self, rhs: Interval) -> Interval {
206        rhs * self
207    }
208}
209
210impl Mul<Interval> for u128 {
211    type Output = Interval;
212
213    fn mul(self, rhs: Interval) -> Interval {
214        rhs * self
215    }
216}
217
218impl Mul<Interval> for f32 {
219    type Output = Interval;
220
221    fn mul(self, rhs: Interval) -> Interval {
222        rhs * self
223    }
224}
225
226impl Mul<Interval> for f64 {
227    type Output = Interval;
228
229    fn mul(self, rhs: Interval) -> Interval {
230        rhs * self
231    }
232}
233
234impl Div<i8> for Interval {
235    type Output = Self;
236
237    fn div(self, rhs: i8) -> Self {
238        match self {
239            Interval::Month(v) => Interval::Month(v / i32::from(rhs)),
240            Interval::Microsecond(v) => Interval::Microsecond(v / i64::from(rhs)),
241        }
242    }
243}
244
245impl Div<i16> for Interval {
246    type Output = Self;
247
248    fn div(self, rhs: i16) -> Self {
249        match self {
250            Interval::Month(v) => Interval::Month(v / i32::from(rhs)),
251            Interval::Microsecond(v) => Interval::Microsecond(v / i64::from(rhs)),
252        }
253    }
254}
255
256impl Div<i32> for Interval {
257    type Output = Self;
258
259    fn div(self, rhs: i32) -> Self {
260        match self {
261            Interval::Month(v) => Interval::Month(v / rhs),
262            Interval::Microsecond(v) => Interval::Microsecond(v / i64::from(rhs)),
263        }
264    }
265}
266
267impl Div<i64> for Interval {
268    type Output = Self;
269
270    fn div(self, rhs: i64) -> Self {
271        match self {
272            Interval::Month(v) => Interval::Month((i64::from(v) / rhs) as i32),
273            Interval::Microsecond(v) => Interval::Microsecond(v / rhs),
274        }
275    }
276}
277
278impl Div<i128> for Interval {
279    type Output = Self;
280
281    fn div(self, rhs: i128) -> Self {
282        match self {
283            Interval::Month(v) => Interval::Month((i128::from(v) / rhs) as i32),
284            Interval::Microsecond(v) => Interval::Microsecond((i128::from(v) / rhs) as i64),
285        }
286    }
287}
288
289impl Div<u8> for Interval {
290    type Output = Self;
291
292    fn div(self, rhs: u8) -> Self {
293        match self {
294            Interval::Month(v) => Interval::Month(i32::from((v as u8) / rhs)),
295            Interval::Microsecond(v) => Interval::Microsecond(i64::from((v as u8) / rhs)),
296        }
297    }
298}
299
300impl Div<u16> for Interval {
301    type Output = Self;
302
303    fn div(self, rhs: u16) -> Self {
304        match self {
305            Interval::Month(v) => Interval::Month(i32::from((v as u16) / rhs)),
306            Interval::Microsecond(v) => Interval::Microsecond(i64::from((v as u16) / rhs)),
307        }
308    }
309}
310
311impl Div<u32> for Interval {
312    type Output = Self;
313
314    fn div(self, rhs: u32) -> Self {
315        match self {
316            Interval::Month(v) => Interval::Month(((v as u32) / rhs) as i32),
317            Interval::Microsecond(v) => Interval::Microsecond(i64::from((v as u32) / rhs)),
318        }
319    }
320}
321
322impl Div<u64> for Interval {
323    type Output = Self;
324
325    fn div(self, rhs: u64) -> Self {
326        match self {
327            Interval::Month(v) => Interval::Month(((v as u64) / rhs) as i32),
328            Interval::Microsecond(v) => Interval::Microsecond(((v as u64) / rhs) as i64),
329        }
330    }
331}
332
333impl Div<u128> for Interval {
334    type Output = Self;
335
336    fn div(self, rhs: u128) -> Self {
337        match self {
338            Interval::Month(v) => Interval::Month(((v as u128) / rhs) as i32),
339            Interval::Microsecond(v) => Interval::Microsecond(((v as u128) / rhs) as i64),
340        }
341    }
342}
343
344impl Div<f32> for Interval {
345    type Output = Self;
346
347    fn div(self, rhs: f32) -> Self {
348        match self {
349            Interval::Month(v) => Interval::Month((v as f32 / rhs) as i32),
350            Interval::Microsecond(v) => Interval::Microsecond((v as f32 / rhs) as i64),
351        }
352    }
353}
354
355impl Div<f64> for Interval {
356    type Output = Self;
357
358    fn div(self, rhs: f64) -> Self {
359        match self {
360            Interval::Month(v) => Interval::Month((f64::from(v) / rhs) as i32),
361            Interval::Microsecond(v) => Interval::Microsecond((v as f64 / rhs) as i64),
362        }
363    }
364}
365
366impl Div<Interval> for i8 {
367    type Output = Interval;
368
369    fn div(self, rhs: Interval) -> Interval {
370        match rhs {
371            Interval::Month(v) => Interval::Month(i32::from(self) / v),
372            Interval::Microsecond(v) => Interval::Microsecond(i64::from(self) / v),
373        }
374    }
375}
376
377impl Div<Interval> for i16 {
378    type Output = Interval;
379
380    fn div(self, rhs: Interval) -> Interval {
381        match rhs {
382            Interval::Month(v) => Interval::Month(i32::from(self) / v),
383            Interval::Microsecond(v) => Interval::Microsecond(i64::from(self) / v),
384        }
385    }
386}
387
388impl Div<Interval> for i32 {
389    type Output = Interval;
390
391    fn div(self, rhs: Interval) -> Interval {
392        match rhs {
393            Interval::Month(v) => Interval::Month(self / v),
394            Interval::Microsecond(v) => Interval::Microsecond(i64::from(self) / v),
395        }
396    }
397}
398
399impl Div<Interval> for i64 {
400    type Output = Interval;
401
402    fn div(self, rhs: Interval) -> Interval {
403        match rhs {
404            Interval::Month(v) => Interval::Month((self / i64::from(v)) as i32),
405            Interval::Microsecond(v) => Interval::Microsecond(self / v),
406        }
407    }
408}
409
410impl Div<Interval> for i128 {
411    type Output = Interval;
412
413    fn div(self, rhs: Interval) -> Interval {
414        match rhs {
415            Interval::Month(v) => Interval::Month((self / i128::from(v)) as i32),
416            Interval::Microsecond(v) => Interval::Microsecond((self / i128::from(v)) as i64),
417        }
418    }
419}
420
421impl Div<Interval> for u8 {
422    type Output = Interval;
423
424    fn div(self, rhs: Interval) -> Interval {
425        match rhs {
426            Interval::Month(v) => Interval::Month(i32::from(self / (v as u8))),
427            Interval::Microsecond(v) => Interval::Microsecond(i64::from(self / (v as u8))),
428        }
429    }
430}
431
432impl Div<Interval> for u16 {
433    type Output = Interval;
434
435    fn div(self, rhs: Interval) -> Interval {
436        match rhs {
437            Interval::Month(v) => Interval::Month(i32::from(self / (v as u16))),
438            Interval::Microsecond(v) => Interval::Microsecond(i64::from(self / (v as u16))),
439        }
440    }
441}
442
443impl Div<Interval> for u32 {
444    type Output = Interval;
445
446    fn div(self, rhs: Interval) -> Interval {
447        match rhs {
448            Interval::Month(v) => Interval::Month((self / (v as u32)) as i32),
449            Interval::Microsecond(v) => Interval::Microsecond(i64::from(self / (v as u32))),
450        }
451    }
452}
453
454impl Div<Interval> for u64 {
455    type Output = Interval;
456
457    fn div(self, rhs: Interval) -> Interval {
458        match rhs {
459            Interval::Month(v) => Interval::Month((self / (v as u64)) as i32),
460            Interval::Microsecond(v) => Interval::Microsecond((self / (v as u64)) as i64),
461        }
462    }
463}
464
465impl Div<Interval> for u128 {
466    type Output = Interval;
467
468    fn div(self, rhs: Interval) -> Interval {
469        match rhs {
470            Interval::Month(v) => Interval::Month((self / (v as u128)) as i32),
471            Interval::Microsecond(v) => Interval::Microsecond((self / (v as u128)) as i64),
472        }
473    }
474}
475
476impl Div<Interval> for f32 {
477    type Output = Interval;
478
479    fn div(self, rhs: Interval) -> Interval {
480        match rhs {
481            Interval::Month(v) => Interval::Month((self / v as f32) as i32),
482            Interval::Microsecond(v) => Interval::Microsecond((self / v as f32) as i64),
483        }
484    }
485}
486
487impl Div<Interval> for f64 {
488    type Output = Interval;
489
490    fn div(self, rhs: Interval) -> Interval {
491        match rhs {
492            Interval::Month(v) => Interval::Month((self / f64::from(v)) as i32),
493            Interval::Microsecond(v) => Interval::Microsecond((self / v as f64) as i64),
494        }
495    }
496}
497
498#[cfg(test)]
499mod tests {
500    use super::Interval;
501
502    #[test]
503    fn arithmetic() {
504        use Interval::*;
505
506        assert_eq!(Month(2) * 3_i8, Month(6));
507        assert_eq!(2_i8 * Month(3), Month(6));
508
509        assert_eq!(Month(2) * 3_i16, Month(6));
510        assert_eq!(2_i16 * Month(3), Month(6));
511
512        assert_eq!(Month(2) * 3_i32, Month(6));
513        assert_eq!(2_i32 * Month(3), Month(6));
514
515        assert_eq!(Month(2) * 3_i64, Month(6));
516        assert_eq!(2_i64 * Month(3), Month(6));
517
518        assert_eq!(Month(2) * 3_i128, Month(6));
519        assert_eq!(2_i128 * Month(3), Month(6));
520
521        assert_eq!(Month(2) * 3_u8, Month(6));
522        assert_eq!(2_u8 * Month(3), Month(6));
523
524        assert_eq!(Month(2) * 3_u16, Month(6));
525        assert_eq!(2_u16 * Month(3), Month(6));
526
527        assert_eq!(Month(2) * 3_u32, Month(6));
528        assert_eq!(2_u32 * Month(3), Month(6));
529
530        assert_eq!(Month(2) * 3_u64, Month(6));
531        assert_eq!(2_u64 * Month(3), Month(6));
532
533        assert_eq!(Month(2) * 3_u128, Month(6));
534        assert_eq!(2_u128 * Month(3), Month(6));
535
536        assert_eq!(Month(2) * 3_f32, Month(6));
537        assert_eq!(2_f32 * Month(3), Month(6));
538
539        assert_eq!(Month(2) * 3_f64, Month(6));
540        assert_eq!(2_f64 * Month(3), Month(6));
541
542        assert_eq!(Month(2) * 3.0, Month(6));
543        assert_eq!(2.0 * Month(3), Month(6));
544
545        assert_eq!(Month(6) / 3_i8, Month(2));
546        assert_eq!(6_i8 / Month(2), Month(3));
547
548        assert_eq!(Month(6) / 3_i16, Month(2));
549        assert_eq!(6_i16 / Month(2), Month(3));
550
551        assert_eq!(Month(6) / 3_i32, Month(2));
552        assert_eq!(6_i32 / Month(2), Month(3));
553
554        assert_eq!(Month(6) / 3_i64, Month(2));
555        assert_eq!(6_i64 / Month(2), Month(3));
556
557        assert_eq!(Month(6) / 3_i128, Month(2));
558        assert_eq!(6_i128 / Month(2), Month(3));
559
560        assert_eq!(Month(6) / 3_u8, Month(2));
561        assert_eq!(6_u8 / Month(2), Month(3));
562
563        assert_eq!(Month(6) / 3_u16, Month(2));
564        assert_eq!(6_u16 / Month(2), Month(3));
565
566        assert_eq!(Month(6) / 3_u32, Month(2));
567        assert_eq!(6_u32 / Month(2), Month(3));
568
569        assert_eq!(Month(6) / 3_u64, Month(2));
570        assert_eq!(6_u64 / Month(2), Month(3));
571
572        assert_eq!(Month(6) / 3_u128, Month(2));
573        assert_eq!(6_u128 / Month(2), Month(3));
574
575        assert_eq!(Month(8) / 4.0_f32, Month(2));
576        assert_eq!(8.0_f32 / Month(4), Month(2));
577
578        assert_eq!(Month(8) / 4.0, Month(2));
579        assert_eq!(8.0 / Month(4), Month(2));
580
581        assert_eq!(Microsecond(2) * 3_i8, Microsecond(6));
582        assert_eq!(2_i8 * Microsecond(3), Microsecond(6));
583
584        assert_eq!(Microsecond(2) * 3_i16, Microsecond(6));
585        assert_eq!(2_i16 * Microsecond(3), Microsecond(6));
586
587        assert_eq!(Microsecond(2) * 3_i32, Microsecond(6));
588        assert_eq!(2_i32 * Microsecond(3), Microsecond(6));
589
590        assert_eq!(Microsecond(2) * 3_i64, Microsecond(6));
591        assert_eq!(2_i64 * Microsecond(3), Microsecond(6));
592
593        assert_eq!(Microsecond(2) * 3_i128, Microsecond(6));
594        assert_eq!(2_i128 * Microsecond(3), Microsecond(6));
595
596        assert_eq!(Microsecond(2) * 3_u8, Microsecond(6));
597        assert_eq!(2_u8 * Microsecond(3), Microsecond(6));
598
599        assert_eq!(Microsecond(2) * 3_u16, Microsecond(6));
600        assert_eq!(2_u16 * Microsecond(3), Microsecond(6));
601
602        assert_eq!(Microsecond(2) * 3_u32, Microsecond(6));
603        assert_eq!(2_u32 * Microsecond(3), Microsecond(6));
604
605        assert_eq!(Microsecond(2) * 3_u64, Microsecond(6));
606        assert_eq!(2_u64 * Microsecond(3), Microsecond(6));
607
608        assert_eq!(Microsecond(2) * 3_u128, Microsecond(6));
609        assert_eq!(2_u128 * Microsecond(3), Microsecond(6));
610
611        assert_eq!(Microsecond(2) * 3.0_f32, Microsecond(6));
612        assert_eq!(2.0_f32 * Microsecond(3), Microsecond(6));
613
614        assert_eq!(Microsecond(2) * 3.0, Microsecond(6));
615        assert_eq!(2.0 * Microsecond(3), Microsecond(6));
616
617        assert_eq!(Microsecond(6) / 3_i8, Microsecond(2));
618        assert_eq!(6_i8 / Microsecond(2), Microsecond(3));
619
620        assert_eq!(Microsecond(6) / 3_i16, Microsecond(2));
621        assert_eq!(6_i16 / Microsecond(2), Microsecond(3));
622
623        assert_eq!(Microsecond(6) / 3_i32, Microsecond(2));
624        assert_eq!(6_i32 / Microsecond(2), Microsecond(3));
625
626        assert_eq!(Microsecond(6) / 3_i64, Microsecond(2));
627        assert_eq!(6_i64 / Microsecond(2), Microsecond(3));
628
629        assert_eq!(Microsecond(6) / 3_i128, Microsecond(2));
630        assert_eq!(6_i128 / Microsecond(2), Microsecond(3));
631
632        assert_eq!(Microsecond(6) / 3_u8, Microsecond(2));
633        assert_eq!(6_u8 / Microsecond(2), Microsecond(3));
634
635        assert_eq!(Microsecond(6) / 3_u16, Microsecond(2));
636        assert_eq!(6_u16 / Microsecond(2), Microsecond(3));
637
638        assert_eq!(Microsecond(6) / 3_u32, Microsecond(2));
639        assert_eq!(6_u32 / Microsecond(2), Microsecond(3));
640
641        assert_eq!(Microsecond(6) / 3_u64, Microsecond(2));
642        assert_eq!(6_u64 / Microsecond(2), Microsecond(3));
643
644        assert_eq!(Microsecond(6) / 3_u128, Microsecond(2));
645        assert_eq!(6_u128 / Microsecond(2), Microsecond(3));
646
647        assert_eq!(Microsecond(6) / 3_f32, Microsecond(2));
648        assert_eq!(6_f32 / Microsecond(2), Microsecond(3));
649
650        assert_eq!(Microsecond(6) / 3_f64, Microsecond(2));
651        assert_eq!(6_f64 / Microsecond(2), Microsecond(3));
652    }
653}