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