1use core::{
18 cmp::Ordering,
19 fmt::{Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp, UpperHex},
20 hash::Hash,
21 iter::{Product, Sum},
22 mem::{size_of, transmute},
23 num::{FpCategory, ParseIntError},
24 ops::{
25 Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div,
26 DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub,
27 SubAssign,
28 },
29 panic::{RefUnwindSafe, UnwindSafe},
30 str::FromStr,
31};
32
33use crate::{array::Array, primitive::Primitive};
34
35pub trait NumberLike:
36 Primitive
37 + Copy
38 + Default
39 + FromStr
40 + PartialEq
41 + PartialOrd
42 + Debug
43 + Display
44 + Unpin
45 + UnwindSafe
46 + RefUnwindSafe
47 + Send
48 + Sync
49 + Sized
50 + 'static
51{
52 const MIN: Self;
56 const MAX: Self;
60
61 type Underlying: Number;
62 type ByteArray: Array<Item = u8>;
63
64 fn to_underlying(self) -> Self::Underlying;
65 fn try_from_underlying(underlying: Self::Underlying) -> Option<Self>;
66 fn to_bytes(self) -> Self::ByteArray;
67 fn try_from_bytes(bytes: Self::ByteArray) -> Option<Self>;
68
69 fn to_be_bytes(self) -> Self::ByteArray;
70 fn to_le_bytes(self) -> Self::ByteArray;
71 fn to_ne_bytes(self) -> Self::ByteArray;
72 fn try_from_be_bytes(bytes: Self::ByteArray) -> Option<Self>;
73 fn try_from_le_bytes(bytes: Self::ByteArray) -> Option<Self>;
74 fn try_from_ne_bytes(bytes: Self::ByteArray) -> Option<Self>;
75}
76
77macro_rules! impl_number_like {
78 (
79 $ty:ty,
80 underlying: $number:ty,
81 min: $min:expr,
82 max: $max:expr,
83 try_from_underlying: $try_from_underlying:expr
84 ) => {
85 impl Primitive for $ty {}
86 impl NumberLike for $ty {
87 const MIN: Self = $min;
88 const MAX: Self = $max;
89
90 type Underlying = $number;
91 type ByteArray = [u8; size_of::<Self>()];
92
93 fn to_underlying(self) -> Self::Underlying {
94 #[allow(clippy::useless_transmute)]
95 unsafe {
96 transmute::<Self, Self::Underlying>(self)
97 }
98 }
99
100 fn try_from_underlying(underlying: Self::Underlying) -> Option<Self> {
101 $try_from_underlying(underlying)
102 }
103
104 fn to_bytes(self) -> Self::ByteArray {
105 #[allow(clippy::transmute_num_to_bytes)]
106 unsafe {
107 transmute::<Self, Self::ByteArray>(self)
108 }
109 }
110
111 fn try_from_bytes(bytes: Self::ByteArray) -> Option<Self> {
112 Self::try_from_underlying(Self::Underlying::from_bytes(bytes))
113 }
114
115 fn to_be_bytes(self) -> Self::ByteArray {
116 self.to_underlying().to_be_bytes()
117 }
118
119 fn to_le_bytes(self) -> Self::ByteArray {
120 self.to_underlying().to_le_bytes()
121 }
122
123 fn to_ne_bytes(self) -> Self::ByteArray {
124 self.to_underlying().to_ne_bytes()
125 }
126
127 fn try_from_be_bytes(bytes: Self::ByteArray) -> Option<Self> {
128 Self::try_from_underlying(Self::Underlying::from_be_bytes(bytes))
129 }
130
131 fn try_from_le_bytes(bytes: Self::ByteArray) -> Option<Self> {
132 Self::try_from_underlying(Self::Underlying::from_le_bytes(bytes))
133 }
134
135 fn try_from_ne_bytes(bytes: Self::ByteArray) -> Option<Self> {
136 Self::try_from_underlying(Self::Underlying::from_ne_bytes(bytes))
137 }
138 }
139 };
140}
141
142impl_number_like!(bool,
143 underlying: u8,
144 min: false,
145 max: true,
146 try_from_underlying: |v| match v {
147 0 => Some(false),
148 1 => Some(true),
149 _ => None,
150 }
151);
152impl_number_like!(char,
153 underlying: u32,
154 min: '\0',
155 max: '\u{10ffff}',
156 try_from_underlying: |v| char::try_from(v).ok()
157);
158
159pub trait Number:
160 NumberLike
161 + LowerExp
162 + UpperExp
163 + Add<Self>
164 + for<'a> Add<&'a Self>
165 + AddAssign<Self>
166 + for<'a> AddAssign<&'a Self>
167 + Sub<Self>
168 + for<'a> Sub<&'a Self>
169 + SubAssign<Self>
170 + for<'a> SubAssign<&'a Self>
171 + Mul<Self>
172 + for<'a> Mul<&'a Self>
173 + MulAssign<Self>
174 + for<'a> MulAssign<&'a Self>
175 + Div<Self>
176 + for<'a> Div<&'a Self>
177 + DivAssign<Self>
178 + for<'a> DivAssign<&'a Self>
179 + Rem<Self>
180 + for<'a> Rem<&'a Self>
181 + RemAssign<Self>
182 + for<'a> RemAssign<&'a Self>
183 + TryFrom<u8>
184 + TryFrom<u16>
185 + TryFrom<i8>
186 + TryFrom<i16>
187 + Sum
188 + Product
189{
190 const ZERO: Self;
191 const ONE: Self;
192 const TWO: Self;
193
194 fn from_bytes(bytes: Self::ByteArray) -> Self;
195 fn as_mut_bytes(&mut self) -> &mut Self::ByteArray;
196
197 fn from_be_bytes(bytes: Self::ByteArray) -> Self;
198 fn from_le_bytes(bytes: Self::ByteArray) -> Self;
199 fn from_ne_bytes(bytes: Self::ByteArray) -> Self;
200
201 fn abs(self) -> Self;
203
204 fn signum(self) -> Self;
206
207 #[cfg(feature = "std")]
208 fn div_euclid(self, rhs: Self) -> Self;
209
210 #[cfg(feature = "std")]
211 fn rem_euclid(self, rhs: Self) -> Self;
212}
213
214macro_rules! impl_number {
215 (
216 $ty:ty,
217 zero: $zero:expr,
218 one: $one:expr,
219 min: $min:expr,
220 max: $max:expr,
221 abs: $abs:expr,
222 signum: $signum:expr
223 ) => {
224 impl_number_like!($ty,
225 underlying: Self,
226 min: $min,
227 max: $max,
228 try_from_underlying: |v| Some(v)
229 );
230 impl Number for $ty {
231 const ZERO: Self = $zero;
232 const ONE: Self = $one;
233 const TWO: Self = $one + $one;
234
235 fn from_bytes(bytes: Self::ByteArray) -> Self {
236 unsafe { transmute::<Self::ByteArray, Self>(bytes) }
237 }
238
239 fn as_mut_bytes(&mut self) -> &mut Self::ByteArray {
240 unsafe { transmute::<&mut Self, &mut Self::ByteArray>(self) }
241 }
242
243 fn from_be_bytes(bytes: Self::ByteArray) -> Self {
244 Self::from_be_bytes(bytes)
245 }
246
247 fn from_le_bytes(bytes: Self::ByteArray) -> Self {
248 Self::from_le_bytes(bytes)
249 }
250
251 fn from_ne_bytes(bytes: Self::ByteArray) -> Self {
252 Self::from_ne_bytes(bytes)
253 }
254
255 fn abs(self) -> Self {
256 $abs(self)
257 }
258
259 fn signum(self) -> Self {
260 $signum(self)
261 }
262
263 #[cfg(feature = "std")]
264 fn div_euclid(self, rhs: Self) -> Self {
265 Self::div_euclid(self, rhs)
266 }
267
268 #[cfg(feature = "std")]
269 fn rem_euclid(self, rhs: Self) -> Self {
270 Self::rem_euclid(self, rhs)
271 }
272 }
273 };
274}
275
276pub trait Float: Number + From<f32> + From<bool> + Into<f64> {
277 const RADIX: u32;
278 const MANTISSA_DIGITS: u32;
279 const DIGITS: u32;
280 const EPSILON: Self;
281
282 const MIN_FINITE: Self;
283 const MIN_POSITIVE_SUBNORMAL: Self;
284 const MIN_POSITIVE_NORMAL: Self;
285 const MIN_EXP: i32;
286 const MIN_10_EXP: i32;
287
288 const MAX_FINITE: Self;
289 const MAX_NEGATIVE_SUBNORMAL: Self;
290 const MAX_NEGATIVE_NORMAL: Self;
291 const MAX_EXP: i32;
292 const MAX_10_EXP: i32;
293
294 const NAN: Self;
295 const INFINITY: Self;
296 const NEG_INFINITY: Self;
297
298 const NEG_ZERO: Self;
299
300 type Bits: Unsigned;
301
302 fn is_nan(self) -> bool;
307
308 fn is_infinite(self) -> bool;
310
311 fn is_finite(self) -> bool;
313
314 fn is_subnormal(self) -> bool;
316
317 fn is_normal(self) -> bool;
319
320 fn classify(self) -> FpCategory;
322
323 fn is_sign_positive(self) -> bool;
325
326 fn is_sign_negative(self) -> bool;
328
329 fn recip(self) -> Self;
331
332 fn to_degrees(self) -> Self;
334
335 fn to_radians(self) -> Self;
337
338 fn max(self, other: Self) -> Self;
340
341 fn min(self, other: Self) -> Self;
343
344 fn to_bits(self) -> Self::Bits;
346
347 fn from_bits(v: Self::Bits) -> Self;
349
350 fn total_cmp(&self, other: &Self) -> Ordering;
352
353 fn clamp(self, min: Self, max: Self) -> Self;
355
356 fn copysign(self, sign: Self) -> Self;
358
359 #[cfg(feature = "std")]
361 fn floor(self) -> Self;
362
363 #[cfg(feature = "std")]
365 fn ceil(self) -> Self;
366
367 #[cfg(feature = "std")]
369 fn round(self) -> Self;
370
371 #[cfg(feature = "std")]
373 fn round_ties_even(self) -> Self;
374
375 #[cfg(feature = "std")]
377 fn trunc(self) -> Self;
378
379 #[cfg(feature = "std")]
381 fn fract(self) -> Self;
382
383 #[cfg(feature = "std")]
385 fn mul_add(self, a: Self, b: Self) -> Self;
386
387 #[cfg(feature = "std")]
389 fn powi(self, n: i32) -> Self;
390
391 #[cfg(feature = "std")]
393 fn powf(self, n: Self) -> Self;
394
395 #[cfg(feature = "std")]
397 fn sqrt(self) -> Self;
398
399 #[cfg(feature = "std")]
401 fn exp(self) -> Self;
402
403 #[cfg(feature = "std")]
405 fn exp2(self) -> Self;
406
407 #[cfg(feature = "std")]
409 fn ln(self) -> Self;
410
411 #[cfg(feature = "std")]
413 fn log(self, base: Self) -> Self;
414
415 #[cfg(feature = "std")]
417 fn log2(self) -> Self;
418
419 #[cfg(feature = "std")]
421 fn log10(self) -> Self;
422
423 #[cfg(feature = "std")]
425 fn cbrt(self) -> Self;
426
427 #[cfg(feature = "std")]
429 fn hypot(self, other: Self) -> Self;
430
431 #[cfg(feature = "std")]
433 fn sin(self) -> Self;
434
435 #[cfg(feature = "std")]
437 fn cos(self) -> Self;
438
439 #[cfg(feature = "std")]
441 fn tan(self) -> Self;
442
443 #[cfg(feature = "std")]
445 fn asin(self) -> Self;
446
447 #[cfg(feature = "std")]
449 fn acos(self) -> Self;
450
451 #[cfg(feature = "std")]
453 fn atan(self) -> Self;
454
455 #[cfg(feature = "std")]
457 fn atan2(self, other: Self) -> Self;
458
459 #[cfg(feature = "std")]
461 fn sin_cos(self) -> (Self, Self);
462
463 #[cfg(feature = "std")]
465 fn exp_m1(self) -> Self;
466
467 #[cfg(feature = "std")]
469 fn ln_1p(self) -> Self;
470
471 #[cfg(feature = "std")]
473 fn sinh(self) -> Self;
474
475 #[cfg(feature = "std")]
477 fn cosh(self) -> Self;
478
479 #[cfg(feature = "std")]
481 fn tanh(self) -> Self;
482
483 #[cfg(feature = "std")]
485 fn asinh(self) -> Self;
486
487 #[cfg(feature = "std")]
489 fn acosh(self) -> Self;
490
491 #[cfg(feature = "std")]
493 fn atanh(self) -> Self;
494
495 }
497
498macro_rules! impl_float {
499 ($ty:ty, $bits:ty, $min_positive_subnormal:expr) => {
500 impl_number!(
501 $ty,
502 zero: 0.0,
503 one: 1.0,
504 min: Self::NEG_INFINITY,
505 max: Self::INFINITY,
506 abs: Self::abs,
507 signum: Self::signum
508 );
509 impl Float for $ty {
510 const RADIX: u32 = Self::RADIX;
511 const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS;
512 const DIGITS: u32 = Self::DIGITS;
513 const EPSILON: Self = Self::EPSILON;
514
515 const MIN_FINITE: Self = Self::MIN;
516 const MIN_POSITIVE_SUBNORMAL: Self = $min_positive_subnormal;
517 const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
518 const MIN_EXP: i32 = Self::MIN_EXP;
519 const MIN_10_EXP: i32 = Self::MIN_10_EXP;
520
521 const MAX_FINITE: Self = Self::MAX;
522 const MAX_NEGATIVE_SUBNORMAL: Self = -Self::MIN_POSITIVE_SUBNORMAL;
523 const MAX_NEGATIVE_NORMAL: Self = -Self::MIN_POSITIVE_NORMAL;
524 const MAX_EXP: i32 = Self::MAX_EXP;
525 const MAX_10_EXP: i32 = Self::MAX_10_EXP;
526
527 const NAN: Self = Self::NAN;
528 const INFINITY: Self = Self::INFINITY;
529 const NEG_INFINITY: Self = Self::NEG_INFINITY;
530
531 const NEG_ZERO: Self = -0.0;
532
533 type Bits = $bits;
534
535 fn is_nan(self) -> bool {
539 Self::is_nan(self)
540 }
541
542 fn is_infinite(self) -> bool {
543 Self::is_infinite(self)
544 }
545
546 fn is_finite(self) -> bool {
547 Self::is_finite(self)
548 }
549
550 fn is_subnormal(self) -> bool {
551 Self::is_subnormal(self)
552 }
553
554 fn is_normal(self) -> bool {
555 Self::is_normal(self)
556 }
557
558 fn classify(self) -> FpCategory {
559 Self::classify(self)
560 }
561
562 fn is_sign_positive(self) -> bool {
563 Self::is_sign_positive(self)
564 }
565
566 fn is_sign_negative(self) -> bool {
567 Self::is_sign_negative(self)
568 }
569
570 fn recip(self) -> Self {
571 Self::recip(self)
572 }
573
574 fn to_degrees(self) -> Self {
575 Self::to_degrees(self)
576 }
577
578 fn to_radians(self) -> Self {
579 Self::to_radians(self)
580 }
581
582 fn max(self, other: Self) -> Self {
583 Self::max(self, other)
584 }
585
586 fn min(self, other: Self) -> Self {
587 Self::min(self, other)
588 }
589
590 fn to_bits(self) -> Self::Bits {
591 Self::to_bits(self)
592 }
593
594 fn from_bits(v: Self::Bits) -> Self {
595 Self::from_bits(v)
596 }
597
598 fn total_cmp(&self, other: &Self) -> Ordering {
599 Self::total_cmp(&self, other)
600 }
601
602 fn clamp(self, min: Self, max: Self) -> Self {
603 Self::clamp(self, min, max)
604 }
605
606 fn copysign(self, sign: Self) -> Self {
607 Self::copysign(self, sign)
608 }
609
610 #[cfg(feature = "std")]
611 fn floor(self) -> Self {
612 Self::floor(self)
613 }
614
615 #[cfg(feature = "std")]
616 fn ceil(self) -> Self {
617 Self::ceil(self)
618 }
619
620 #[cfg(feature = "std")]
621 fn round(self) -> Self {
622 Self::round(self)
623 }
624
625 #[cfg(feature = "std")]
626 fn round_ties_even(self) -> Self {
627 Self::round_ties_even(self)
628 }
629
630 #[cfg(feature = "std")]
631 fn trunc(self) -> Self {
632 Self::trunc(self)
633 }
634
635 #[cfg(feature = "std")]
636 fn fract(self) -> Self {
637 Self::fract(self)
638 }
639
640 #[cfg(feature = "std")]
641 fn mul_add(self, a: Self, b: Self) -> Self {
642 Self::mul_add(self, a, b)
643 }
644
645 #[cfg(feature = "std")]
646 fn powi(self, n: i32) -> Self {
647 Self::powi(self, n)
648 }
649
650 #[cfg(feature = "std")]
651 fn powf(self, n: Self) -> Self {
652 Self::powf(self, n)
653 }
654
655 #[cfg(feature = "std")]
656 fn sqrt(self) -> Self {
657 Self::sqrt(self)
658 }
659
660 #[cfg(feature = "std")]
661 fn exp(self) -> Self {
662 Self::exp(self)
663 }
664
665 #[cfg(feature = "std")]
666 fn exp2(self) -> Self {
667 Self::exp2(self)
668 }
669
670 #[cfg(feature = "std")]
671 fn ln(self) -> Self {
672 Self::ln(self)
673 }
674
675 #[cfg(feature = "std")]
676 fn log(self, base: Self) -> Self {
677 Self::log(self, base)
678 }
679
680 #[cfg(feature = "std")]
681 fn log2(self) -> Self {
682 Self::log2(self)
683 }
684
685 #[cfg(feature = "std")]
686 fn log10(self) -> Self {
687 Self::log10(self)
688 }
689
690 #[cfg(feature = "std")]
691 fn cbrt(self) -> Self {
692 Self::cbrt(self)
693 }
694
695 #[cfg(feature = "std")]
696 fn hypot(self, other: Self) -> Self {
697 Self::hypot(self, other)
698 }
699
700 #[cfg(feature = "std")]
701 fn sin(self) -> Self {
702 Self::sin(self)
703 }
704
705 #[cfg(feature = "std")]
706 fn cos(self) -> Self {
707 Self::cos(self)
708 }
709
710 #[cfg(feature = "std")]
711 fn tan(self) -> Self {
712 Self::tan(self)
713 }
714
715 #[cfg(feature = "std")]
716 fn asin(self) -> Self {
717 Self::asin(self)
718 }
719
720 #[cfg(feature = "std")]
721 fn acos(self) -> Self {
722 Self::acos(self)
723 }
724
725 #[cfg(feature = "std")]
726 fn atan(self) -> Self {
727 Self::atan(self)
728 }
729
730 #[cfg(feature = "std")]
731 fn atan2(self, other: Self) -> Self {
732 Self::atan2(self, other)
733 }
734
735 #[cfg(feature = "std")]
736 fn sin_cos(self) -> (Self, Self) {
737 Self::sin_cos(self)
738 }
739
740 #[cfg(feature = "std")]
741 fn exp_m1(self) -> Self {
742 Self::exp_m1(self)
743 }
744
745 #[cfg(feature = "std")]
746 fn ln_1p(self) -> Self {
747 Self::ln_1p(self)
748 }
749
750 #[cfg(feature = "std")]
751 fn sinh(self) -> Self {
752 Self::sinh(self)
753 }
754
755 #[cfg(feature = "std")]
756 fn cosh(self) -> Self {
757 Self::cosh(self)
758 }
759
760 #[cfg(feature = "std")]
761 fn tanh(self) -> Self {
762 Self::tanh(self)
763 }
764
765 #[cfg(feature = "std")]
766 fn asinh(self) -> Self {
767 Self::asinh(self)
768 }
769
770 #[cfg(feature = "std")]
771 fn acosh(self) -> Self {
772 Self::acosh(self)
773 }
774
775 #[cfg(feature = "std")]
776 fn atanh(self) -> Self {
777 Self::atanh(self)
778 }
779
780 }
782 };
783}
784
785impl_float!(f32, u32, 1e-45);
786impl_float!(f64, u64, 5e-324);
787
788pub trait Integer:
789 Number
790 + Ord
791 + Eq
792 + Not
793 + BitAnd<Self>
794 + for<'a> BitAnd<&'a Self>
795 + BitAndAssign<Self>
796 + for<'a> BitAndAssign<&'a Self>
797 + BitOr<Self>
798 + for<'a> BitOr<&'a Self>
799 + BitOrAssign<Self>
800 + for<'a> BitOrAssign<&'a Self>
801 + BitXor<Self>
802 + for<'a> BitXor<&'a Self>
803 + BitXorAssign<Self>
804 + for<'a> BitXorAssign<&'a Self>
805 + Shl<Self>
806 + for<'a> Shl<&'a Self>
807 + ShlAssign<Self>
808 + for<'a> ShlAssign<&'a Self>
809 + Shr<Self>
810 + for<'a> Shr<&'a Self>
811 + ShrAssign<Self>
812 + for<'a> ShrAssign<&'a Self>
813 + TryFrom<u32>
814 + TryFrom<u64>
815 + TryFrom<u128>
816 + TryFrom<usize>
817 + TryFrom<i32>
818 + TryFrom<i64>
819 + TryFrom<i128>
820 + TryFrom<isize>
821 + TryInto<u8>
822 + TryInto<u16>
823 + TryInto<u32>
824 + TryInto<u64>
825 + TryInto<u128>
826 + TryInto<usize>
827 + TryInto<i8>
828 + TryInto<i16>
829 + TryInto<i32>
830 + TryInto<i64>
831 + TryInto<i128>
832 + TryInto<isize>
833 + Hash
834 + Binary
835 + Octal
836 + LowerHex
837 + UpperHex
838{
839 type Unsigned: Unsigned;
840 type Signed: Signed;
841
842 fn to_unsigned(self) -> Self::Unsigned;
843 fn to_signed(self) -> Self::Signed;
844
845 #[cfg(not(feature = "std"))]
847 fn div_euclid(self, rhs: Self) -> Self;
848
849 #[cfg(not(feature = "std"))]
851 fn rem_euclid(self, rhs: Self) -> Self;
852
853 fn count_ones(self) -> u32;
858
859 fn count_zeros(self) -> u32;
861
862 fn leading_zeros(self) -> u32;
864
865 fn trailing_zeros(self) -> u32;
867
868 fn leading_ones(self) -> u32;
870
871 fn trailing_ones(self) -> u32;
873
874 fn rotate_left(self, n: u32) -> Self;
876
877 fn rotate_right(self, n: u32) -> Self;
879
880 fn swap_bytes(self) -> Self;
882
883 fn reverse_bits(self) -> Self;
885
886 fn from_be(x: Self) -> Self;
888
889 fn from_le(x: Self) -> Self;
891
892 fn to_be(self) -> Self;
894
895 fn to_le(self) -> Self;
897
898 fn checked_add(self, rhs: Self) -> Option<Self>;
900
901 unsafe fn unchecked_add(self, rhs: Self) -> Self;
907
908 fn checked_sub(self, rhs: Self) -> Option<Self>;
910
911 unsafe fn unchecked_sub(self, rhs: Self) -> Self;
917
918 fn checked_mul(self, rhs: Self) -> Option<Self>;
920
921 unsafe fn unchecked_mul(self, rhs: Self) -> Self;
927
928 fn checked_div(self, rhs: Self) -> Option<Self>;
930
931 fn checked_div_euclid(self, rhs: Self) -> Option<Self>;
933
934 fn checked_rem(self, rhs: Self) -> Option<Self>;
936
937 fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;
939
940 fn checked_neg(self) -> Option<Self>;
942
943 fn checked_shl(self, rhs: u32) -> Option<Self>;
945
946 fn checked_shr(self, rhs: u32) -> Option<Self>;
948
949 fn checked_pow(self, exp: u32) -> Option<Self>;
951
952 fn saturating_add(self, rhs: Self) -> Self;
954
955 fn saturating_sub(self, rhs: Self) -> Self;
957
958 fn saturating_mul(self, rhs: Self) -> Self;
960
961 fn saturating_div(self, rhs: Self) -> Self;
963
964 fn saturating_pow(self, exp: u32) -> Self;
966
967 fn wrapping_add(self, rhs: Self) -> Self;
969
970 fn wrapping_sub(self, rhs: Self) -> Self;
972
973 fn wrapping_mul(self, rhs: Self) -> Self;
975
976 fn wrapping_div(self, rhs: Self) -> Self;
978
979 fn wrapping_div_euclid(self, rhs: Self) -> Self;
981
982 fn wrapping_rem(self, rhs: Self) -> Self;
984
985 fn wrapping_rem_euclid(self, rhs: Self) -> Self;
987
988 fn wrapping_neg(self) -> Self;
990
991 fn wrapping_shl(self, rhs: u32) -> Self;
993
994 fn wrapping_shr(self, rhs: u32) -> Self;
996
997 fn wrapping_pow(self, exp: u32) -> Self;
999
1000 fn overflowing_add(self, rhs: Self) -> (Self, bool);
1002
1003 fn overflowing_sub(self, rhs: Self) -> (Self, bool);
1005
1006 fn overflowing_mul(self, rhs: Self) -> (Self, bool);
1008
1009 fn overflowing_div(self, rhs: Self) -> (Self, bool);
1011
1012 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);
1014
1015 fn overflowing_rem(self, rhs: Self) -> (Self, bool);
1017
1018 fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool);
1020
1021 fn overflowing_neg(self) -> (Self, bool);
1023
1024 fn overflowing_shl(self, rhs: u32) -> (Self, bool);
1026
1027 fn overflowing_shr(self, rhs: u32) -> (Self, bool);
1029
1030 fn overflowing_pow(self, exp: u32) -> (Self, bool);
1032
1033 fn pow(self, exp: u32) -> Self;
1035
1036 fn isqrt(self) -> Self;
1038
1039 fn ilog(self, base: Self) -> u32;
1041
1042 fn ilog2(self) -> u32;
1044
1045 fn ilog10(self) -> u32;
1047
1048 fn checked_ilog(self, base: Self) -> Option<u32>;
1050
1051 fn checked_ilog2(self) -> Option<u32>;
1053
1054 fn checked_ilog10(self) -> Option<u32>;
1056
1057 fn abs_diff(self, other: Self) -> Self::Unsigned;
1059
1060 fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>;
1062
1063 }
1065
1066macro_rules! impl_integer {
1067 ($ty:ty, $unsigned:ty, $signed:ty, abs: $abs:expr, signum: $signum:expr) => {
1068 impl_number!(
1069 $ty,
1070 zero: 0,
1071 one: 1,
1072 min: Self::MIN,
1073 max: Self::MAX,
1074 abs: $abs,
1075 signum: $signum
1076 );
1077 impl Integer for $ty {
1078 type Unsigned = $unsigned;
1079 type Signed = $signed;
1080
1081 fn to_unsigned(self) -> Self::Unsigned {
1082 #[allow(clippy::useless_transmute)]
1083 unsafe { transmute::<Self, Self::Unsigned>(self) }
1084 }
1085
1086 fn to_signed(self) -> Self::Signed {
1087 #[allow(clippy::useless_transmute)]
1088 unsafe { transmute::<Self, Self::Signed>(self) }
1089 }
1090
1091 #[cfg(not(feature = "std"))]
1092 fn div_euclid(self, rhs: Self) -> Self {
1093 Self::div_euclid(self, rhs)
1094 }
1095
1096 #[cfg(not(feature = "std"))]
1097 fn rem_euclid(self, rhs: Self) -> Self {
1098 Self::rem_euclid(self, rhs)
1099 }
1100
1101 fn count_ones(self) -> u32 {
1105 Self::count_ones(self)
1106 }
1107
1108 fn count_zeros(self) -> u32 {
1109 Self::count_zeros(self)
1110 }
1111
1112 fn leading_zeros(self) -> u32 {
1113 Self::leading_zeros(self)
1114 }
1115
1116 fn trailing_zeros(self) -> u32 {
1117 Self::trailing_zeros(self)
1118 }
1119
1120 fn leading_ones(self) -> u32 {
1121 Self::leading_ones(self)
1122 }
1123
1124 fn trailing_ones(self) -> u32 {
1125 Self::trailing_ones(self)
1126 }
1127
1128 fn rotate_left(self, n: u32) -> Self {
1129 Self::rotate_left(self, n)
1130 }
1131
1132 fn rotate_right(self, n: u32) -> Self {
1133 Self::rotate_right(self, n)
1134 }
1135
1136 fn swap_bytes(self) -> Self {
1137 Self::swap_bytes(self)
1138 }
1139
1140 fn reverse_bits(self) -> Self {
1141 Self::reverse_bits(self)
1142 }
1143
1144 fn from_be(x: Self) -> Self {
1145 Self::from_be(x)
1146 }
1147
1148 fn from_le(x: Self) -> Self {
1149 Self::from_le(x)
1150 }
1151
1152 fn to_be(self) -> Self {
1153 Self::to_be(self)
1154 }
1155
1156 fn to_le(self) -> Self {
1157 Self::to_le(self)
1158 }
1159
1160 fn checked_add(self, rhs: Self) -> Option<Self> {
1161 Self::checked_add(self, rhs)
1162 }
1163
1164 unsafe fn unchecked_add(self, rhs: Self) -> Self {
1165 Self::unchecked_add(self, rhs)
1166 }
1167
1168 fn checked_sub(self, rhs: Self) -> Option<Self> {
1169 Self::checked_sub(self, rhs)
1170 }
1171
1172 unsafe fn unchecked_sub(self, rhs: Self) -> Self {
1173 Self::unchecked_sub(self, rhs)
1174 }
1175
1176 fn checked_mul(self, rhs: Self) -> Option<Self> {
1177 Self::checked_mul(self, rhs)
1178 }
1179
1180 unsafe fn unchecked_mul(self, rhs: Self) -> Self {
1181 Self::unchecked_mul(self, rhs)
1182 }
1183
1184 fn checked_div(self, rhs: Self) -> Option<Self> {
1185 Self::checked_div(self, rhs)
1186 }
1187
1188 fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
1189 Self::checked_div_euclid(self, rhs)
1190 }
1191
1192 fn checked_rem(self, rhs: Self) -> Option<Self> {
1193 Self::checked_rem(self, rhs)
1194 }
1195
1196 fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1197 Self::checked_rem_euclid(self, rhs)
1198 }
1199
1200 fn checked_neg(self) -> Option<Self> {
1201 Self::checked_neg(self)
1202 }
1203
1204 fn checked_shl(self, rhs: u32) -> Option<Self> {
1205 Self::checked_shl(self, rhs)
1206 }
1207
1208 fn checked_shr(self, rhs: u32) -> Option<Self> {
1209 Self::checked_shr(self, rhs)
1210 }
1211
1212 fn checked_pow(self, exp: u32) -> Option<Self> {
1213 Self::checked_pow(self, exp)
1214 }
1215
1216 fn saturating_add(self, rhs: Self) -> Self {
1217 Self::saturating_add(self, rhs)
1218 }
1219
1220 fn saturating_sub(self, rhs: Self) -> Self {
1221 Self::saturating_sub(self, rhs)
1222 }
1223
1224 fn saturating_mul(self, rhs: Self) -> Self {
1225 Self::saturating_mul(self, rhs)
1226 }
1227
1228 fn saturating_div(self, rhs: Self) -> Self {
1229 Self::saturating_div(self, rhs)
1230 }
1231
1232 fn saturating_pow(self, exp: u32) -> Self {
1233 Self::saturating_pow(self, exp)
1234 }
1235
1236 fn wrapping_add(self, rhs: Self) -> Self {
1237 Self::wrapping_add(self, rhs)
1238 }
1239
1240 fn wrapping_sub(self, rhs: Self) -> Self {
1241 Self::wrapping_sub(self, rhs)
1242 }
1243
1244 fn wrapping_mul(self, rhs: Self) -> Self {
1245 Self::wrapping_mul(self, rhs)
1246 }
1247
1248 fn wrapping_div(self, rhs: Self) -> Self {
1249 Self::wrapping_div(self, rhs)
1250 }
1251
1252 fn wrapping_div_euclid(self, rhs: Self) -> Self {
1253 Self::wrapping_div_euclid(self, rhs)
1254 }
1255
1256 fn wrapping_rem(self, rhs: Self) -> Self {
1257 Self::wrapping_rem(self, rhs)
1258 }
1259
1260 fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1261 Self::wrapping_rem_euclid(self, rhs)
1262 }
1263
1264 fn wrapping_neg(self) -> Self {
1265 Self::wrapping_neg(self)
1266 }
1267
1268 fn wrapping_shl(self, rhs: u32) -> Self {
1269 Self::wrapping_shl(self, rhs)
1270 }
1271
1272 fn wrapping_shr(self, rhs: u32) -> Self {
1273 Self::wrapping_shr(self, rhs)
1274 }
1275
1276 fn wrapping_pow(self, exp: u32) -> Self {
1277 Self::wrapping_pow(self, exp)
1278 }
1279
1280 fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1281 Self::overflowing_add(self, rhs)
1282 }
1283
1284 fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1285 Self::overflowing_sub(self, rhs)
1286 }
1287
1288 fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1289 Self::overflowing_mul(self, rhs)
1290 }
1291
1292 fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1293 Self::overflowing_div(self, rhs)
1294 }
1295
1296 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1297 Self::overflowing_div_euclid(self, rhs)
1298 }
1299
1300 fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1301 Self::overflowing_rem(self, rhs)
1302 }
1303
1304 fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1305 Self::overflowing_rem_euclid(self, rhs)
1306 }
1307
1308 fn overflowing_neg(self) -> (Self, bool) {
1309 Self::overflowing_neg(self)
1310 }
1311
1312 fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1313 Self::overflowing_shl(self, rhs)
1314 }
1315
1316 fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1317 Self::overflowing_shr(self, rhs)
1318 }
1319
1320 fn overflowing_pow(self, exp: u32) -> (Self, bool) {
1321 Self::overflowing_pow(self, exp)
1322 }
1323
1324 fn pow(self, exp: u32) -> Self {
1325 Self::pow(self, exp)
1326 }
1327
1328 fn isqrt(self) -> Self {
1329 Self::isqrt(self)
1330 }
1331
1332 fn ilog(self, base: Self) -> u32 {
1333 Self::ilog(self, base)
1334 }
1335
1336 fn ilog2(self) -> u32 {
1337 Self::ilog2(self)
1338 }
1339
1340 fn ilog10(self) -> u32 {
1341 Self::ilog10(self)
1342 }
1343
1344 fn checked_ilog(self, base: Self) -> Option<u32> {
1345 Self::checked_ilog(self, base)
1346 }
1347
1348 fn checked_ilog2(self) -> Option<u32> {
1349 Self::checked_ilog2(self)
1350 }
1351
1352 fn checked_ilog10(self) -> Option<u32> {
1353 Self::checked_ilog10(self)
1354 }
1355
1356 fn abs_diff(self, other: Self) -> Self::Unsigned {
1357 Self::abs_diff(self, other)
1358 }
1359
1360 fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
1361 Self::from_str_radix(src, radix)
1362 }
1363
1364 }
1366 };
1367}
1368
1369pub trait Unsigned: Integer + From<u8> {
1370 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
1375
1376 fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
1378
1379 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
1381
1382 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
1384
1385 fn div_ceil(self, rhs: Self) -> Self;
1387
1388 fn next_multiple_of(self, rhs: Self) -> Self;
1390
1391 fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>;
1393
1394 fn is_power_of_two(self) -> bool;
1396
1397 fn next_power_of_two(self) -> Self;
1399
1400 fn checked_next_power_of_two(self) -> Option<Self>;
1402
1403 }
1405
1406macro_rules! impl_unsigned {
1407 ($ty:ty, $signed:ty) => {
1408 impl_integer!(
1409 $ty,
1410 Self,
1411 $signed,
1412 abs: |v| v,
1413 signum: |v| (v > 0) as Self
1414 );
1415 impl Unsigned for $ty {
1416 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self> {
1420 Self::checked_add_signed(self, rhs)
1421 }
1422
1423 fn saturating_add_signed(self, rhs: Self::Signed) -> Self {
1424 Self::saturating_add_signed(self, rhs)
1425 }
1426
1427 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self {
1428 Self::wrapping_add_signed(self, rhs)
1429 }
1430
1431 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool) {
1432 Self::overflowing_add_signed(self, rhs)
1433 }
1434
1435 fn div_ceil(self, rhs: Self) -> Self {
1436 Self::div_ceil(self, rhs)
1437 }
1438
1439 fn next_multiple_of(self, rhs: Self) -> Self {
1440 Self::next_multiple_of(self, rhs)
1441 }
1442
1443 fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
1444 Self::checked_next_multiple_of(self, rhs)
1445 }
1446
1447 fn is_power_of_two(self) -> bool {
1448 Self::is_power_of_two(self)
1449 }
1450
1451 fn next_power_of_two(self) -> Self {
1452 Self::next_power_of_two(self)
1453 }
1454
1455 fn checked_next_power_of_two(self) -> Option<Self> {
1456 Self::checked_next_power_of_two(self)
1457 }
1458
1459 }
1461 };
1462}
1463
1464impl_unsigned!(u8, i8);
1465impl_unsigned!(u16, i16);
1466impl_unsigned!(u32, i32);
1467impl_unsigned!(u64, i64);
1468impl_unsigned!(u128, i128);
1469impl_unsigned!(usize, isize);
1470
1471pub trait Signed: Integer + Neg + From<i8> {
1472 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
1477
1478 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
1480
1481 fn checked_abs(self) -> Option<Self>;
1483
1484 fn checked_isqrt(self) -> Option<Self>;
1486
1487 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self;
1489
1490 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
1492
1493 fn saturating_neg(self) -> Self;
1495
1496 fn saturating_abs(self) -> Self;
1498
1499 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self;
1501
1502 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
1504
1505 fn wrapping_abs(self) -> Self;
1507
1508 fn unsigned_abs(self) -> Self::Unsigned;
1510
1511 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
1513
1514 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
1516
1517 fn overflowing_abs(self) -> (Self, bool);
1519
1520 fn abs(self) -> Self;
1522
1523 fn signum(self) -> Self;
1525
1526 fn is_positive(self) -> bool;
1528
1529 fn is_negative(self) -> bool;
1531
1532 }
1534
1535macro_rules! impl_signed {
1536 ($ty:ty, $unsigned:ty) => {
1537 impl_integer!($ty, $unsigned, Self, abs: Self::abs, signum: Self::signum);
1538 impl Signed for $ty {
1539 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self> {
1543 Self::checked_add_unsigned(self, rhs)
1544 }
1545
1546 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self> {
1547 Self::checked_sub_unsigned(self, rhs)
1548 }
1549
1550 fn checked_abs(self) -> Option<Self> {
1551 Self::checked_abs(self)
1552 }
1553
1554 fn checked_isqrt(self) -> Option<Self> {
1555 Self::checked_isqrt(self)
1556 }
1557
1558 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self {
1559 Self::saturating_add_unsigned(self, rhs)
1560 }
1561
1562 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
1563 Self::saturating_sub_unsigned(self, rhs)
1564 }
1565
1566 fn saturating_neg(self) -> Self {
1567 Self::saturating_neg(self)
1568 }
1569
1570 fn saturating_abs(self) -> Self {
1571 Self::saturating_abs(self)
1572 }
1573
1574 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self {
1575 Self::wrapping_add_unsigned(self, rhs)
1576 }
1577
1578 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
1579 Self::wrapping_sub_unsigned(self, rhs)
1580 }
1581
1582 fn wrapping_abs(self) -> Self {
1583 Self::wrapping_abs(self)
1584 }
1585
1586 fn unsigned_abs(self) -> Self::Unsigned {
1587 Self::unsigned_abs(self)
1588 }
1589
1590 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool) {
1591 Self::overflowing_add_unsigned(self, rhs)
1592 }
1593
1594 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool) {
1595 Self::overflowing_sub_unsigned(self, rhs)
1596 }
1597
1598 fn overflowing_abs(self) -> (Self, bool) {
1599 Self::overflowing_abs(self)
1600 }
1601
1602 fn abs(self) -> Self {
1603 Self::abs(self)
1604 }
1605
1606 fn signum(self) -> Self {
1607 Self::signum(self)
1608 }
1609
1610 fn is_positive(self) -> bool {
1611 Self::is_positive(self)
1612 }
1613
1614 fn is_negative(self) -> bool {
1615 Self::is_negative(self)
1616 }
1617
1618 }
1620 };
1621}
1622
1623impl_signed!(i8, u8);
1624impl_signed!(i16, u16);
1625impl_signed!(i32, u32);
1626impl_signed!(i64, u64);
1627impl_signed!(i128, u128);
1628impl_signed!(isize, usize);
1629
1630#[cfg(test)]
1631mod test {
1632 use super::*;
1633
1634 #[test]
1635 fn test_subnormal_consts() {
1636 assert_eq!(f32::MIN_POSITIVE_SUBNORMAL, f32::from_bits(1));
1637 assert_eq!(f32::MAX_NEGATIVE_SUBNORMAL, -f32::from_bits(1));
1638 assert_eq!(f64::MIN_POSITIVE_SUBNORMAL, f64::from_bits(1));
1639 assert_eq!(f64::MAX_NEGATIVE_SUBNORMAL, -f64::from_bits(1));
1640 }
1641
1642 #[cfg(feature = "std")]
1643 #[test]
1644 fn test_float_floor() {
1645 assert_eq!(<f64 as Float>::floor(1.5), 1.0);
1646 }
1647
1648 #[test]
1649 fn test_euclid_core() {
1650 fn test_int<T: Integer>(a: T, b: T) -> (T, T) {
1651 (a.div_euclid(b), a.rem_euclid(b))
1652 }
1653
1654 assert_eq!(test_int(-7, 4), (-2, 1));
1655 }
1656
1657 #[cfg(feature = "std")]
1658 #[test]
1659 fn test_euclid_std() {
1660 fn test_num<T: Number>(a: T, b: T) -> (T, T) {
1661 (a.div_euclid(b), a.rem_euclid(b))
1662 }
1663
1664 assert_eq!(test_num(-7, 4), (-2, 1));
1665 assert_eq!(test_num(-7.0, 4.0), (-2.0, 1.0));
1666 }
1667
1668 #[test]
1669 fn test_abs() {
1670 fn test_abs<T: Number>(a: T) -> T {
1671 a.abs()
1672 }
1673
1674 assert_eq!(test_abs(1i32), 1);
1675 assert_eq!(test_abs(1u32), 1);
1676 assert_eq!(test_abs(1.0), 1.0);
1677
1678 assert_eq!(test_abs(-1i32), 1);
1679 assert_eq!(test_abs(-1.0), 1.0);
1680
1681 assert!(test_abs(f64::NAN).is_nan());
1682 }
1683
1684 #[test]
1685 fn test_signum() {
1686 fn test_signum<T: Number>(a: T) -> T {
1687 a.signum()
1688 }
1689
1690 assert_eq!(test_signum(123i32), 1);
1691 assert_eq!(test_signum(123u32), 1);
1692 assert_eq!(test_signum(123.0), 1.0);
1693
1694 assert_eq!(test_signum(0i32), 0);
1695 assert_eq!(test_signum(0u32), 0);
1696 assert_eq!(test_signum(0.0), 1.0);
1697 assert_eq!(test_signum(-0.0), -1.0);
1698
1699 assert_eq!(test_signum(-123i32), -1);
1700 assert_eq!(test_signum(-123.0), -1.0);
1701
1702 assert!(test_signum(f64::NAN).is_nan());
1703 }
1704}