hyperloglog_rs/
primitive.rs

1//! This module defines a trait for the primitive conversion of unsigned integer values
2//! between one-another. While this is not a trait in the core library, we are aware that
3//! it is available in other crates - we do not intend to use them as dependencies, as we want to keep
4//! the dependencies to the very bare minimum.
5
6use core::{
7    iter::Sum,
8    ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign},
9};
10
11use crate::max_min::MaxMin;
12use crate::ones::One;
13use core::fmt::Debug;
14
15pub trait Primitive<U>:
16    Sized
17    + Copy
18    + Send
19    + Sync
20    + One
21    + Debug
22    + Sum
23    + Default
24    + Sub<Self, Output = Self>
25    + Add<Self, Output = Self>
26    + AddAssign
27    + MulAssign
28    + SubAssign
29    + DivAssign
30    + PartialOrd
31    + Mul<Self, Output = Self>
32    + Div<Self, Output = Self>
33    + MaxMin
34{
35    fn convert(self) -> U;
36    fn reverse(other: U) -> Self;
37}
38
39impl Primitive<u8> for u16 {
40    #[inline(always)]
41    fn convert(self) -> u8 {
42        self as u8
43    }
44
45    #[inline(always)]
46    fn reverse(other: u8) -> Self {
47        other as u16
48    }
49}
50
51impl Primitive<u8> for u32 {
52    #[inline(always)]
53    fn convert(self) -> u8 {
54        self as u8
55    }
56
57    #[inline(always)]
58    fn reverse(other: u8) -> Self {
59        other as u32
60    }
61}
62
63impl Primitive<u8> for u64 {
64    #[inline(always)]
65    fn convert(self) -> u8 {
66        self as u8
67    }
68
69    #[inline(always)]
70    fn reverse(other: u8) -> Self {
71        other as u64
72    }
73}
74
75impl Primitive<u8> for u128 {
76    #[inline(always)]
77    fn convert(self) -> u8 {
78        self as u8
79    }
80
81    #[inline(always)]
82    fn reverse(other: u8) -> Self {
83        other as u128
84    }
85}
86
87impl Primitive<u8> for usize {
88    #[inline(always)]
89    fn convert(self) -> u8 {
90        self as u8
91    }
92
93    #[inline(always)]
94    fn reverse(other: u8) -> Self {
95        other as usize
96    }
97}
98
99impl Primitive<u16> for u8 {
100    #[inline(always)]
101    fn convert(self) -> u16 {
102        self as u16
103    }
104
105    #[inline(always)]
106    fn reverse(other: u16) -> Self {
107        other as u8
108    }
109}
110
111impl Primitive<u16> for u32 {
112    #[inline(always)]
113    fn convert(self) -> u16 {
114        self as u16
115    }
116
117    #[inline(always)]
118    fn reverse(other: u16) -> Self {
119        other as u32
120    }
121}
122
123impl Primitive<u16> for u64 {
124    #[inline(always)]
125    fn convert(self) -> u16 {
126        self as u16
127    }
128
129    #[inline(always)]
130    fn reverse(other: u16) -> Self {
131        other as u64
132    }
133}
134
135impl Primitive<u16> for u128 {
136    #[inline(always)]
137    fn convert(self) -> u16 {
138        self as u16
139    }
140
141    #[inline(always)]
142    fn reverse(other: u16) -> Self {
143        other as u128
144    }
145}
146
147impl Primitive<u16> for usize {
148    #[inline(always)]
149    fn convert(self) -> u16 {
150        self as u16
151    }
152
153    #[inline(always)]
154    fn reverse(other: u16) -> Self {
155        other as usize
156    }
157}
158
159impl Primitive<u32> for u8 {
160    #[inline(always)]
161    fn convert(self) -> u32 {
162        self as u32
163    }
164
165    #[inline(always)]
166    fn reverse(other: u32) -> Self {
167        other as u8
168    }
169}
170
171impl Primitive<u32> for u16 {
172    #[inline(always)]
173    fn convert(self) -> u32 {
174        self as u32
175    }
176
177    #[inline(always)]
178    fn reverse(other: u32) -> Self {
179        other as u16
180    }
181}
182
183impl Primitive<u32> for u64 {
184    #[inline(always)]
185    fn convert(self) -> u32 {
186        self as u32
187    }
188
189    #[inline(always)]
190    fn reverse(other: u32) -> Self {
191        other as u64
192    }
193}
194
195impl Primitive<u32> for u128 {
196    #[inline(always)]
197    fn convert(self) -> u32 {
198        self as u32
199    }
200
201    #[inline(always)]
202    fn reverse(other: u32) -> Self {
203        other as u128
204    }
205}
206
207impl Primitive<u32> for usize {
208    #[inline(always)]
209    fn convert(self) -> u32 {
210        self as u32
211    }
212
213    #[inline(always)]
214    fn reverse(other: u32) -> Self {
215        other as usize
216    }
217}
218
219impl Primitive<u64> for u8 {
220    #[inline(always)]
221    fn convert(self) -> u64 {
222        self as u64
223    }
224
225    #[inline(always)]
226    fn reverse(other: u64) -> Self {
227        other as u8
228    }
229}
230
231impl Primitive<u64> for u16 {
232    #[inline(always)]
233    fn convert(self) -> u64 {
234        self as u64
235    }
236
237    #[inline(always)]
238    fn reverse(other: u64) -> Self {
239        other as u16
240    }
241}
242
243impl Primitive<u64> for u32 {
244    #[inline(always)]
245    fn convert(self) -> u64 {
246        self as u64
247    }
248
249    #[inline(always)]
250    fn reverse(other: u64) -> Self {
251        other as u32
252    }
253}
254
255impl Primitive<u64> for u128 {
256    #[inline(always)]
257    fn convert(self) -> u64 {
258        self as u64
259    }
260
261    #[inline(always)]
262    fn reverse(other: u64) -> Self {
263        other as u128
264    }
265}
266
267impl Primitive<u64> for usize {
268    #[inline(always)]
269    fn convert(self) -> u64 {
270        self as u64
271    }
272
273    #[inline(always)]
274    fn reverse(other: u64) -> Self {
275        other as usize
276    }
277}
278
279impl Primitive<u128> for u8 {
280    #[inline(always)]
281    fn convert(self) -> u128 {
282        self as u128
283    }
284
285    #[inline(always)]
286    fn reverse(other: u128) -> Self {
287        other as u8
288    }
289}
290
291impl Primitive<u128> for u16 {
292    #[inline(always)]
293    fn convert(self) -> u128 {
294        self as u128
295    }
296
297    #[inline(always)]
298    fn reverse(other: u128) -> Self {
299        other as u16
300    }
301}
302
303impl Primitive<u128> for u32 {
304    #[inline(always)]
305    fn convert(self) -> u128 {
306        self as u128
307    }
308
309    #[inline(always)]
310    fn reverse(other: u128) -> Self {
311        other as u32
312    }
313}
314
315impl Primitive<u128> for u64 {
316    #[inline(always)]
317    fn convert(self) -> u128 {
318        self as u128
319    }
320
321    #[inline(always)]
322    fn reverse(other: u128) -> Self {
323        other as u64
324    }
325}
326
327impl Primitive<u128> for usize {
328    #[inline(always)]
329    fn convert(self) -> u128 {
330        self as u128
331    }
332
333    #[inline(always)]
334    fn reverse(other: u128) -> Self {
335        other as usize
336    }
337}
338
339impl Primitive<usize> for u8 {
340    #[inline(always)]
341    fn convert(self) -> usize {
342        self as usize
343    }
344
345    #[inline(always)]
346    fn reverse(other: usize) -> Self {
347        other as u8
348    }
349}
350
351impl Primitive<usize> for u16 {
352    #[inline(always)]
353    fn convert(self) -> usize {
354        self as usize
355    }
356
357    #[inline(always)]
358    fn reverse(other: usize) -> Self {
359        other as u16
360    }
361}
362
363impl Primitive<usize> for u32 {
364    #[inline(always)]
365    fn convert(self) -> usize {
366        self as usize
367    }
368
369    #[inline(always)]
370    fn reverse(other: usize) -> Self {
371        other as u32
372    }
373}
374
375impl Primitive<usize> for u64 {
376    #[inline(always)]
377    fn convert(self) -> usize {
378        self as usize
379    }
380
381    #[inline(always)]
382    fn reverse(other: usize) -> Self {
383        other as u64
384    }
385}
386
387impl Primitive<usize> for u128 {
388    #[inline(always)]
389    fn convert(self) -> usize {
390        self as usize
391    }
392
393    #[inline(always)]
394    fn reverse(other: usize) -> Self {
395        other as u128
396    }
397}
398
399impl Primitive<usize> for usize {
400    #[inline(always)]
401    fn convert(self) -> usize {
402        self
403    }
404
405    #[inline(always)]
406    fn reverse(other: usize) -> Self {
407        other
408    }
409}
410
411impl Primitive<bool> for u8 {
412    #[inline(always)]
413    fn convert(self) -> bool {
414        self != 0
415    }
416
417    #[inline(always)]
418    fn reverse(other: bool) -> Self {
419        if other {
420            1
421        } else {
422            0
423        }
424    }
425}
426
427impl Primitive<bool> for u16 {
428    #[inline(always)]
429    fn convert(self) -> bool {
430        self != 0
431    }
432
433    #[inline(always)]
434    fn reverse(other: bool) -> Self {
435        if other {
436            1
437        } else {
438            0
439        }
440    }
441}
442
443impl Primitive<bool> for u32 {
444    #[inline(always)]
445    fn convert(self) -> bool {
446        self != 0
447    }
448
449    #[inline(always)]
450    fn reverse(other: bool) -> Self {
451        if other {
452            1
453        } else {
454            0
455        }
456    }
457}
458
459impl Primitive<bool> for u64 {
460    #[inline(always)]
461    fn convert(self) -> bool {
462        self != 0
463    }
464
465    #[inline(always)]
466    fn reverse(other: bool) -> Self {
467        if other {
468            1
469        } else {
470            0
471        }
472    }
473}
474
475impl Primitive<bool> for u128 {
476    #[inline(always)]
477    fn convert(self) -> bool {
478        self != 0
479    }
480
481    #[inline(always)]
482    fn reverse(other: bool) -> Self {
483        if other {
484            1
485        } else {
486            0
487        }
488    }
489}
490
491impl Primitive<bool> for usize {
492    #[inline(always)]
493    fn convert(self) -> bool {
494        self != 0
495    }
496
497    #[inline(always)]
498    fn reverse(other: bool) -> Self {
499        if other {
500            1
501        } else {
502            0
503        }
504    }
505}
506
507impl Primitive<f32> for f32 {
508    #[inline(always)]
509    fn convert(self) -> f32 {
510        self
511    }
512
513    #[inline(always)]
514    fn reverse(other: f32) -> Self {
515        other
516    }
517}
518
519impl Primitive<f64> for f64 {
520    #[inline(always)]
521    fn convert(self) -> f64 {
522        self
523    }
524
525    #[inline(always)]
526    fn reverse(other: f64) -> Self {
527        other
528    }
529}
530
531impl Primitive<f32> for f64 {
532    #[inline(always)]
533    fn convert(self) -> f32 {
534        self as f32
535    }
536
537    #[inline(always)]
538    fn reverse(other: f32) -> Self {
539        other as f64
540    }
541}
542
543impl Primitive<f64> for f32 {
544    #[inline(always)]
545    fn convert(self) -> f64 {
546        self as f64
547    }
548
549    #[inline(always)]
550    fn reverse(other: f64) -> Self {
551        other as f32
552    }
553}
554
555impl Primitive<u8> for f32 {
556    #[inline(always)]
557    fn convert(self) -> u8 {
558        self as u8
559    }
560
561    #[inline(always)]
562    fn reverse(other: u8) -> Self {
563        other as f32
564    }
565}
566
567impl Primitive<u8> for f64 {
568    #[inline(always)]
569    fn convert(self) -> u8 {
570        self as u8
571    }
572
573    #[inline(always)]
574    fn reverse(other: u8) -> Self {
575        other as f64
576    }
577}
578
579impl Primitive<u16> for f32 {
580    #[inline(always)]
581    fn convert(self) -> u16 {
582        self as u16
583    }
584
585    #[inline(always)]
586    fn reverse(other: u16) -> Self {
587        other as f32
588    }
589}
590
591impl Primitive<u16> for f64 {
592    #[inline(always)]
593    fn convert(self) -> u16 {
594        self as u16
595    }
596
597    #[inline(always)]
598    fn reverse(other: u16) -> Self {
599        other as f64
600    }
601}
602
603impl Primitive<u32> for f32 {
604    #[inline(always)]
605    fn convert(self) -> u32 {
606        self as u32
607    }
608
609    #[inline(always)]
610    fn reverse(other: u32) -> Self {
611        other as f32
612    }
613}
614
615impl Primitive<u32> for f64 {
616    #[inline(always)]
617    fn convert(self) -> u32 {
618        self as u32
619    }
620
621    #[inline(always)]
622    fn reverse(other: u32) -> Self {
623        other as f64
624    }
625}
626
627impl Primitive<u64> for f32 {
628    #[inline(always)]
629    fn convert(self) -> u64 {
630        self as u64
631    }
632
633    #[inline(always)]
634    fn reverse(other: u64) -> Self {
635        other as f32
636    }
637}
638
639impl Primitive<u64> for f64 {
640    #[inline(always)]
641    fn convert(self) -> u64 {
642        self as u64
643    }
644
645    #[inline(always)]
646    fn reverse(other: u64) -> Self {
647        other as f64
648    }
649}
650
651impl Primitive<u128> for f32 {
652    #[inline(always)]
653    fn convert(self) -> u128 {
654        self as u128
655    }
656
657    #[inline(always)]
658    fn reverse(other: u128) -> Self {
659        other as f32
660    }
661}
662
663impl Primitive<u128> for f64 {
664    #[inline(always)]
665    fn convert(self) -> u128 {
666        self as u128
667    }
668
669    #[inline(always)]
670    fn reverse(other: u128) -> Self {
671        other as f64
672    }
673}
674
675impl Primitive<usize> for f32 {
676    #[inline(always)]
677    fn convert(self) -> usize {
678        self as usize
679    }
680
681    #[inline(always)]
682    fn reverse(other: usize) -> Self {
683        other as f32
684    }
685}
686
687impl Primitive<usize> for f64 {
688    #[inline(always)]
689    fn convert(self) -> usize {
690        self as usize
691    }
692
693    #[inline(always)]
694    fn reverse(other: usize) -> Self {
695        other as f64
696    }
697}
698
699impl Primitive<bool> for f32 {
700    #[inline(always)]
701    fn convert(self) -> bool {
702        self != 0.0
703    }
704
705    #[inline(always)]
706    fn reverse(other: bool) -> Self {
707        if other {
708            1.0
709        } else {
710            0.0
711        }
712    }
713}
714
715impl Primitive<bool> for f64 {
716    #[inline(always)]
717    fn convert(self) -> bool {
718        self != 0.0
719    }
720
721    #[inline(always)]
722    fn reverse(other: bool) -> Self {
723        if other {
724            1.0
725        } else {
726            0.0
727        }
728    }
729}
730
731impl Primitive<f32> for u8 {
732    #[inline(always)]
733    fn convert(self) -> f32 {
734        self as f32
735    }
736
737    #[inline(always)]
738    fn reverse(other: f32) -> Self {
739        other as u8
740    }
741}
742
743impl Primitive<f32> for u16 {
744    #[inline(always)]
745    fn convert(self) -> f32 {
746        self as f32
747    }
748
749    #[inline(always)]
750    fn reverse(other: f32) -> Self {
751        other as u16
752    }
753}
754
755impl Primitive<f32> for u32 {
756    #[inline(always)]
757    fn convert(self) -> f32 {
758        self as f32
759    }
760
761    #[inline(always)]
762    fn reverse(other: f32) -> Self {
763        other as u32
764    }
765}
766
767impl Primitive<f32> for u64 {
768    #[inline(always)]
769    fn convert(self) -> f32 {
770        self as f32
771    }
772
773    #[inline(always)]
774    fn reverse(other: f32) -> Self {
775        other as u64
776    }
777}
778
779impl Primitive<f32> for u128 {
780    #[inline(always)]
781    fn convert(self) -> f32 {
782        self as f32
783    }
784
785    #[inline(always)]
786    fn reverse(other: f32) -> Self {
787        other as u128
788    }
789}
790
791impl Primitive<f32> for usize {
792    #[inline(always)]
793    fn convert(self) -> f32 {
794        self as f32
795    }
796
797    #[inline(always)]
798    fn reverse(other: f32) -> Self {
799        other as usize
800    }
801}
802
803impl Primitive<f64> for u8 {
804    #[inline(always)]
805    fn convert(self) -> f64 {
806        self as f64
807    }
808
809    #[inline(always)]
810    fn reverse(other: f64) -> Self {
811        other as u8
812    }
813}
814
815impl Primitive<f64> for u16 {
816    #[inline(always)]
817    fn convert(self) -> f64 {
818        self as f64
819    }
820
821    #[inline(always)]
822    fn reverse(other: f64) -> Self {
823        other as u16
824    }
825}
826
827impl Primitive<f64> for u32 {
828    #[inline(always)]
829    fn convert(self) -> f64 {
830        self as f64
831    }
832
833    #[inline(always)]
834    fn reverse(other: f64) -> Self {
835        other as u32
836    }
837}
838
839impl Primitive<f64> for u64 {
840    #[inline(always)]
841    fn convert(self) -> f64 {
842        self as f64
843    }
844
845    #[inline(always)]
846    fn reverse(other: f64) -> Self {
847        other as u64
848    }
849}
850
851impl Primitive<f64> for u128 {
852    #[inline(always)]
853    fn convert(self) -> f64 {
854        self as f64
855    }
856
857    #[inline(always)]
858    fn reverse(other: f64) -> Self {
859        other as u128
860    }
861}
862
863impl Primitive<f64> for usize {
864    #[inline(always)]
865    fn convert(self) -> f64 {
866        self as f64
867    }
868
869    #[inline(always)]
870    fn reverse(other: f64) -> Self {
871        other as usize
872    }
873}