1use core::{
18 fmt::{Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp, UpperHex},
19 hash::Hash,
20 iter::{Product, Sum},
21 mem::{size_of, transmute},
22 num::{FpCategory, ParseIntError},
23 ops::{
24 Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div,
25 DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub,
26 SubAssign,
27 },
28 panic::{RefUnwindSafe, UnwindSafe},
29 str::FromStr,
30};
31
32use crate::{
33 array::Array,
34 primitive::Primitive,
35 ptr::{Pointer, cast_ptr},
36 util::{cast_generic, cast_int},
37};
38
39pub trait NumberLike:
40 Primitive
41 + Copy
42 + Default
43 + FromStr
44 + PartialEq
45 + PartialOrd
46 + Debug
47 + Display
48 + Unpin
49 + UnwindSafe
50 + RefUnwindSafe
51 + Send
52 + Sync
53 + Sized
54 + 'static
55{
56 const MIN: Self;
60 const MAX: Self;
64
65 type Underlying: Number;
66 type ByteArray: Array<Item = u8>;
67
68 fn to_underlying(self) -> Self::Underlying;
69 fn try_from_underlying(underlying: Self::Underlying) -> Option<Self>;
70 fn to_bytes(self) -> Self::ByteArray;
71 fn try_from_bytes(bytes: Self::ByteArray) -> Option<Self>;
72
73 fn to_be_bytes(self) -> Self::ByteArray;
74 fn to_le_bytes(self) -> Self::ByteArray;
75 fn to_ne_bytes(self) -> Self::ByteArray;
76 fn try_from_be_bytes(bytes: Self::ByteArray) -> Option<Self>;
77 fn try_from_le_bytes(bytes: Self::ByteArray) -> Option<Self>;
78 fn try_from_ne_bytes(bytes: Self::ByteArray) -> Option<Self>;
79
80 fn cast_int<T: Integer>(self) -> T;
81}
82
83macro_rules! impl_number_like {
84 (
85 $ty:ty,
86 underlying: $number:ty,
87 min: $min:expr,
88 max: $max:expr,
89 try_from_underlying: $try_from_underlying:expr
90 ) => {
91 impl Primitive for $ty {}
92 impl NumberLike for $ty {
93 const MIN: Self = $min;
94 const MAX: Self = $max;
95
96 type Underlying = $number;
97 type ByteArray = [u8; size_of::<Self>()];
98
99 fn to_underlying(self) -> Self::Underlying {
100 #[allow(clippy::useless_transmute)]
101 #[allow(unnecessary_transmutes)]
102 unsafe {
103 transmute::<Self, Self::Underlying>(self)
104 }
105 }
106
107 fn try_from_underlying(underlying: Self::Underlying) -> Option<Self> {
108 $try_from_underlying(underlying)
109 }
110
111 fn to_bytes(self) -> Self::ByteArray {
112 #[allow(unnecessary_transmutes)]
113 unsafe {
114 transmute::<Self, Self::ByteArray>(self)
115 }
116 }
117
118 fn try_from_bytes(bytes: Self::ByteArray) -> Option<Self> {
119 Self::try_from_underlying(Self::Underlying::from_bytes(bytes))
120 }
121
122 fn to_be_bytes(self) -> Self::ByteArray {
123 self.to_underlying().to_be_bytes()
124 }
125
126 fn to_le_bytes(self) -> Self::ByteArray {
127 self.to_underlying().to_le_bytes()
128 }
129
130 fn to_ne_bytes(self) -> Self::ByteArray {
131 self.to_underlying().to_ne_bytes()
132 }
133
134 fn try_from_be_bytes(bytes: Self::ByteArray) -> Option<Self> {
135 Self::try_from_underlying(Self::Underlying::from_be_bytes(bytes))
136 }
137
138 fn try_from_le_bytes(bytes: Self::ByteArray) -> Option<Self> {
139 Self::try_from_underlying(Self::Underlying::from_le_bytes(bytes))
140 }
141
142 fn try_from_ne_bytes(bytes: Self::ByteArray) -> Option<Self> {
143 Self::try_from_underlying(Self::Underlying::from_ne_bytes(bytes))
144 }
145
146 fn cast_int<T: Integer>(self) -> T {
147 cast_int!(self => T)
148 }
149 }
150 };
151}
152
153impl_number_like!(bool,
154 underlying: u8,
155 min: false,
156 max: true,
157 try_from_underlying: |v| match v {
158 0 => Some(false),
159 1 => Some(true),
160 _ => None,
161 }
162);
163impl_number_like!(char,
164 underlying: u32,
165 min: '\0',
166 max: '\u{10ffff}',
167 try_from_underlying: |v| char::try_from(v).ok()
168);
169
170pub trait Number:
171 NumberLike
172 + LowerExp
173 + UpperExp
174 + Add<Self, Output = Self>
175 + for<'a> Add<&'a Self, Output = Self>
176 + AddAssign<Self>
177 + for<'a> AddAssign<&'a Self>
178 + Sub<Self, Output = Self>
179 + for<'a> Sub<&'a Self, Output = Self>
180 + SubAssign<Self>
181 + for<'a> SubAssign<&'a Self>
182 + Mul<Self, Output = Self>
183 + for<'a> Mul<&'a Self, Output = Self>
184 + MulAssign<Self>
185 + for<'a> MulAssign<&'a Self>
186 + Div<Self, Output = Self>
187 + for<'a> Div<&'a Self, Output = Self>
188 + DivAssign<Self>
189 + for<'a> DivAssign<&'a Self>
190 + Rem<Self, Output = Self>
191 + for<'a> Rem<&'a Self, Output = Self>
192 + RemAssign<Self>
193 + for<'a> RemAssign<&'a Self>
194 + From<bool>
195 + TryFrom<u8>
196 + TryFrom<u16>
197 + TryFrom<i8>
198 + TryFrom<i16>
199 + Sum
200 + Product
201{
202 const ZERO: Self;
203 const ONE: Self;
204 const TWO: Self;
205
206 fn from_bytes(bytes: Self::ByteArray) -> Self;
207 fn as_mut_bytes(&mut self) -> &mut Self::ByteArray;
208
209 fn from_be_bytes(bytes: Self::ByteArray) -> Self;
210 fn from_le_bytes(bytes: Self::ByteArray) -> Self;
211 fn from_ne_bytes(bytes: Self::ByteArray) -> Self;
212
213 fn cast_float<T: Float>(self) -> T;
214 fn cast_number<T: Number>(self) -> T;
215
216 fn abs(self) -> Self;
218
219 fn signum(self) -> Self;
221
222 #[cfg(feature = "std")]
223 fn div_euclid(self, rhs: Self) -> Self;
224
225 #[cfg(feature = "std")]
226 fn rem_euclid(self, rhs: Self) -> Self;
227}
228
229macro_rules! impl_number {
230 (
231 $ty:ty,
232 zero: $zero:expr,
233 one: $one:expr,
234 min: $min:expr,
235 max: $max:expr,
236 abs: $abs:expr,
237 signum: $signum:expr
238 ) => {
239 impl_number_like!($ty,
240 underlying: Self,
241 min: $min,
242 max: $max,
243 try_from_underlying: |v| Some(v)
244 );
245 impl Number for $ty {
246 const ZERO: Self = $zero;
247 const ONE: Self = $one;
248 const TWO: Self = $one + $one;
249
250 fn from_bytes(bytes: Self::ByteArray) -> Self {
251 #[allow(unnecessary_transmutes)]
252 unsafe { transmute::<Self::ByteArray, Self>(bytes) }
253 }
254
255 fn as_mut_bytes(&mut self) -> &mut Self::ByteArray {
256 unsafe { transmute::<&mut Self, &mut Self::ByteArray>(self) }
257 }
258
259 fn from_be_bytes(bytes: Self::ByteArray) -> Self {
260 Self::from_be_bytes(bytes)
261 }
262
263 fn from_le_bytes(bytes: Self::ByteArray) -> Self {
264 Self::from_le_bytes(bytes)
265 }
266
267 fn from_ne_bytes(bytes: Self::ByteArray) -> Self {
268 Self::from_ne_bytes(bytes)
269 }
270
271 fn cast_float<T: Float>(self) -> T {
272 cast_generic!(self => T; f32 f64)
273 }
274
275 fn cast_number<T: Number>(self) -> T {
276 cast_generic!(self => T;
277 i8 i16 i32 i64 i128 isize
278 u8 u16 u32 u64 u128 usize
279 f32 f64
280 )
281 }
282
283 fn abs(self) -> Self {
284 $abs(self)
285 }
286
287 fn signum(self) -> Self {
288 $signum(self)
289 }
290
291 #[cfg(feature = "std")]
292 fn div_euclid(self, rhs: Self) -> Self {
293 Self::div_euclid(self, rhs)
294 }
295
296 #[cfg(feature = "std")]
297 fn rem_euclid(self, rhs: Self) -> Self {
298 Self::rem_euclid(self, rhs)
299 }
300 }
301 };
302}
303
304#[rustfmt::skip] pub trait Float:
306 Number
307 + Neg<Output = Self>
308 + From<f32>
309 + Into<f64>
310 + From<i8>
311 + From<i16>
312 + From<u8>
313 + From<u16>
314{
315 const RADIX: u32;
316 const MANTISSA_DIGITS: u32;
317 const DIGITS: u32;
318 const EPSILON: Self;
319
320 const MIN_FINITE: Self;
321 const MIN_POSITIVE_SUBNORMAL: Self;
322 const MIN_POSITIVE_NORMAL: Self;
323 const MIN_EXP: i32;
324 const MIN_10_EXP: i32;
325
326 const MAX_FINITE: Self;
327 const MAX_NEGATIVE_SUBNORMAL: Self;
328 const MAX_NEGATIVE_NORMAL: Self;
329 const MAX_EXP: i32;
330 const MAX_10_EXP: i32;
331
332 const NAN: Self;
333 const INFINITY: Self;
334 const NEG_INFINITY: Self;
335
336 const NEG_ZERO: Self;
337
338 type Bits: Unsigned;
339
340 fn is_nan(self) -> bool;
345
346 fn is_infinite(self) -> bool;
348
349 fn is_finite(self) -> bool;
351
352 fn is_subnormal(self) -> bool;
354
355 fn is_normal(self) -> bool;
357
358 fn classify(self) -> FpCategory;
360
361 fn is_sign_positive(self) -> bool;
363
364 fn is_sign_negative(self) -> bool;
366
367 fn next_up(self) -> Self;
369
370 fn next_down(self) -> Self;
372
373 #[must_use = "this returns the result of the operation, without modifying the original"]
375 fn recip(self) -> Self;
376
377 #[must_use = "this returns the result of the operation, without modifying the original"]
379 fn to_degrees(self) -> Self;
380
381 #[must_use = "this returns the result of the operation, without modifying the original"]
383 fn to_radians(self) -> Self;
384
385 #[must_use = "this returns the result of the comparison, without modifying either input"]
387 fn max(self, other: Self) -> Self;
388
389 #[must_use = "this returns the result of the comparison, without modifying either input"]
391 fn min(self, other: Self) -> Self;
392
393 fn midpoint(self, other: Self) -> Self;
395
396 #[must_use = "this returns the result of the operation, without modifying the original"]
398 fn to_bits(self) -> Self::Bits;
399
400 fn from_bits(v: Self::Bits) -> Self;
402
403 fn total_cmp(&self, other: &Self) -> core::cmp::Ordering;
405
406 #[must_use = "method returns a new number and does not mutate the original value"]
408 fn clamp(self, min: Self, max: Self) -> Self;
409
410 #[must_use = "method returns a new number and does not mutate the original value"]
412 fn copysign(self, sign: Self) -> Self;
413
414 #[cfg(feature = "std")]
416 #[must_use = "method returns a new number and does not mutate the original value"]
417 fn floor(self) -> Self;
418
419 #[cfg(feature = "std")]
421 #[must_use = "method returns a new number and does not mutate the original value"]
422 fn ceil(self) -> Self;
423
424 #[cfg(feature = "std")]
426 #[must_use = "method returns a new number and does not mutate the original value"]
427 fn round(self) -> Self;
428
429 #[cfg(feature = "std")]
431 #[must_use = "method returns a new number and does not mutate the original value"]
432 fn round_ties_even(self) -> Self;
433
434 #[cfg(feature = "std")]
436 #[must_use = "method returns a new number and does not mutate the original value"]
437 fn trunc(self) -> Self;
438
439 #[cfg(feature = "std")]
441 #[must_use = "method returns a new number and does not mutate the original value"]
442 fn fract(self) -> Self;
443
444 #[cfg(feature = "std")]
446 #[must_use = "method returns a new number and does not mutate the original value"]
447 fn mul_add(self, a: Self, b: Self) -> Self;
448
449 #[cfg(feature = "std")]
451 #[must_use = "method returns a new number and does not mutate the original value"]
452 fn powi(self, n: i32) -> Self;
453
454 #[cfg(feature = "std")]
456 #[must_use = "method returns a new number and does not mutate the original value"]
457 fn powf(self, n: Self) -> Self;
458
459 #[cfg(feature = "std")]
461 #[must_use = "method returns a new number and does not mutate the original value"]
462 fn sqrt(self) -> Self;
463
464 #[cfg(feature = "std")]
466 #[must_use = "method returns a new number and does not mutate the original value"]
467 fn exp(self) -> Self;
468
469 #[cfg(feature = "std")]
471 #[must_use = "method returns a new number and does not mutate the original value"]
472 fn exp2(self) -> Self;
473
474 #[cfg(feature = "std")]
476 #[must_use = "method returns a new number and does not mutate the original value"]
477 fn ln(self) -> Self;
478
479 #[cfg(feature = "std")]
481 #[must_use = "method returns a new number and does not mutate the original value"]
482 fn log(self, base: Self) -> Self;
483
484 #[cfg(feature = "std")]
486 #[must_use = "method returns a new number and does not mutate the original value"]
487 fn log2(self) -> Self;
488
489 #[cfg(feature = "std")]
491 #[must_use = "method returns a new number and does not mutate the original value"]
492 fn log10(self) -> Self;
493
494 #[cfg(feature = "std")]
496 #[must_use = "method returns a new number and does not mutate the original value"]
497 fn cbrt(self) -> Self;
498
499 #[cfg(feature = "std")]
501 #[must_use = "method returns a new number and does not mutate the original value"]
502 fn hypot(self, other: Self) -> Self;
503
504 #[cfg(feature = "std")]
506 #[must_use = "method returns a new number and does not mutate the original value"]
507 fn sin(self) -> Self;
508
509 #[cfg(feature = "std")]
511 #[must_use = "method returns a new number and does not mutate the original value"]
512 fn cos(self) -> Self;
513
514 #[cfg(feature = "std")]
516 #[must_use = "method returns a new number and does not mutate the original value"]
517 fn tan(self) -> Self;
518
519 #[cfg(feature = "std")]
521 #[must_use = "method returns a new number and does not mutate the original value"]
522 fn asin(self) -> Self;
523
524 #[cfg(feature = "std")]
526 #[must_use = "method returns a new number and does not mutate the original value"]
527 fn acos(self) -> Self;
528
529 #[cfg(feature = "std")]
531 #[must_use = "method returns a new number and does not mutate the original value"]
532 fn atan(self) -> Self;
533
534 #[cfg(feature = "std")]
536 #[must_use = "method returns a new number and does not mutate the original value"]
537 fn atan2(self, other: Self) -> Self;
538
539 #[cfg(feature = "std")]
541 fn sin_cos(self) -> (Self, Self);
542
543 #[cfg(feature = "std")]
545 #[must_use = "method returns a new number and does not mutate the original value"]
546 fn exp_m1(self) -> Self;
547
548 #[cfg(feature = "std")]
550 #[must_use = "method returns a new number and does not mutate the original value"]
551 fn ln_1p(self) -> Self;
552
553 #[cfg(feature = "std")]
555 #[must_use = "method returns a new number and does not mutate the original value"]
556 fn sinh(self) -> Self;
557
558 #[cfg(feature = "std")]
560 #[must_use = "method returns a new number and does not mutate the original value"]
561 fn cosh(self) -> Self;
562
563 #[cfg(feature = "std")]
565 #[must_use = "method returns a new number and does not mutate the original value"]
566 fn tanh(self) -> Self;
567
568 #[cfg(feature = "std")]
570 #[must_use = "method returns a new number and does not mutate the original value"]
571 fn asinh(self) -> Self;
572
573 #[cfg(feature = "std")]
575 #[must_use = "method returns a new number and does not mutate the original value"]
576 fn acosh(self) -> Self;
577
578 #[cfg(feature = "std")]
580 #[must_use = "method returns a new number and does not mutate the original value"]
581 fn atanh(self) -> Self;
582
583 }
585
586macro_rules! impl_float {
587 ($ty:ty, $bits:ty, $min_positive_subnormal:expr) => {
588 impl_number!(
589 $ty,
590 zero: 0.0,
591 one: 1.0,
592 min: Self::NEG_INFINITY,
593 max: Self::INFINITY,
594 abs: Self::abs,
595 signum: Self::signum
596 );
597 impl Float for $ty {
598 const RADIX: u32 = Self::RADIX;
599 const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS;
600 const DIGITS: u32 = Self::DIGITS;
601 const EPSILON: Self = Self::EPSILON;
602
603 const MIN_FINITE: Self = Self::MIN;
604 const MIN_POSITIVE_SUBNORMAL: Self = $min_positive_subnormal;
605 const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
606 const MIN_EXP: i32 = Self::MIN_EXP;
607 const MIN_10_EXP: i32 = Self::MIN_10_EXP;
608
609 const MAX_FINITE: Self = Self::MAX;
610 const MAX_NEGATIVE_SUBNORMAL: Self = -Self::MIN_POSITIVE_SUBNORMAL;
611 const MAX_NEGATIVE_NORMAL: Self = -Self::MIN_POSITIVE_NORMAL;
612 const MAX_EXP: i32 = Self::MAX_EXP;
613 const MAX_10_EXP: i32 = Self::MAX_10_EXP;
614
615 const NAN: Self = Self::NAN;
616 const INFINITY: Self = Self::INFINITY;
617 const NEG_INFINITY: Self = Self::NEG_INFINITY;
618
619 const NEG_ZERO: Self = -0.0;
620
621 type Bits = $bits;
622
623 fn is_nan(self) -> bool {
627 Self::is_nan(self)
628 }
629
630 fn is_infinite(self) -> bool {
631 Self::is_infinite(self)
632 }
633
634 fn is_finite(self) -> bool {
635 Self::is_finite(self)
636 }
637
638 fn is_subnormal(self) -> bool {
639 Self::is_subnormal(self)
640 }
641
642 fn is_normal(self) -> bool {
643 Self::is_normal(self)
644 }
645
646 fn classify(self) -> FpCategory {
647 Self::classify(self)
648 }
649
650 fn is_sign_positive(self) -> bool {
651 Self::is_sign_positive(self)
652 }
653
654 fn is_sign_negative(self) -> bool {
655 Self::is_sign_negative(self)
656 }
657
658 fn next_up(self) -> Self {
659 Self::next_up(self)
660 }
661
662 fn next_down(self) -> Self {
663 Self::next_down(self)
664 }
665
666 fn recip(self) -> Self {
667 Self::recip(self)
668 }
669
670 fn to_degrees(self) -> Self {
671 Self::to_degrees(self)
672 }
673
674 fn to_radians(self) -> Self {
675 Self::to_radians(self)
676 }
677
678 fn max(self, other: Self) -> Self {
679 Self::max(self, other)
680 }
681
682 fn min(self, other: Self) -> Self {
683 Self::min(self, other)
684 }
685
686 fn midpoint(self, other: Self) -> Self {
687 Self::midpoint(self, other)
688 }
689
690 fn to_bits(self) -> Self::Bits {
691 Self::to_bits(self)
692 }
693
694 fn from_bits(v: Self::Bits) -> Self {
695 Self::from_bits(v)
696 }
697
698 fn total_cmp(&self, other: &Self) -> core::cmp::Ordering {
699 Self::total_cmp(self, other)
700 }
701
702 fn clamp(self, min: Self, max: Self) -> Self {
703 Self::clamp(self, min, max)
704 }
705
706 fn copysign(self, sign: Self) -> Self {
707 Self::copysign(self, sign)
708 }
709
710 #[cfg(feature = "std")]
711 fn floor(self) -> Self {
712 Self::floor(self)
713 }
714
715 #[cfg(feature = "std")]
716 fn ceil(self) -> Self {
717 Self::ceil(self)
718 }
719
720 #[cfg(feature = "std")]
721 fn round(self) -> Self {
722 Self::round(self)
723 }
724
725 #[cfg(feature = "std")]
726 fn round_ties_even(self) -> Self {
727 Self::round_ties_even(self)
728 }
729
730 #[cfg(feature = "std")]
731 fn trunc(self) -> Self {
732 Self::trunc(self)
733 }
734
735 #[cfg(feature = "std")]
736 fn fract(self) -> Self {
737 Self::fract(self)
738 }
739
740 #[cfg(feature = "std")]
741 fn mul_add(self, a: Self, b: Self) -> Self {
742 Self::mul_add(self, a, b)
743 }
744
745 #[cfg(feature = "std")]
746 fn powi(self, n: i32) -> Self {
747 Self::powi(self, n)
748 }
749
750 #[cfg(feature = "std")]
751 fn powf(self, n: Self) -> Self {
752 Self::powf(self, n)
753 }
754
755 #[cfg(feature = "std")]
756 fn sqrt(self) -> Self {
757 Self::sqrt(self)
758 }
759
760 #[cfg(feature = "std")]
761 fn exp(self) -> Self {
762 Self::exp(self)
763 }
764
765 #[cfg(feature = "std")]
766 fn exp2(self) -> Self {
767 Self::exp2(self)
768 }
769
770 #[cfg(feature = "std")]
771 fn ln(self) -> Self {
772 Self::ln(self)
773 }
774
775 #[cfg(feature = "std")]
776 fn log(self, base: Self) -> Self {
777 Self::log(self, base)
778 }
779
780 #[cfg(feature = "std")]
781 fn log2(self) -> Self {
782 Self::log2(self)
783 }
784
785 #[cfg(feature = "std")]
786 fn log10(self) -> Self {
787 Self::log10(self)
788 }
789
790 #[cfg(feature = "std")]
791 fn cbrt(self) -> Self {
792 Self::cbrt(self)
793 }
794
795 #[cfg(feature = "std")]
796 fn hypot(self, other: Self) -> Self {
797 Self::hypot(self, other)
798 }
799
800 #[cfg(feature = "std")]
801 fn sin(self) -> Self {
802 Self::sin(self)
803 }
804
805 #[cfg(feature = "std")]
806 fn cos(self) -> Self {
807 Self::cos(self)
808 }
809
810 #[cfg(feature = "std")]
811 fn tan(self) -> Self {
812 Self::tan(self)
813 }
814
815 #[cfg(feature = "std")]
816 fn asin(self) -> Self {
817 Self::asin(self)
818 }
819
820 #[cfg(feature = "std")]
821 fn acos(self) -> Self {
822 Self::acos(self)
823 }
824
825 #[cfg(feature = "std")]
826 fn atan(self) -> Self {
827 Self::atan(self)
828 }
829
830 #[cfg(feature = "std")]
831 fn atan2(self, other: Self) -> Self {
832 Self::atan2(self, other)
833 }
834
835 #[cfg(feature = "std")]
836 fn sin_cos(self) -> (Self, Self) {
837 Self::sin_cos(self)
838 }
839
840 #[cfg(feature = "std")]
841 fn exp_m1(self) -> Self {
842 Self::exp_m1(self)
843 }
844
845 #[cfg(feature = "std")]
846 fn ln_1p(self) -> Self {
847 Self::ln_1p(self)
848 }
849
850 #[cfg(feature = "std")]
851 fn sinh(self) -> Self {
852 Self::sinh(self)
853 }
854
855 #[cfg(feature = "std")]
856 fn cosh(self) -> Self {
857 Self::cosh(self)
858 }
859
860 #[cfg(feature = "std")]
861 fn tanh(self) -> Self {
862 Self::tanh(self)
863 }
864
865 #[cfg(feature = "std")]
866 fn asinh(self) -> Self {
867 Self::asinh(self)
868 }
869
870 #[cfg(feature = "std")]
871 fn acosh(self) -> Self {
872 Self::acosh(self)
873 }
874
875 #[cfg(feature = "std")]
876 fn atanh(self) -> Self {
877 Self::atanh(self)
878 }
879
880 }
882 };
883}
884
885impl_float!(f32, u32, 1e-45);
886impl_float!(f64, u64, 5e-324);
887
888pub trait Integer:
889 Number
890 + Ord
891 + Eq
892 + Not<Output = Self>
893 + BitAnd<Self, Output = Self>
894 + for<'a> BitAnd<&'a Self, Output = Self>
895 + BitAndAssign<Self>
896 + for<'a> BitAndAssign<&'a Self>
897 + BitOr<Self, Output = Self>
898 + for<'a> BitOr<&'a Self, Output = Self>
899 + BitOrAssign<Self>
900 + for<'a> BitOrAssign<&'a Self>
901 + BitXor<Self, Output = Self>
902 + for<'a> BitXor<&'a Self, Output = Self>
903 + BitXorAssign<Self>
904 + for<'a> BitXorAssign<&'a Self>
905 + Shl<Self, Output = Self>
906 + for<'a> Shl<&'a Self, Output = Self>
907 + ShlAssign<Self>
908 + for<'a> ShlAssign<&'a Self>
909 + Shr<Self, Output = Self>
910 + for<'a> Shr<&'a Self, Output = Self>
911 + ShrAssign<Self>
912 + for<'a> ShrAssign<&'a Self>
913 + TryFrom<u32>
914 + TryFrom<u64>
915 + TryFrom<u128>
916 + TryFrom<usize>
917 + TryFrom<i32>
918 + TryFrom<i64>
919 + TryFrom<i128>
920 + TryFrom<isize>
921 + TryInto<u8>
922 + TryInto<u16>
923 + TryInto<u32>
924 + TryInto<u64>
925 + TryInto<u128>
926 + TryInto<usize>
927 + TryInto<i8>
928 + TryInto<i16>
929 + TryInto<i32>
930 + TryInto<i64>
931 + TryInto<i128>
932 + TryInto<isize>
933 + Hash
934 + Binary
935 + Octal
936 + LowerHex
937 + UpperHex
938{
939 type Unsigned: Unsigned;
940 type Signed: Signed;
941
942 fn from_unsigned(v: Self::Unsigned) -> Self;
943 fn from_signed(v: Self::Signed) -> Self;
944 fn to_unsigned(self) -> Self::Unsigned;
945 fn to_signed(self) -> Self::Signed;
946
947 fn cast_ptr<T: Sized, P: Pointer<T>>(self) -> P;
948
949 #[cfg(not(feature = "std"))]
951 fn div_euclid(self, rhs: Self) -> Self;
952
953 #[cfg(not(feature = "std"))]
955 fn rem_euclid(self, rhs: Self) -> Self;
956
957 #[must_use = "this returns the result of the operation, without modifying the original"]
962 fn count_ones(self) -> u32;
963
964 #[must_use = "this returns the result of the operation, without modifying the original"]
966 fn count_zeros(self) -> u32;
967
968 #[must_use = "this returns the result of the operation, without modifying the original"]
970 fn leading_zeros(self) -> u32;
971
972 #[must_use = "this returns the result of the operation, without modifying the original"]
974 fn trailing_zeros(self) -> u32;
975
976 #[must_use = "this returns the result of the operation, without modifying the original"]
978 fn leading_ones(self) -> u32;
979
980 #[must_use = "this returns the result of the operation, without modifying the original"]
982 fn trailing_ones(self) -> u32;
983
984 #[must_use = "this returns the result of the operation, without modifying the original"]
986 fn rotate_left(self, n: u32) -> Self;
987
988 #[must_use = "this returns the result of the operation, without modifying the original"]
990 fn rotate_right(self, n: u32) -> Self;
991
992 #[must_use = "this returns the result of the operation, without modifying the original"]
994 fn swap_bytes(self) -> Self;
995
996 #[must_use = "this returns the result of the operation, without modifying the original"]
998 fn reverse_bits(self) -> Self;
999
1000 fn from_be(x: Self) -> Self;
1002
1003 fn from_le(x: Self) -> Self;
1005
1006 #[must_use = "this returns the result of the operation, without modifying the original"]
1008 fn to_be(self) -> Self;
1009
1010 #[must_use = "this returns the result of the operation, without modifying the original"]
1012 fn to_le(self) -> Self;
1013
1014 #[must_use = "this returns the result of the operation, without modifying the original"]
1016 fn checked_add(self, rhs: Self) -> Option<Self>;
1017
1018 #[must_use = "this returns the result of the operation, without modifying the original"]
1020 fn strict_add(self, rhs: Self) -> Self;
1021
1022 #[must_use = "this returns the result of the operation, without modifying the original"]
1028 unsafe fn unchecked_add(self, rhs: Self) -> Self;
1029
1030 #[must_use = "this returns the result of the operation, without modifying the original"]
1032 fn checked_sub(self, rhs: Self) -> Option<Self>;
1033
1034 #[must_use = "this returns the result of the operation, without modifying the original"]
1036 fn strict_sub(self, rhs: Self) -> Self;
1037
1038 #[must_use = "this returns the result of the operation, without modifying the original"]
1044 unsafe fn unchecked_sub(self, rhs: Self) -> Self;
1045
1046 #[must_use = "this returns the result of the operation, without modifying the original"]
1048 fn checked_mul(self, rhs: Self) -> Option<Self>;
1049
1050 #[must_use = "this returns the result of the operation, without modifying the original"]
1052 fn strict_mul(self, rhs: Self) -> Self;
1053
1054 #[must_use = "this returns the result of the operation, without modifying the original"]
1060 unsafe fn unchecked_mul(self, rhs: Self) -> Self;
1061
1062 #[must_use = "this returns the result of the operation, without modifying the original"]
1064 fn checked_div(self, rhs: Self) -> Option<Self>;
1065
1066 #[must_use = "this returns the result of the operation, without modifying the original"]
1068 fn strict_div(self, rhs: Self) -> Self;
1069
1070 #[must_use = "this returns the result of the operation, without modifying the original"]
1072 fn checked_div_euclid(self, rhs: Self) -> Option<Self>;
1073
1074 #[must_use = "this returns the result of the operation, without modifying the original"]
1076 fn strict_div_euclid(self, rhs: Self) -> Self;
1077
1078 #[must_use = "this returns the result of the operation, without modifying the original"]
1080 fn checked_rem(self, rhs: Self) -> Option<Self>;
1081
1082 #[must_use = "this returns the result of the operation, without modifying the original"]
1084 fn strict_rem(self, rhs: Self) -> Self;
1085
1086 #[must_use = "this returns the result of the operation, without modifying the original"]
1088 fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;
1089
1090 #[must_use = "this returns the result of the operation, without modifying the original"]
1092 fn strict_rem_euclid(self, rhs: Self) -> Self;
1093
1094 #[must_use = "this returns the result of the operation, without modifying the original"]
1096 fn checked_neg(self) -> Option<Self>;
1097
1098 #[must_use = "this returns the result of the operation, without modifying the original"]
1100 fn strict_neg(self) -> Self;
1101
1102 #[must_use = "this returns the result of the operation, without modifying the original"]
1104 fn checked_shl(self, rhs: u32) -> Option<Self>;
1105
1106 #[must_use = "this returns the result of the operation, without modifying the original"]
1108 fn strict_shl(self, rhs: u32) -> Self;
1109
1110 #[must_use = "this returns the result of the operation, without modifying the original"]
1116 unsafe fn unchecked_shl(self, rhs: u32) -> Self;
1117
1118 #[must_use = "this returns the result of the operation, without modifying the original"]
1120 fn unbounded_shl(self, rhs: u32) -> Self;
1121
1122 #[must_use = "this returns the result of the operation, without modifying the original"]
1124 fn checked_shr(self, rhs: u32) -> Option<Self>;
1125
1126 #[must_use = "this returns the result of the operation, without modifying the original"]
1128 fn strict_shr(self, rhs: u32) -> Self;
1129
1130 #[must_use = "this returns the result of the operation, without modifying the original"]
1136 unsafe fn unchecked_shr(self, rhs: u32) -> Self;
1137
1138 #[must_use = "this returns the result of the operation, without modifying the original"]
1140 fn unbounded_shr(self, rhs: u32) -> Self;
1141
1142 #[must_use = "this returns the result of the operation, without modifying the original"]
1144 fn checked_pow(self, exp: u32) -> Option<Self>;
1145
1146 #[must_use = "this returns the result of the operation, without modifying the original"]
1148 fn strict_pow(self, exp: u32) -> Self;
1149
1150 #[must_use = "this returns the result of the operation, without modifying the original"]
1152 fn saturating_add(self, rhs: Self) -> Self;
1153
1154 #[must_use = "this returns the result of the operation, without modifying the original"]
1156 fn saturating_sub(self, rhs: Self) -> Self;
1157
1158 #[must_use = "this returns the result of the operation, without modifying the original"]
1160 fn saturating_mul(self, rhs: Self) -> Self;
1161
1162 #[must_use = "this returns the result of the operation, without modifying the original"]
1164 fn saturating_div(self, rhs: Self) -> Self;
1165
1166 #[must_use = "this returns the result of the operation, without modifying the original"]
1168 fn saturating_pow(self, exp: u32) -> Self;
1169
1170 #[must_use = "this returns the result of the operation, without modifying the original"]
1172 fn wrapping_add(self, rhs: Self) -> Self;
1173
1174 #[must_use = "this returns the result of the operation, without modifying the original"]
1176 fn wrapping_sub(self, rhs: Self) -> Self;
1177
1178 #[must_use = "this returns the result of the operation, without modifying the original"]
1180 fn wrapping_mul(self, rhs: Self) -> Self;
1181
1182 #[must_use = "this returns the result of the operation, without modifying the original"]
1184 fn wrapping_div(self, rhs: Self) -> Self;
1185
1186 #[must_use = "this returns the result of the operation, without modifying the original"]
1188 fn wrapping_div_euclid(self, rhs: Self) -> Self;
1189
1190 #[must_use = "this returns the result of the operation, without modifying the original"]
1192 fn wrapping_rem(self, rhs: Self) -> Self;
1193
1194 #[must_use = "this returns the result of the operation, without modifying the original"]
1196 fn wrapping_rem_euclid(self, rhs: Self) -> Self;
1197
1198 #[must_use = "this returns the result of the operation, without modifying the original"]
1200 fn wrapping_neg(self) -> Self;
1201
1202 #[must_use = "this returns the result of the operation, without modifying the original"]
1204 fn wrapping_shl(self, rhs: u32) -> Self;
1205
1206 #[must_use = "this returns the result of the operation, without modifying the original"]
1208 fn wrapping_shr(self, rhs: u32) -> Self;
1209
1210 #[must_use = "this returns the result of the operation, without modifying the original"]
1212 fn wrapping_pow(self, exp: u32) -> Self;
1213
1214 #[must_use = "this returns the result of the operation, without modifying the original"]
1216 fn overflowing_add(self, rhs: Self) -> (Self, bool);
1217
1218 #[must_use = "this returns the result of the operation, without modifying the original"]
1220 fn overflowing_sub(self, rhs: Self) -> (Self, bool);
1221
1222 #[must_use = "this returns the result of the operation, without modifying the original"]
1224 fn overflowing_mul(self, rhs: Self) -> (Self, bool);
1225
1226 #[must_use = "this returns the result of the operation, without modifying the original"]
1228 fn overflowing_div(self, rhs: Self) -> (Self, bool);
1229
1230 #[must_use = "this returns the result of the operation, without modifying the original"]
1232 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);
1233
1234 #[must_use = "this returns the result of the operation, without modifying the original"]
1236 fn overflowing_rem(self, rhs: Self) -> (Self, bool);
1237
1238 #[must_use = "this returns the result of the operation, without modifying the original"]
1240 fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool);
1241
1242 #[must_use = "this returns the result of the operation, without modifying the original"]
1244 fn overflowing_neg(self) -> (Self, bool);
1245
1246 #[must_use = "this returns the result of the operation, without modifying the original"]
1248 fn overflowing_shl(self, rhs: u32) -> (Self, bool);
1249
1250 #[must_use = "this returns the result of the operation, without modifying the original"]
1252 fn overflowing_shr(self, rhs: u32) -> (Self, bool);
1253
1254 #[must_use = "this returns the result of the operation, without modifying the original"]
1256 fn overflowing_pow(self, exp: u32) -> (Self, bool);
1257
1258 #[must_use = "this returns the result of the operation, without modifying the original"]
1260 fn pow(self, exp: u32) -> Self;
1261
1262 #[must_use = "this returns the result of the operation, without modifying the original"]
1264 fn isqrt(self) -> Self;
1265
1266 #[must_use = "this returns the result of the operation, without modifying the original"]
1268 fn ilog(self, base: Self) -> u32;
1269
1270 #[must_use = "this returns the result of the operation, without modifying the original"]
1272 fn ilog2(self) -> u32;
1273
1274 #[must_use = "this returns the result of the operation, without modifying the original"]
1276 fn ilog10(self) -> u32;
1277
1278 #[must_use = "this returns the result of the operation, without modifying the original"]
1280 fn checked_ilog(self, base: Self) -> Option<u32>;
1281
1282 #[must_use = "this returns the result of the operation, without modifying the original"]
1284 fn checked_ilog2(self) -> Option<u32>;
1285
1286 #[must_use = "this returns the result of the operation, without modifying the original"]
1288 fn checked_ilog10(self) -> Option<u32>;
1289
1290 #[must_use = "this returns the result of the operation, without modifying the original"]
1292 fn abs_diff(self, other: Self) -> Self::Unsigned;
1293
1294 #[must_use = "this returns the result of the operation, without modifying the original"]
1296 fn midpoint(self, rhs: Self) -> Self;
1297
1298 fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>;
1300
1301 }
1303
1304macro_rules! impl_integer {
1305 ($ty:ty, $unsigned:ty, $signed:ty, abs: $abs:expr, signum: $signum:expr) => {
1306 impl_number!(
1307 $ty,
1308 zero: 0,
1309 one: 1,
1310 min: Self::MIN,
1311 max: Self::MAX,
1312 abs: $abs,
1313 signum: $signum
1314 );
1315 impl Integer for $ty {
1316 type Unsigned = $unsigned;
1317 type Signed = $signed;
1318
1319 fn from_unsigned(v: Self::Unsigned) -> Self {
1320 #[allow(clippy::useless_transmute)]
1321 #[allow(unnecessary_transmutes)]
1322 unsafe { transmute::<Self::Unsigned, Self>(v) }
1323 }
1324
1325 fn from_signed(v: Self::Signed) -> Self {
1326 #[allow(clippy::useless_transmute)]
1327 #[allow(unnecessary_transmutes)]
1328 unsafe { transmute::<Self::Signed, Self>(v) }
1329 }
1330
1331 fn to_unsigned(self) -> Self::Unsigned {
1332 #[allow(clippy::useless_transmute)]
1333 #[allow(unnecessary_transmutes)]
1334 unsafe { transmute::<Self, Self::Unsigned>(self) }
1335 }
1336
1337 fn to_signed(self) -> Self::Signed {
1338 #[allow(clippy::useless_transmute)]
1339 #[allow(unnecessary_transmutes)]
1340 unsafe { transmute::<Self, Self::Signed>(self) }
1341 }
1342
1343 fn cast_ptr<T: Sized, P: Pointer<T>>(self) -> P {
1344 cast_ptr!(T, P, self)
1345 }
1346
1347 #[cfg(not(feature = "std"))]
1348 fn div_euclid(self, rhs: Self) -> Self {
1349 Self::div_euclid(self, rhs)
1350 }
1351
1352 #[cfg(not(feature = "std"))]
1353 fn rem_euclid(self, rhs: Self) -> Self {
1354 Self::rem_euclid(self, rhs)
1355 }
1356
1357 fn count_ones(self) -> u32 {
1361 Self::count_ones(self)
1362 }
1363
1364 fn count_zeros(self) -> u32 {
1365 Self::count_zeros(self)
1366 }
1367
1368 fn leading_zeros(self) -> u32 {
1369 Self::leading_zeros(self)
1370 }
1371
1372 fn trailing_zeros(self) -> u32 {
1373 Self::trailing_zeros(self)
1374 }
1375
1376 fn leading_ones(self) -> u32 {
1377 Self::leading_ones(self)
1378 }
1379
1380 fn trailing_ones(self) -> u32 {
1381 Self::trailing_ones(self)
1382 }
1383
1384 fn rotate_left(self, n: u32) -> Self {
1385 Self::rotate_left(self, n)
1386 }
1387
1388 fn rotate_right(self, n: u32) -> Self {
1389 Self::rotate_right(self, n)
1390 }
1391
1392 fn swap_bytes(self) -> Self {
1393 Self::swap_bytes(self)
1394 }
1395
1396 fn reverse_bits(self) -> Self {
1397 Self::reverse_bits(self)
1398 }
1399
1400 fn from_be(x: Self) -> Self {
1401 Self::from_be(x)
1402 }
1403
1404 fn from_le(x: Self) -> Self {
1405 Self::from_le(x)
1406 }
1407
1408 fn to_be(self) -> Self {
1409 Self::to_be(self)
1410 }
1411
1412 fn to_le(self) -> Self {
1413 Self::to_le(self)
1414 }
1415
1416 fn checked_add(self, rhs: Self) -> Option<Self> {
1417 Self::checked_add(self, rhs)
1418 }
1419
1420 fn strict_add(self, rhs: Self) -> Self {
1421 Self::strict_add(self, rhs)
1422 }
1423
1424 unsafe fn unchecked_add(self, rhs: Self) -> Self {
1425 unsafe { Self::unchecked_add(self, rhs) }
1426 }
1427
1428 fn checked_sub(self, rhs: Self) -> Option<Self> {
1429 Self::checked_sub(self, rhs)
1430 }
1431
1432 fn strict_sub(self, rhs: Self) -> Self {
1433 Self::strict_sub(self, rhs)
1434 }
1435
1436 unsafe fn unchecked_sub(self, rhs: Self) -> Self {
1437 unsafe { Self::unchecked_sub(self, rhs) }
1438 }
1439
1440 fn checked_mul(self, rhs: Self) -> Option<Self> {
1441 Self::checked_mul(self, rhs)
1442 }
1443
1444 fn strict_mul(self, rhs: Self) -> Self {
1445 Self::strict_mul(self, rhs)
1446 }
1447
1448 unsafe fn unchecked_mul(self, rhs: Self) -> Self {
1449 unsafe { Self::unchecked_mul(self, rhs) }
1450 }
1451
1452 fn checked_div(self, rhs: Self) -> Option<Self> {
1453 Self::checked_div(self, rhs)
1454 }
1455
1456 fn strict_div(self, rhs: Self) -> Self {
1457 Self::strict_div(self, rhs)
1458 }
1459
1460 fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
1461 Self::checked_div_euclid(self, rhs)
1462 }
1463
1464 fn strict_div_euclid(self, rhs: Self) -> Self {
1465 Self::strict_div_euclid(self, rhs)
1466 }
1467
1468 fn checked_rem(self, rhs: Self) -> Option<Self> {
1469 Self::checked_rem(self, rhs)
1470 }
1471
1472 fn strict_rem(self, rhs: Self) -> Self {
1473 Self::strict_rem(self, rhs)
1474 }
1475
1476 fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1477 Self::checked_rem_euclid(self, rhs)
1478 }
1479
1480 fn strict_rem_euclid(self, rhs: Self) -> Self {
1481 Self::strict_rem_euclid(self, rhs)
1482 }
1483
1484 fn checked_neg(self) -> Option<Self> {
1485 Self::checked_neg(self)
1486 }
1487
1488 fn strict_neg(self) -> Self {
1489 Self::strict_neg(self)
1490 }
1491
1492 fn checked_shl(self, rhs: u32) -> Option<Self> {
1493 Self::checked_shl(self, rhs)
1494 }
1495
1496 fn strict_shl(self, rhs: u32) -> Self {
1497 Self::strict_shl(self, rhs)
1498 }
1499
1500 unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1501 unsafe { Self::unchecked_shl(self, rhs) }
1502 }
1503
1504 fn unbounded_shl(self, rhs: u32) -> Self {
1505 Self::unbounded_shl(self, rhs)
1506 }
1507
1508 fn checked_shr(self, rhs: u32) -> Option<Self> {
1509 Self::checked_shr(self, rhs)
1510 }
1511
1512 fn strict_shr(self, rhs: u32) -> Self {
1513 Self::strict_shr(self, rhs)
1514 }
1515
1516 unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1517 unsafe { Self::unchecked_shr(self, rhs) }
1518 }
1519
1520 fn unbounded_shr(self, rhs: u32) -> Self {
1521 Self::unbounded_shr(self, rhs)
1522 }
1523
1524 fn checked_pow(self, exp: u32) -> Option<Self> {
1525 Self::checked_pow(self, exp)
1526 }
1527
1528 fn strict_pow(self, exp: u32) -> Self {
1529 Self::strict_pow(self, exp)
1530 }
1531
1532 fn saturating_add(self, rhs: Self) -> Self {
1533 Self::saturating_add(self, rhs)
1534 }
1535
1536 fn saturating_sub(self, rhs: Self) -> Self {
1537 Self::saturating_sub(self, rhs)
1538 }
1539
1540 fn saturating_mul(self, rhs: Self) -> Self {
1541 Self::saturating_mul(self, rhs)
1542 }
1543
1544 fn saturating_div(self, rhs: Self) -> Self {
1545 Self::saturating_div(self, rhs)
1546 }
1547
1548 fn saturating_pow(self, exp: u32) -> Self {
1549 Self::saturating_pow(self, exp)
1550 }
1551
1552 fn wrapping_add(self, rhs: Self) -> Self {
1553 Self::wrapping_add(self, rhs)
1554 }
1555
1556 fn wrapping_sub(self, rhs: Self) -> Self {
1557 Self::wrapping_sub(self, rhs)
1558 }
1559
1560 fn wrapping_mul(self, rhs: Self) -> Self {
1561 Self::wrapping_mul(self, rhs)
1562 }
1563
1564 fn wrapping_div(self, rhs: Self) -> Self {
1565 Self::wrapping_div(self, rhs)
1566 }
1567
1568 fn wrapping_div_euclid(self, rhs: Self) -> Self {
1569 Self::wrapping_div_euclid(self, rhs)
1570 }
1571
1572 fn wrapping_rem(self, rhs: Self) -> Self {
1573 Self::wrapping_rem(self, rhs)
1574 }
1575
1576 fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1577 Self::wrapping_rem_euclid(self, rhs)
1578 }
1579
1580 fn wrapping_neg(self) -> Self {
1581 Self::wrapping_neg(self)
1582 }
1583
1584 fn wrapping_shl(self, rhs: u32) -> Self {
1585 Self::wrapping_shl(self, rhs)
1586 }
1587
1588 fn wrapping_shr(self, rhs: u32) -> Self {
1589 Self::wrapping_shr(self, rhs)
1590 }
1591
1592 fn wrapping_pow(self, exp: u32) -> Self {
1593 Self::wrapping_pow(self, exp)
1594 }
1595
1596 fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1597 Self::overflowing_add(self, rhs)
1598 }
1599
1600 fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1601 Self::overflowing_sub(self, rhs)
1602 }
1603
1604 fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1605 Self::overflowing_mul(self, rhs)
1606 }
1607
1608 fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1609 Self::overflowing_div(self, rhs)
1610 }
1611
1612 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1613 Self::overflowing_div_euclid(self, rhs)
1614 }
1615
1616 fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1617 Self::overflowing_rem(self, rhs)
1618 }
1619
1620 fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1621 Self::overflowing_rem_euclid(self, rhs)
1622 }
1623
1624 fn overflowing_neg(self) -> (Self, bool) {
1625 Self::overflowing_neg(self)
1626 }
1627
1628 fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1629 Self::overflowing_shl(self, rhs)
1630 }
1631
1632 fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1633 Self::overflowing_shr(self, rhs)
1634 }
1635
1636 fn overflowing_pow(self, exp: u32) -> (Self, bool) {
1637 Self::overflowing_pow(self, exp)
1638 }
1639
1640 fn pow(self, exp: u32) -> Self {
1641 Self::pow(self, exp)
1642 }
1643
1644 fn isqrt(self) -> Self {
1645 Self::isqrt(self)
1646 }
1647
1648 fn ilog(self, base: Self) -> u32 {
1649 Self::ilog(self, base)
1650 }
1651
1652 fn ilog2(self) -> u32 {
1653 Self::ilog2(self)
1654 }
1655
1656 fn ilog10(self) -> u32 {
1657 Self::ilog10(self)
1658 }
1659
1660 fn checked_ilog(self, base: Self) -> Option<u32> {
1661 Self::checked_ilog(self, base)
1662 }
1663
1664 fn checked_ilog2(self) -> Option<u32> {
1665 Self::checked_ilog2(self)
1666 }
1667
1668 fn checked_ilog10(self) -> Option<u32> {
1669 Self::checked_ilog10(self)
1670 }
1671
1672 fn abs_diff(self, other: Self) -> Self::Unsigned {
1673 Self::abs_diff(self, other)
1674 }
1675
1676 fn midpoint(self, rhs: Self) -> Self {
1677 Self::midpoint(self, rhs)
1678 }
1679
1680 fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
1681 Self::from_str_radix(src, radix)
1682 }
1683
1684 }
1686 };
1687}
1688
1689pub trait Unsigned: Integer<Unsigned = Self> + From<u8> {
1690 #[must_use = "this returns the result of the operation, without modifying the original"]
1695 fn cast_signed(self) -> Self::Signed;
1696
1697 #[must_use = "this returns the result of the operation, without modifying the original"]
1699 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
1700
1701 #[must_use = "this returns the result of the operation, without modifying the original"]
1703 fn strict_add_signed(self, rhs: Self::Signed) -> Self;
1704
1705 #[must_use = "this returns the result of the operation, without modifying the original"]
1707 fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self>;
1708
1709 #[must_use = "this returns the result of the operation, without modifying the original"]
1711 fn strict_sub_signed(self, rhs: Self::Signed) -> Self;
1712
1713 fn checked_signed_diff(self, rhs: Self) -> Option<Self::Signed>;
1715
1716 #[must_use = "this returns the result of the operation, without modifying the original"]
1718 fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
1719
1720 #[must_use = "this returns the result of the operation, without modifying the original"]
1722 fn saturating_sub_signed(self, rhs: Self::Signed) -> Self;
1723
1724 #[must_use = "this returns the result of the operation, without modifying the original"]
1726 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
1727
1728 #[must_use = "this returns the result of the operation, without modifying the original"]
1730 fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self;
1731
1732 #[must_use = "this returns the result of the operation, without modifying the original"]
1734 fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool);
1735
1736 #[must_use = "this returns the result of the operation, without modifying the original"]
1738 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
1739
1740 #[must_use = "this returns the result of the operation, without modifying the original"]
1742 fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool);
1743
1744 #[must_use = "this returns the result of the operation, without modifying the original"]
1746 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool);
1747
1748 #[must_use = "this returns the result of the operation, without modifying the original"]
1750 fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self);
1751
1752 #[must_use = "this returns the result of the operation, without modifying the original"]
1754 fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> (Self, Self);
1755
1756 #[must_use = "this returns the result of the operation, without modifying the original"]
1758 fn div_ceil(self, rhs: Self) -> Self;
1759
1760 #[must_use = "this returns the result of the operation, without modifying the original"]
1762 fn next_multiple_of(self, rhs: Self) -> Self;
1763
1764 #[must_use = "this returns the result of the operation, without modifying the original"]
1766 fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>;
1767
1768 fn is_multiple_of(self, rhs: Self) -> bool;
1770
1771 fn is_power_of_two(self) -> bool;
1773
1774 #[must_use = "this returns the result of the operation, without modifying the original"]
1776 fn next_power_of_two(self) -> Self;
1777
1778 #[must_use = "this returns the result of the operation, without modifying the original"]
1780 fn checked_next_power_of_two(self) -> Option<Self>;
1781
1782 }
1784
1785macro_rules! impl_unsigned {
1786 ($ty:ty, $signed:ty) => {
1787 impl_integer!(
1788 $ty,
1789 Self,
1790 $signed,
1791 abs: |v| v,
1792 signum: |v| (v > 0) as Self
1793 );
1794 impl Unsigned for $ty {
1795 fn cast_signed(self) -> Self::Signed {
1799 Self::cast_signed(self)
1800 }
1801
1802 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self> {
1803 Self::checked_add_signed(self, rhs)
1804 }
1805
1806 fn strict_add_signed(self, rhs: Self::Signed) -> Self {
1807 Self::strict_add_signed(self, rhs)
1808 }
1809
1810 fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self> {
1811 Self::checked_sub_signed(self, rhs)
1812 }
1813
1814 fn strict_sub_signed(self, rhs: Self::Signed) -> Self {
1815 Self::strict_sub_signed(self, rhs)
1816 }
1817
1818 fn checked_signed_diff(self, rhs: Self) -> Option<Self::Signed> {
1819 Self::checked_signed_diff(self, rhs)
1820 }
1821
1822 fn saturating_add_signed(self, rhs: Self::Signed) -> Self {
1823 Self::saturating_add_signed(self, rhs)
1824 }
1825
1826 fn saturating_sub_signed(self, rhs: Self::Signed) -> Self {
1827 Self::saturating_sub_signed(self, rhs)
1828 }
1829
1830 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self {
1831 Self::wrapping_add_signed(self, rhs)
1832 }
1833
1834 fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self {
1835 Self::wrapping_sub_signed(self, rhs)
1836 }
1837
1838 fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
1839 Self::carrying_add(self, rhs, carry)
1840 }
1841
1842 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool) {
1843 Self::overflowing_add_signed(self, rhs)
1844 }
1845
1846 fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
1847 Self::borrowing_sub(self, rhs, borrow)
1848 }
1849
1850 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool) {
1851 Self::overflowing_sub_signed(self, rhs)
1852 }
1853
1854 fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
1855 Self::carrying_mul(self, rhs, carry)
1856 }
1857
1858 fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> (Self, Self) {
1859 Self::carrying_mul_add(self, rhs, carry, add)
1860 }
1861
1862 fn div_ceil(self, rhs: Self) -> Self {
1863 Self::div_ceil(self, rhs)
1864 }
1865
1866 fn next_multiple_of(self, rhs: Self) -> Self {
1867 Self::next_multiple_of(self, rhs)
1868 }
1869
1870 fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
1871 Self::checked_next_multiple_of(self, rhs)
1872 }
1873
1874 fn is_multiple_of(self, rhs: Self) -> bool {
1875 Self::is_multiple_of(self, rhs)
1876 }
1877
1878 fn is_power_of_two(self) -> bool {
1879 Self::is_power_of_two(self)
1880 }
1881
1882 fn next_power_of_two(self) -> Self {
1883 Self::next_power_of_two(self)
1884 }
1885
1886 fn checked_next_power_of_two(self) -> Option<Self> {
1887 Self::checked_next_power_of_two(self)
1888 }
1889
1890 }
1892 };
1893}
1894
1895impl_unsigned!(u8, i8);
1896impl_unsigned!(u16, i16);
1897impl_unsigned!(u32, i32);
1898impl_unsigned!(u64, i64);
1899impl_unsigned!(u128, i128);
1900impl_unsigned!(usize, isize);
1901
1902pub trait Signed: Integer<Signed = Self> + Neg<Output = Self> + From<i8> {
1903 #[must_use = "this returns the result of the operation, without modifying the original"]
1908 fn cast_unsigned(self) -> Self::Unsigned;
1909
1910 #[must_use = "this returns the result of the operation, without modifying the original"]
1912 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
1913
1914 #[must_use = "this returns the result of the operation, without modifying the original"]
1916 fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self;
1917
1918 #[must_use = "this returns the result of the operation, without modifying the original"]
1920 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
1921
1922 #[must_use = "this returns the result of the operation, without modifying the original"]
1924 fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
1925
1926 #[must_use = "this returns the result of the operation, without modifying the original"]
1932 unsafe fn unchecked_neg(self) -> Self;
1933
1934 #[must_use = "this returns the result of the operation, without modifying the original"]
1936 fn checked_abs(self) -> Option<Self>;
1937
1938 #[must_use = "this returns the result of the operation, without modifying the original"]
1940 fn strict_abs(self) -> Self;
1941
1942 #[must_use = "this returns the result of the operation, without modifying the original"]
1944 fn checked_isqrt(self) -> Option<Self>;
1945
1946 #[must_use = "this returns the result of the operation, without modifying the original"]
1948 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self;
1949
1950 #[must_use = "this returns the result of the operation, without modifying the original"]
1952 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
1953
1954 #[must_use = "this returns the result of the operation, without modifying the original"]
1956 fn saturating_neg(self) -> Self;
1957
1958 #[must_use = "this returns the result of the operation, without modifying the original"]
1960 fn saturating_abs(self) -> Self;
1961
1962 #[must_use = "this returns the result of the operation, without modifying the original"]
1964 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self;
1965
1966 #[must_use = "this returns the result of the operation, without modifying the original"]
1968 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
1969
1970 #[must_use = "this returns the result of the operation, without modifying the original"]
1972 fn wrapping_abs(self) -> Self;
1973
1974 #[must_use = "this returns the result of the operation, without modifying the original"]
1976 fn unsigned_abs(self) -> Self::Unsigned;
1977
1978 #[must_use = "this returns the result of the operation, without modifying the original"]
1980 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
1981
1982 #[must_use = "this returns the result of the operation, without modifying the original"]
1984 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
1985
1986 #[must_use = "this returns the result of the operation, without modifying the original"]
1988 fn overflowing_abs(self) -> (Self, bool);
1989
1990 #[must_use = "this returns the result of the operation, without modifying the original"]
1992 fn abs(self) -> Self;
1993
1994 #[must_use = "this returns the result of the operation, without modifying the original"]
1996 fn signum(self) -> Self;
1997
1998 fn is_positive(self) -> bool;
2000
2001 fn is_negative(self) -> bool;
2003
2004 }
2006
2007macro_rules! impl_signed {
2008 ($ty:ty, $unsigned:ty) => {
2009 impl_integer!($ty, $unsigned, Self, abs: Self::abs, signum: Self::signum);
2010 impl Signed for $ty {
2011 fn cast_unsigned(self) -> Self::Unsigned {
2015 Self::cast_unsigned(self)
2016 }
2017
2018 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self> {
2019 Self::checked_add_unsigned(self, rhs)
2020 }
2021
2022 fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self {
2023 Self::strict_add_unsigned(self, rhs)
2024 }
2025
2026 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self> {
2027 Self::checked_sub_unsigned(self, rhs)
2028 }
2029
2030 fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
2031 Self::strict_sub_unsigned(self, rhs)
2032 }
2033
2034 unsafe fn unchecked_neg(self) -> Self {
2035 unsafe { Self::unchecked_neg(self) }
2036 }
2037
2038 fn checked_abs(self) -> Option<Self> {
2039 Self::checked_abs(self)
2040 }
2041
2042 fn strict_abs(self) -> Self {
2043 Self::strict_abs(self)
2044 }
2045
2046 fn checked_isqrt(self) -> Option<Self> {
2047 Self::checked_isqrt(self)
2048 }
2049
2050 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self {
2051 Self::saturating_add_unsigned(self, rhs)
2052 }
2053
2054 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
2055 Self::saturating_sub_unsigned(self, rhs)
2056 }
2057
2058 fn saturating_neg(self) -> Self {
2059 Self::saturating_neg(self)
2060 }
2061
2062 fn saturating_abs(self) -> Self {
2063 Self::saturating_abs(self)
2064 }
2065
2066 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self {
2067 Self::wrapping_add_unsigned(self, rhs)
2068 }
2069
2070 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
2071 Self::wrapping_sub_unsigned(self, rhs)
2072 }
2073
2074 fn wrapping_abs(self) -> Self {
2075 Self::wrapping_abs(self)
2076 }
2077
2078 fn unsigned_abs(self) -> Self::Unsigned {
2079 Self::unsigned_abs(self)
2080 }
2081
2082 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool) {
2083 Self::overflowing_add_unsigned(self, rhs)
2084 }
2085
2086 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool) {
2087 Self::overflowing_sub_unsigned(self, rhs)
2088 }
2089
2090 fn overflowing_abs(self) -> (Self, bool) {
2091 Self::overflowing_abs(self)
2092 }
2093
2094 fn abs(self) -> Self {
2095 Self::abs(self)
2096 }
2097
2098 fn signum(self) -> Self {
2099 Self::signum(self)
2100 }
2101
2102 fn is_positive(self) -> bool {
2103 Self::is_positive(self)
2104 }
2105
2106 fn is_negative(self) -> bool {
2107 Self::is_negative(self)
2108 }
2109
2110 }
2112 };
2113}
2114
2115impl_signed!(i8, u8);
2116impl_signed!(i16, u16);
2117impl_signed!(i32, u32);
2118impl_signed!(i64, u64);
2119impl_signed!(i128, u128);
2120impl_signed!(isize, usize);
2121
2122#[cfg(test)]
2123mod test {
2124 use super::*;
2125
2126 #[test]
2127 fn test_subnormal_consts() {
2128 assert_eq!(f32::MIN_POSITIVE_SUBNORMAL, f32::from_bits(1));
2129 assert_eq!(f32::MAX_NEGATIVE_SUBNORMAL, -f32::from_bits(1));
2130 assert_eq!(f64::MIN_POSITIVE_SUBNORMAL, f64::from_bits(1));
2131 assert_eq!(f64::MAX_NEGATIVE_SUBNORMAL, -f64::from_bits(1));
2132 }
2133
2134 #[cfg(feature = "std")]
2135 #[test]
2136 fn test_float_floor() {
2137 assert_eq!(<f64 as Float>::floor(1.5), 1.0);
2138 }
2139
2140 #[test]
2141 fn test_euclid_core() {
2142 fn test_int<T: Integer>(a: T, b: T) -> (T, T) {
2143 (a.div_euclid(b), a.rem_euclid(b))
2144 }
2145
2146 assert_eq!(test_int(-7, 4), (-2, 1));
2147 }
2148
2149 #[cfg(feature = "std")]
2150 #[test]
2151 fn test_euclid_std() {
2152 fn test_num<T: Number>(a: T, b: T) -> (T, T) {
2153 (a.div_euclid(b), a.rem_euclid(b))
2154 }
2155
2156 assert_eq!(test_num(-7, 4), (-2, 1));
2157 assert_eq!(test_num(-7.0, 4.0), (-2.0, 1.0));
2158 }
2159
2160 #[test]
2161 fn test_abs() {
2162 fn test_abs<T: Number>(a: T) -> T {
2163 a.abs()
2164 }
2165
2166 assert_eq!(test_abs(1i32), 1);
2167 assert_eq!(test_abs(1u32), 1);
2168 assert_eq!(test_abs(1.0), 1.0);
2169
2170 assert_eq!(test_abs(-1i32), 1);
2171 assert_eq!(test_abs(-1.0), 1.0);
2172
2173 assert!(test_abs(f64::NAN).is_nan());
2174 }
2175
2176 #[test]
2177 fn test_signum() {
2178 fn test_signum<T: Number>(a: T) -> T {
2179 a.signum()
2180 }
2181
2182 assert_eq!(test_signum(123i32), 1);
2183 assert_eq!(test_signum(123u32), 1);
2184 assert_eq!(test_signum(123.0), 1.0);
2185
2186 assert_eq!(test_signum(0i32), 0);
2187 assert_eq!(test_signum(0u32), 0);
2188 assert_eq!(test_signum(0.0), 1.0);
2189 assert_eq!(test_signum(-0.0), -1.0);
2190
2191 assert_eq!(test_signum(-123i32), -1);
2192 assert_eq!(test_signum(-123.0), -1.0);
2193
2194 assert!(test_signum(f64::NAN).is_nan());
2195 }
2196
2197 #[test]
2198 fn test_int_conversions() {
2199 fn inner<T: Integer>(v: T) {
2200 let bytes = v.to_bytes();
2201 let v2: T = T::from_bytes(bytes);
2202 assert_eq!(v2, v);
2203
2204 let signed = v.to_signed();
2205 let v2 = T::from_signed(signed);
2206 assert_eq!(v2, v);
2207
2208 let unsigned = v.to_unsigned();
2209 let v2 = T::from_unsigned(unsigned);
2210 assert_eq!(v2, v);
2211 }
2212
2213 inner(123u64);
2214 inner(-123i64);
2215 }
2216
2217 #[test]
2218 fn test_number_ops() {
2219 #[allow(clippy::op_ref)]
2220 fn number_ops<T: Number>() {
2221 let three = T::TWO + T::ONE;
2222 let four = T::TWO + T::TWO;
2223 let eight = four * T::TWO;
2224
2225 assert_eq!(T::ONE + T::ONE, T::TWO);
2226 assert_eq!(T::ONE + &T::ONE, T::TWO);
2227
2228 assert_eq!(T::TWO - T::ONE, T::ONE);
2229 assert_eq!(T::TWO - &T::ONE, T::ONE);
2230
2231 assert_eq!(T::TWO * T::TWO, four);
2232 assert_eq!(T::TWO * &T::TWO, four);
2233
2234 assert_eq!(four / T::TWO, T::TWO);
2235 assert_eq!(four / &T::TWO, T::TWO);
2236
2237 assert_eq!(three % T::TWO, T::ONE);
2238 assert_eq!(three % &T::TWO, T::ONE);
2239
2240 let mut v = T::ZERO;
2241 v += T::TWO;
2242 v += &T::TWO;
2243 assert_eq!(v, four);
2244
2245 v -= T::ONE;
2246 v -= &T::ONE;
2247 assert_eq!(v, T::TWO);
2248
2249 v *= T::TWO;
2250 v *= &T::TWO;
2251 assert_eq!(v, eight);
2252
2253 v /= T::TWO;
2254 v /= &T::TWO;
2255 assert_eq!(v, T::TWO);
2256
2257 v += T::ONE;
2258 v %= three;
2259 assert_eq!(v, T::ZERO);
2260 v += three;
2261 v %= &T::TWO;
2262 assert_eq!(v, T::ONE);
2263 }
2264
2265 number_ops::<u8>();
2266 }
2267
2268 #[test]
2269 fn test_integer_ops() {
2270 #[allow(clippy::op_ref)]
2271 fn integer_ops<T: Integer>() {
2272 let three = T::TWO + T::ONE;
2273 let four = T::TWO + T::TWO;
2274 let seven = four * T::TWO - T::ONE;
2275
2276 assert_eq!(!!T::ONE, T::ONE);
2277
2278 assert_eq!(three & T::TWO, T::TWO);
2279 assert_eq!(three & &T::TWO, T::TWO);
2280
2281 assert_eq!(T::ONE | T::TWO, three);
2282 assert_eq!(T::ONE | &T::TWO, three);
2283
2284 assert_eq!(three ^ T::TWO, T::ONE);
2285 assert_eq!(three ^ &T::TWO, T::ONE);
2286
2287 assert_eq!(T::ONE << T::TWO, four);
2288 assert_eq!(T::ONE << &T::TWO, four);
2289
2290 assert_eq!(seven >> T::ONE, three);
2291 assert_eq!(seven >> &T::ONE, three);
2292
2293 let mut v = T::ZERO;
2294 v |= T::ONE;
2295 assert_eq!(v, T::ONE);
2296 v |= &T::TWO;
2297 assert_eq!(v, three);
2298
2299 v &= T::TWO;
2300 assert_eq!(v, T::TWO);
2301 v &= &T::ONE;
2302 assert_eq!(v, T::ZERO);
2303
2304 v ^= three;
2305 assert_eq!(v, three);
2306 v ^= &T::TWO;
2307 assert_eq!(v, T::ONE);
2308
2309 v <<= T::ONE;
2310 assert_eq!(v, T::TWO);
2311 v <<= &T::ZERO;
2312 assert_eq!(v, T::TWO);
2313
2314 v >>= T::ZERO;
2315 assert_eq!(v, T::TWO);
2316 v >>= &T::TWO;
2317 assert_eq!(v, T::ZERO);
2318 }
2319
2320 integer_ops::<u8>();
2321 }
2322
2323 #[test]
2324 fn casts() {
2325 assert_eq!(0u8.cast_int::<i8>(), 0i8);
2326 assert_eq!(0u8.cast_number::<i8>(), 0i8);
2327 assert_eq!(0u8.cast_float::<f32>(), 0f32);
2328 assert_eq!(0u8.cast_number::<f32>(), 0f32);
2329
2330 assert_eq!(0f32.cast_int::<u8>(), 0u8);
2331 assert_eq!(0f32.cast_number::<u8>(), 0u8);
2332 assert_eq!(0f32.cast_float::<f64>(), 0f64);
2333 assert_eq!(0f32.cast_number::<f64>(), 0f64);
2334 }
2335}