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