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