1use core::{
18 fmt::{Binary, Debug, Display, LowerExp, LowerHex, Octal, UpperExp, UpperHex},
19 hash::Hash,
20 iter::{Product, Sum},
21 mem::{size_of, transmute},
22 num::{FpCategory, ParseIntError},
23 ops::{
24 Add, AddAssign, BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Div,
25 DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Shl, ShlAssign, Shr, ShrAssign, Sub,
26 SubAssign,
27 },
28 panic::{RefUnwindSafe, UnwindSafe},
29 str::FromStr,
30};
31
32use crate::{
33 array::Array,
34 primitive::Primitive,
35 ptr::{Pointer, cast_ptr},
36 util::{cast_generic, cast_int},
37};
38
39pub trait NumberLike:
40 Primitive
41 + Copy
42 + Default
43 + FromStr
44 + PartialEq
45 + PartialOrd
46 + Debug
47 + Display
48 + Unpin
49 + UnwindSafe
50 + RefUnwindSafe
51 + Send
52 + Sync
53 + Sized
54 + 'static
55{
56 const MIN: Self;
60 const MAX: Self;
64
65 type Underlying: Number;
66 type ByteArray: Array<Item = u8>;
67
68 #[must_use]
69 fn to_underlying(self) -> Self::Underlying;
70 #[must_use]
71 fn try_from_underlying(underlying: Self::Underlying) -> Option<Self>;
72 #[must_use]
73 fn to_bytes(self) -> Self::ByteArray;
74 #[must_use]
75 fn try_from_bytes(bytes: Self::ByteArray) -> Option<Self>;
76
77 #[must_use]
78 fn to_be_bytes(self) -> Self::ByteArray;
79 #[must_use]
80 fn to_le_bytes(self) -> Self::ByteArray;
81 #[must_use]
82 fn to_ne_bytes(self) -> Self::ByteArray;
83 #[must_use]
84 fn try_from_be_bytes(bytes: Self::ByteArray) -> Option<Self>;
85 #[must_use]
86 fn try_from_le_bytes(bytes: Self::ByteArray) -> Option<Self>;
87 #[must_use]
88 fn try_from_ne_bytes(bytes: Self::ByteArray) -> Option<Self>;
89
90 #[must_use]
91 fn cast_int<T: Integer>(self) -> T;
92}
93
94macro_rules! impl_number_like {
95 (
96 $ty:ty,
97 underlying: $number:ty,
98 min: $min:expr,
99 max: $max:expr,
100 try_from_underlying: $try_from_underlying:expr
101 ) => {
102 impl Primitive for $ty {}
103 impl NumberLike for $ty {
104 const MIN: Self = $min;
105 const MAX: Self = $max;
106
107 type Underlying = $number;
108 type ByteArray = [u8; size_of::<Self>()];
109
110 fn to_underlying(self) -> Self::Underlying {
111 #[allow(clippy::useless_transmute)]
112 #[allow(unnecessary_transmutes)]
113 unsafe {
114 transmute::<Self, Self::Underlying>(self)
115 }
116 }
117
118 fn try_from_underlying(underlying: Self::Underlying) -> Option<Self> {
119 $try_from_underlying(underlying)
120 }
121
122 fn to_bytes(self) -> Self::ByteArray {
123 #[allow(unnecessary_transmutes)]
124 unsafe {
125 transmute::<Self, Self::ByteArray>(self)
126 }
127 }
128
129 fn try_from_bytes(bytes: Self::ByteArray) -> Option<Self> {
130 Self::try_from_underlying(Self::Underlying::from_bytes(bytes))
131 }
132
133 fn to_be_bytes(self) -> Self::ByteArray {
134 self.to_underlying().to_be_bytes()
135 }
136
137 fn to_le_bytes(self) -> Self::ByteArray {
138 self.to_underlying().to_le_bytes()
139 }
140
141 fn to_ne_bytes(self) -> Self::ByteArray {
142 self.to_underlying().to_ne_bytes()
143 }
144
145 fn try_from_be_bytes(bytes: Self::ByteArray) -> Option<Self> {
146 Self::try_from_underlying(Self::Underlying::from_be_bytes(bytes))
147 }
148
149 fn try_from_le_bytes(bytes: Self::ByteArray) -> Option<Self> {
150 Self::try_from_underlying(Self::Underlying::from_le_bytes(bytes))
151 }
152
153 fn try_from_ne_bytes(bytes: Self::ByteArray) -> Option<Self> {
154 Self::try_from_underlying(Self::Underlying::from_ne_bytes(bytes))
155 }
156
157 fn cast_int<T: Integer>(self) -> T {
158 cast_int!(self => T)
159 }
160 }
161 };
162}
163
164impl_number_like!(bool,
165 underlying: u8,
166 min: false,
167 max: true,
168 try_from_underlying: |v| match v {
169 0 => Some(false),
170 1 => Some(true),
171 _ => None,
172 }
173);
174impl_number_like!(char,
175 underlying: u32,
176 min: '\0',
177 max: '\u{10ffff}',
178 try_from_underlying: |v| char::try_from(v).ok()
179);
180
181pub trait Number:
182 NumberLike
183 + LowerExp
184 + UpperExp
185 + Add<Self, Output = Self>
186 + for<'a> Add<&'a Self, Output = Self>
187 + AddAssign<Self>
188 + for<'a> AddAssign<&'a Self>
189 + Sub<Self, Output = Self>
190 + for<'a> Sub<&'a Self, Output = Self>
191 + SubAssign<Self>
192 + for<'a> SubAssign<&'a Self>
193 + Mul<Self, Output = Self>
194 + for<'a> Mul<&'a Self, Output = Self>
195 + MulAssign<Self>
196 + for<'a> MulAssign<&'a Self>
197 + Div<Self, Output = Self>
198 + for<'a> Div<&'a Self, Output = Self>
199 + DivAssign<Self>
200 + for<'a> DivAssign<&'a Self>
201 + Rem<Self, Output = Self>
202 + for<'a> Rem<&'a Self, Output = Self>
203 + RemAssign<Self>
204 + for<'a> RemAssign<&'a Self>
205 + From<bool>
206 + TryFrom<u8>
207 + TryFrom<u16>
208 + TryFrom<i8>
209 + TryFrom<i16>
210 + Sum
211 + Product
212{
213 const ZERO: Self;
214 const ONE: Self;
215 const TWO: Self;
216
217 #[must_use]
218 fn from_bytes(bytes: Self::ByteArray) -> Self;
219 #[must_use]
220 fn as_mut_bytes(&mut self) -> &mut Self::ByteArray;
221
222 #[must_use]
227 fn from_be_bytes(bytes: Self::ByteArray) -> Self;
228
229 #[must_use]
231 fn from_le_bytes(bytes: Self::ByteArray) -> Self;
232
233 #[must_use]
235 fn from_ne_bytes(bytes: Self::ByteArray) -> Self;
236
237 #[must_use = "this returns the result of the operation, without modifying the original"]
239 fn midpoint(self, rhs: Self) -> Self;
240
241 #[must_use]
244 fn cast_float<T: Float>(self) -> T;
245 #[must_use]
246 fn cast_number<T: Number>(self) -> T;
247
248 #[must_use = "this returns the result of the operation, without modifying the original"]
250 fn abs(self) -> Self;
251
252 #[must_use = "this returns the result of the operation, without modifying the original"]
254 fn signum(self) -> Self;
255
256 #[cfg(feature = "std")]
257 #[must_use = "this returns the result of the operation, without modifying the original"]
258 fn div_euclid(self, rhs: Self) -> Self;
259
260 #[cfg(feature = "std")]
261 #[must_use = "this returns the result of the operation, without modifying the original"]
262 fn rem_euclid(self, rhs: Self) -> Self;
263}
264
265macro_rules! impl_number {
266 (
267 $ty:ty,
268 zero: $zero:expr,
269 one: $one:expr,
270 min: $min:expr,
271 max: $max:expr,
272 abs: $abs:expr,
273 signum: $signum:expr
274 ) => {
275 impl_number_like!($ty,
276 underlying: Self,
277 min: $min,
278 max: $max,
279 try_from_underlying: |v| Some(v)
280 );
281 impl Number for $ty {
282 const ZERO: Self = $zero;
283 const ONE: Self = $one;
284 const TWO: Self = $one + $one;
285
286 fn from_bytes(bytes: Self::ByteArray) -> Self {
287 #[allow(unnecessary_transmutes)]
288 unsafe { transmute::<Self::ByteArray, Self>(bytes) }
289 }
290
291 fn as_mut_bytes(&mut self) -> &mut Self::ByteArray {
292 unsafe { transmute::<&mut Self, &mut Self::ByteArray>(self) }
293 }
294
295 fn from_be_bytes(bytes: Self::ByteArray) -> Self {
299 Self::from_be_bytes(bytes)
300 }
301
302 fn from_le_bytes(bytes: Self::ByteArray) -> Self {
303 Self::from_le_bytes(bytes)
304 }
305
306 fn from_ne_bytes(bytes: Self::ByteArray) -> Self {
307 Self::from_ne_bytes(bytes)
308 }
309
310 fn midpoint(self, rhs: Self) -> Self {
311 Self::midpoint(self, rhs)
312 }
313
314 fn cast_float<T: Float>(self) -> T {
317 cast_generic!(self => T; f32 f64)
318 }
319
320 fn cast_number<T: Number>(self) -> T {
321 cast_generic!(self => T;
322 i8 i16 i32 i64 i128 isize
323 u8 u16 u32 u64 u128 usize
324 f32 f64
325 )
326 }
327
328 fn abs(self) -> Self {
329 $abs(self)
330 }
331
332 fn signum(self) -> Self {
333 $signum(self)
334 }
335
336 #[cfg(feature = "std")]
337 fn div_euclid(self, rhs: Self) -> Self {
338 Self::div_euclid(self, rhs)
339 }
340
341 #[cfg(feature = "std")]
342 fn rem_euclid(self, rhs: Self) -> Self {
343 Self::rem_euclid(self, rhs)
344 }
345 }
346 };
347}
348
349#[rustfmt::skip] pub trait Float:
351 Number
352 + Neg<Output = Self>
353 + From<f32>
354 + Into<f64>
355 + From<i8>
356 + From<i16>
357 + From<u8>
358 + From<u16>
359{
360 const RADIX: u32;
361 const MANTISSA_DIGITS: u32;
362 const DIGITS: u32;
363 const EPSILON: Self;
364
365 const MIN_FINITE: Self;
366 const MIN_POSITIVE_SUBNORMAL: Self;
367 const MIN_POSITIVE_NORMAL: Self;
368 const MIN_EXP: i32;
369 const MIN_10_EXP: i32;
370
371 const MAX_FINITE: Self;
372 const MAX_NEGATIVE_SUBNORMAL: Self;
373 const MAX_NEGATIVE_NORMAL: Self;
374 const MAX_EXP: i32;
375 const MAX_10_EXP: i32;
376
377 const NAN: Self;
378 const INFINITY: Self;
379 const NEG_INFINITY: Self;
380
381 const NEG_ZERO: Self;
382
383 type Bits: Unsigned;
384
385 #[must_use]
390 fn is_nan(self) -> bool;
391
392 #[must_use]
394 fn is_infinite(self) -> bool;
395
396 #[must_use]
398 fn is_finite(self) -> bool;
399
400 #[must_use]
402 fn is_subnormal(self) -> bool;
403
404 #[must_use]
406 fn is_normal(self) -> bool;
407
408 #[must_use]
410 fn classify(self) -> FpCategory;
411
412 #[must_use]
414 fn is_sign_positive(self) -> bool;
415
416 #[must_use]
418 fn is_sign_negative(self) -> bool;
419
420 #[must_use = "method returns a new number and does not mutate the original value"]
422 fn next_up(self) -> Self;
423
424 #[must_use = "method returns a new number and does not mutate the original value"]
426 fn next_down(self) -> Self;
427
428 #[must_use = "this returns the result of the operation, without modifying the original"]
430 fn recip(self) -> Self;
431
432 #[must_use = "this returns the result of the operation, without modifying the original"]
434 fn to_degrees(self) -> Self;
435
436 #[must_use = "this returns the result of the operation, without modifying the original"]
438 fn to_radians(self) -> Self;
439
440 #[must_use = "this returns the result of the comparison, without modifying either input"]
442 fn max(self, other: Self) -> Self;
443
444 #[must_use = "this returns the result of the comparison, without modifying either input"]
446 fn min(self, other: Self) -> Self;
447
448 #[must_use = "this returns the result of the operation, without modifying the original"]
450 fn midpoint(self, other: Self) -> Self;
451
452 #[must_use = "this returns the result of the operation, without modifying the original"]
454 fn to_bits(self) -> Self::Bits;
455
456 #[must_use]
458 fn from_bits(v: Self::Bits) -> Self;
459
460 #[must_use]
462 fn total_cmp(&self, other: &Self) -> core::cmp::Ordering;
463
464 #[must_use = "method returns a new number and does not mutate the original value"]
466 fn clamp(self, min: Self, max: Self) -> Self;
467
468 #[must_use = "method returns a new number and does not mutate the original value"]
470 fn copysign(self, sign: Self) -> Self;
471
472 #[must_use = "method returns a new number and does not mutate the original value"]
474 fn algebraic_add(self, rhs: Self) -> Self;
475
476 #[must_use = "method returns a new number and does not mutate the original value"]
478 fn algebraic_sub(self, rhs: Self) -> Self;
479
480 #[must_use = "method returns a new number and does not mutate the original value"]
482 fn algebraic_mul(self, rhs: Self) -> Self;
483
484 #[must_use = "method returns a new number and does not mutate the original value"]
486 fn algebraic_div(self, rhs: Self) -> Self;
487
488 #[must_use = "method returns a new number and does not mutate the original value"]
490 fn algebraic_rem(self, rhs: Self) -> Self;
491
492 #[cfg(feature = "std")]
494 #[must_use = "method returns a new number and does not mutate the original value"]
495 fn floor(self) -> Self;
496
497 #[cfg(feature = "std")]
499 #[must_use = "method returns a new number and does not mutate the original value"]
500 fn ceil(self) -> Self;
501
502 #[cfg(feature = "std")]
504 #[must_use = "method returns a new number and does not mutate the original value"]
505 fn round(self) -> Self;
506
507 #[cfg(feature = "std")]
509 #[must_use = "method returns a new number and does not mutate the original value"]
510 fn round_ties_even(self) -> Self;
511
512 #[cfg(feature = "std")]
514 #[must_use = "method returns a new number and does not mutate the original value"]
515 fn trunc(self) -> Self;
516
517 #[cfg(feature = "std")]
519 #[must_use = "method returns a new number and does not mutate the original value"]
520 fn fract(self) -> Self;
521
522 #[cfg(feature = "std")]
524 #[must_use = "method returns a new number and does not mutate the original value"]
525 fn mul_add(self, a: Self, b: Self) -> Self;
526
527 #[cfg(feature = "std")]
529 #[must_use = "method returns a new number and does not mutate the original value"]
530 fn powi(self, n: i32) -> Self;
531
532 #[cfg(feature = "std")]
534 #[must_use = "method returns a new number and does not mutate the original value"]
535 fn powf(self, n: Self) -> Self;
536
537 #[cfg(feature = "std")]
539 #[must_use = "method returns a new number and does not mutate the original value"]
540 fn sqrt(self) -> Self;
541
542 #[cfg(feature = "std")]
544 #[must_use = "method returns a new number and does not mutate the original value"]
545 fn exp(self) -> Self;
546
547 #[cfg(feature = "std")]
549 #[must_use = "method returns a new number and does not mutate the original value"]
550 fn exp2(self) -> Self;
551
552 #[cfg(feature = "std")]
554 #[must_use = "method returns a new number and does not mutate the original value"]
555 fn ln(self) -> Self;
556
557 #[cfg(feature = "std")]
559 #[must_use = "method returns a new number and does not mutate the original value"]
560 fn log(self, base: Self) -> Self;
561
562 #[cfg(feature = "std")]
564 #[must_use = "method returns a new number and does not mutate the original value"]
565 fn log2(self) -> Self;
566
567 #[cfg(feature = "std")]
569 #[must_use = "method returns a new number and does not mutate the original value"]
570 fn log10(self) -> Self;
571
572 #[cfg(feature = "std")]
574 #[must_use = "method returns a new number and does not mutate the original value"]
575 fn cbrt(self) -> Self;
576
577 #[cfg(feature = "std")]
579 #[must_use = "method returns a new number and does not mutate the original value"]
580 fn hypot(self, other: Self) -> Self;
581
582 #[cfg(feature = "std")]
584 #[must_use = "method returns a new number and does not mutate the original value"]
585 fn sin(self) -> Self;
586
587 #[cfg(feature = "std")]
589 #[must_use = "method returns a new number and does not mutate the original value"]
590 fn cos(self) -> Self;
591
592 #[cfg(feature = "std")]
594 #[must_use = "method returns a new number and does not mutate the original value"]
595 fn tan(self) -> Self;
596
597 #[cfg(feature = "std")]
599 #[must_use = "method returns a new number and does not mutate the original value"]
600 fn asin(self) -> Self;
601
602 #[cfg(feature = "std")]
604 #[must_use = "method returns a new number and does not mutate the original value"]
605 fn acos(self) -> Self;
606
607 #[cfg(feature = "std")]
609 #[must_use = "method returns a new number and does not mutate the original value"]
610 fn atan(self) -> Self;
611
612 #[cfg(feature = "std")]
614 #[must_use = "method returns a new number and does not mutate the original value"]
615 fn atan2(self, other: Self) -> Self;
616
617 #[cfg(feature = "std")]
619 #[must_use = "this returns the result of the operation, without modifying the original"]
620 fn sin_cos(self) -> (Self, Self);
621
622 #[cfg(feature = "std")]
624 #[must_use = "method returns a new number and does not mutate the original value"]
625 fn exp_m1(self) -> Self;
626
627 #[cfg(feature = "std")]
629 #[must_use = "method returns a new number and does not mutate the original value"]
630 fn ln_1p(self) -> Self;
631
632 #[cfg(feature = "std")]
634 #[must_use = "method returns a new number and does not mutate the original value"]
635 fn sinh(self) -> Self;
636
637 #[cfg(feature = "std")]
639 #[must_use = "method returns a new number and does not mutate the original value"]
640 fn cosh(self) -> Self;
641
642 #[cfg(feature = "std")]
644 #[must_use = "method returns a new number and does not mutate the original value"]
645 fn tanh(self) -> Self;
646
647 #[cfg(feature = "std")]
649 #[must_use = "method returns a new number and does not mutate the original value"]
650 fn asinh(self) -> Self;
651
652 #[cfg(feature = "std")]
654 #[must_use = "method returns a new number and does not mutate the original value"]
655 fn acosh(self) -> Self;
656
657 #[cfg(feature = "std")]
659 #[must_use = "method returns a new number and does not mutate the original value"]
660 fn atanh(self) -> Self;
661
662 }
664
665macro_rules! impl_float {
666 ($ty:ty, $bits:ty, $min_positive_subnormal:expr) => {
667 impl_number!(
668 $ty,
669 zero: 0.0,
670 one: 1.0,
671 min: Self::NEG_INFINITY,
672 max: Self::INFINITY,
673 abs: Self::abs,
674 signum: Self::signum
675 );
676 impl Float for $ty {
677 const RADIX: u32 = Self::RADIX;
678 const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS;
679 const DIGITS: u32 = Self::DIGITS;
680 const EPSILON: Self = Self::EPSILON;
681
682 const MIN_FINITE: Self = Self::MIN;
683 const MIN_POSITIVE_SUBNORMAL: Self = $min_positive_subnormal;
684 const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
685 const MIN_EXP: i32 = Self::MIN_EXP;
686 const MIN_10_EXP: i32 = Self::MIN_10_EXP;
687
688 const MAX_FINITE: Self = Self::MAX;
689 const MAX_NEGATIVE_SUBNORMAL: Self = -Self::MIN_POSITIVE_SUBNORMAL;
690 const MAX_NEGATIVE_NORMAL: Self = -Self::MIN_POSITIVE_NORMAL;
691 const MAX_EXP: i32 = Self::MAX_EXP;
692 const MAX_10_EXP: i32 = Self::MAX_10_EXP;
693
694 const NAN: Self = Self::NAN;
695 const INFINITY: Self = Self::INFINITY;
696 const NEG_INFINITY: Self = Self::NEG_INFINITY;
697
698 const NEG_ZERO: Self = -0.0;
699
700 type Bits = $bits;
701
702 fn is_nan(self) -> bool {
706 Self::is_nan(self)
707 }
708
709 fn is_infinite(self) -> bool {
710 Self::is_infinite(self)
711 }
712
713 fn is_finite(self) -> bool {
714 Self::is_finite(self)
715 }
716
717 fn is_subnormal(self) -> bool {
718 Self::is_subnormal(self)
719 }
720
721 fn is_normal(self) -> bool {
722 Self::is_normal(self)
723 }
724
725 fn classify(self) -> FpCategory {
726 Self::classify(self)
727 }
728
729 fn is_sign_positive(self) -> bool {
730 Self::is_sign_positive(self)
731 }
732
733 fn is_sign_negative(self) -> bool {
734 Self::is_sign_negative(self)
735 }
736
737 fn next_up(self) -> Self {
738 Self::next_up(self)
739 }
740
741 fn next_down(self) -> Self {
742 Self::next_down(self)
743 }
744
745 fn recip(self) -> Self {
746 Self::recip(self)
747 }
748
749 fn to_degrees(self) -> Self {
750 Self::to_degrees(self)
751 }
752
753 fn to_radians(self) -> Self {
754 Self::to_radians(self)
755 }
756
757 fn max(self, other: Self) -> Self {
758 Self::max(self, other)
759 }
760
761 fn min(self, other: Self) -> Self {
762 Self::min(self, other)
763 }
764
765 fn midpoint(self, other: Self) -> Self {
766 Self::midpoint(self, other)
767 }
768
769 fn to_bits(self) -> Self::Bits {
770 Self::to_bits(self)
771 }
772
773 fn from_bits(v: Self::Bits) -> Self {
774 Self::from_bits(v)
775 }
776
777 fn total_cmp(&self, other: &Self) -> core::cmp::Ordering {
778 Self::total_cmp(self, other)
779 }
780
781 fn clamp(self, min: Self, max: Self) -> Self {
782 Self::clamp(self, min, max)
783 }
784
785 fn copysign(self, sign: Self) -> Self {
786 Self::copysign(self, sign)
787 }
788
789 fn algebraic_add(self, rhs: Self) -> Self {
790 Self::algebraic_add(self, rhs)
791 }
792
793 fn algebraic_sub(self, rhs: Self) -> Self {
794 Self::algebraic_sub(self, rhs)
795 }
796
797 fn algebraic_mul(self, rhs: Self) -> Self {
798 Self::algebraic_mul(self, rhs)
799 }
800
801 fn algebraic_div(self, rhs: Self) -> Self {
802 Self::algebraic_div(self, rhs)
803 }
804
805 fn algebraic_rem(self, rhs: Self) -> Self {
806 Self::algebraic_rem(self, rhs)
807 }
808
809 #[cfg(feature = "std")]
810 fn floor(self) -> Self {
811 Self::floor(self)
812 }
813
814 #[cfg(feature = "std")]
815 fn ceil(self) -> Self {
816 Self::ceil(self)
817 }
818
819 #[cfg(feature = "std")]
820 fn round(self) -> Self {
821 Self::round(self)
822 }
823
824 #[cfg(feature = "std")]
825 fn round_ties_even(self) -> Self {
826 Self::round_ties_even(self)
827 }
828
829 #[cfg(feature = "std")]
830 fn trunc(self) -> Self {
831 Self::trunc(self)
832 }
833
834 #[cfg(feature = "std")]
835 fn fract(self) -> Self {
836 Self::fract(self)
837 }
838
839 #[cfg(feature = "std")]
840 fn mul_add(self, a: Self, b: Self) -> Self {
841 Self::mul_add(self, a, b)
842 }
843
844 #[cfg(feature = "std")]
845 fn powi(self, n: i32) -> Self {
846 Self::powi(self, n)
847 }
848
849 #[cfg(feature = "std")]
850 fn powf(self, n: Self) -> Self {
851 Self::powf(self, n)
852 }
853
854 #[cfg(feature = "std")]
855 fn sqrt(self) -> Self {
856 Self::sqrt(self)
857 }
858
859 #[cfg(feature = "std")]
860 fn exp(self) -> Self {
861 Self::exp(self)
862 }
863
864 #[cfg(feature = "std")]
865 fn exp2(self) -> Self {
866 Self::exp2(self)
867 }
868
869 #[cfg(feature = "std")]
870 fn ln(self) -> Self {
871 Self::ln(self)
872 }
873
874 #[cfg(feature = "std")]
875 fn log(self, base: Self) -> Self {
876 Self::log(self, base)
877 }
878
879 #[cfg(feature = "std")]
880 fn log2(self) -> Self {
881 Self::log2(self)
882 }
883
884 #[cfg(feature = "std")]
885 fn log10(self) -> Self {
886 Self::log10(self)
887 }
888
889 #[cfg(feature = "std")]
890 fn cbrt(self) -> Self {
891 Self::cbrt(self)
892 }
893
894 #[cfg(feature = "std")]
895 fn hypot(self, other: Self) -> Self {
896 Self::hypot(self, other)
897 }
898
899 #[cfg(feature = "std")]
900 fn sin(self) -> Self {
901 Self::sin(self)
902 }
903
904 #[cfg(feature = "std")]
905 fn cos(self) -> Self {
906 Self::cos(self)
907 }
908
909 #[cfg(feature = "std")]
910 fn tan(self) -> Self {
911 Self::tan(self)
912 }
913
914 #[cfg(feature = "std")]
915 fn asin(self) -> Self {
916 Self::asin(self)
917 }
918
919 #[cfg(feature = "std")]
920 fn acos(self) -> Self {
921 Self::acos(self)
922 }
923
924 #[cfg(feature = "std")]
925 fn atan(self) -> Self {
926 Self::atan(self)
927 }
928
929 #[cfg(feature = "std")]
930 fn atan2(self, other: Self) -> Self {
931 Self::atan2(self, other)
932 }
933
934 #[cfg(feature = "std")]
935 fn sin_cos(self) -> (Self, Self) {
936 Self::sin_cos(self)
937 }
938
939 #[cfg(feature = "std")]
940 fn exp_m1(self) -> Self {
941 Self::exp_m1(self)
942 }
943
944 #[cfg(feature = "std")]
945 fn ln_1p(self) -> Self {
946 Self::ln_1p(self)
947 }
948
949 #[cfg(feature = "std")]
950 fn sinh(self) -> Self {
951 Self::sinh(self)
952 }
953
954 #[cfg(feature = "std")]
955 fn cosh(self) -> Self {
956 Self::cosh(self)
957 }
958
959 #[cfg(feature = "std")]
960 fn tanh(self) -> Self {
961 Self::tanh(self)
962 }
963
964 #[cfg(feature = "std")]
965 fn asinh(self) -> Self {
966 Self::asinh(self)
967 }
968
969 #[cfg(feature = "std")]
970 fn acosh(self) -> Self {
971 Self::acosh(self)
972 }
973
974 #[cfg(feature = "std")]
975 fn atanh(self) -> Self {
976 Self::atanh(self)
977 }
978
979 }
981 };
982}
983
984impl_float!(f32, u32, 1e-45);
985impl_float!(f64, u64, 5e-324);
986
987pub trait Integer:
988 Number
989 + Ord
990 + Eq
991 + Not<Output = Self>
992 + BitAnd<Self, Output = Self>
993 + for<'a> BitAnd<&'a Self, Output = Self>
994 + BitAndAssign<Self>
995 + for<'a> BitAndAssign<&'a Self>
996 + BitOr<Self, Output = Self>
997 + for<'a> BitOr<&'a Self, Output = Self>
998 + BitOrAssign<Self>
999 + for<'a> BitOrAssign<&'a Self>
1000 + BitXor<Self, Output = Self>
1001 + for<'a> BitXor<&'a Self, Output = Self>
1002 + BitXorAssign<Self>
1003 + for<'a> BitXorAssign<&'a Self>
1004 + Shl<Self, Output = Self>
1005 + for<'a> Shl<&'a Self, Output = Self>
1006 + ShlAssign<Self>
1007 + for<'a> ShlAssign<&'a Self>
1008 + Shr<Self, Output = Self>
1009 + for<'a> Shr<&'a Self, Output = Self>
1010 + ShrAssign<Self>
1011 + for<'a> ShrAssign<&'a Self>
1012 + TryFrom<u32>
1013 + TryFrom<u64>
1014 + TryFrom<u128>
1015 + TryFrom<usize>
1016 + TryFrom<i32>
1017 + TryFrom<i64>
1018 + TryFrom<i128>
1019 + TryFrom<isize>
1020 + TryInto<u8>
1021 + TryInto<u16>
1022 + TryInto<u32>
1023 + TryInto<u64>
1024 + TryInto<u128>
1025 + TryInto<usize>
1026 + TryInto<i8>
1027 + TryInto<i16>
1028 + TryInto<i32>
1029 + TryInto<i64>
1030 + TryInto<i128>
1031 + TryInto<isize>
1032 + Hash
1033 + Binary
1034 + Octal
1035 + LowerHex
1036 + UpperHex
1037{
1038 type Unsigned: Unsigned;
1039 type Signed: Signed;
1040
1041 #[must_use]
1042 fn from_unsigned(v: Self::Unsigned) -> Self;
1043 #[must_use]
1044 fn from_signed(v: Self::Signed) -> Self;
1045 #[must_use]
1046 fn to_unsigned(self) -> Self::Unsigned;
1047 #[must_use]
1048 fn to_signed(self) -> Self::Signed;
1049
1050 #[must_use]
1051 fn cast_ptr<T: Sized, P: Pointer<T>>(self) -> P;
1052
1053 #[cfg(not(feature = "std"))]
1055 #[must_use = "this returns the result of the operation, without modifying the original"]
1056 fn div_euclid(self, rhs: Self) -> Self;
1057
1058 #[cfg(not(feature = "std"))]
1060 #[must_use = "this returns the result of the operation, without modifying the original"]
1061 fn rem_euclid(self, rhs: Self) -> Self;
1062
1063 fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>;
1068
1069 #[must_use = "this returns the result of the operation, without modifying the original"]
1071 fn count_ones(self) -> u32;
1072
1073 #[must_use = "this returns the result of the operation, without modifying the original"]
1075 fn count_zeros(self) -> u32;
1076
1077 #[must_use = "this returns the result of the operation, without modifying the original"]
1079 fn leading_zeros(self) -> u32;
1080
1081 #[must_use = "this returns the result of the operation, without modifying the original"]
1083 fn trailing_zeros(self) -> u32;
1084
1085 #[must_use = "this returns the result of the operation, without modifying the original"]
1087 fn leading_ones(self) -> u32;
1088
1089 #[must_use = "this returns the result of the operation, without modifying the original"]
1091 fn trailing_ones(self) -> u32;
1092
1093 #[must_use = "this returns the result of the operation, without modifying the original"]
1095 fn isolate_highest_one(self) -> Self;
1096
1097 #[must_use = "this returns the result of the operation, without modifying the original"]
1099 fn isolate_lowest_one(self) -> Self;
1100
1101 #[must_use = "this returns the result of the operation, without modifying the original"]
1103 fn highest_one(self) -> Option<u32>;
1104
1105 #[must_use = "this returns the result of the operation, without modifying the original"]
1107 fn lowest_one(self) -> Option<u32>;
1108
1109 #[must_use = "this returns the result of the operation, without modifying the original"]
1111 fn rotate_left(self, n: u32) -> Self;
1112
1113 #[must_use = "this returns the result of the operation, without modifying the original"]
1115 fn rotate_right(self, n: u32) -> Self;
1116
1117 #[must_use = "this returns the result of the operation, without modifying the original"]
1119 fn swap_bytes(self) -> Self;
1120
1121 #[must_use = "this returns the result of the operation, without modifying the original"]
1123 fn reverse_bits(self) -> Self;
1124
1125 #[must_use]
1127 fn from_be(x: Self) -> Self;
1128
1129 #[must_use]
1131 fn from_le(x: Self) -> Self;
1132
1133 #[must_use = "this returns the result of the operation, without modifying the original"]
1135 fn to_be(self) -> Self;
1136
1137 #[must_use = "this returns the result of the operation, without modifying the original"]
1139 fn to_le(self) -> Self;
1140
1141 #[must_use = "this returns the result of the operation, without modifying the original"]
1143 fn checked_add(self, rhs: Self) -> Option<Self>;
1144
1145 #[must_use = "this returns the result of the operation, without modifying the original"]
1147 fn strict_add(self, rhs: Self) -> Self;
1148
1149 #[must_use = "this returns the result of the operation, without modifying the original"]
1155 unsafe fn unchecked_add(self, rhs: Self) -> Self;
1156
1157 #[must_use = "this returns the result of the operation, without modifying the original"]
1159 fn checked_sub(self, rhs: Self) -> Option<Self>;
1160
1161 #[must_use = "this returns the result of the operation, without modifying the original"]
1163 fn strict_sub(self, rhs: Self) -> Self;
1164
1165 #[must_use = "this returns the result of the operation, without modifying the original"]
1171 unsafe fn unchecked_sub(self, rhs: Self) -> Self;
1172
1173 #[must_use = "this returns the result of the operation, without modifying the original"]
1175 fn checked_mul(self, rhs: Self) -> Option<Self>;
1176
1177 #[must_use = "this returns the result of the operation, without modifying the original"]
1179 fn strict_mul(self, rhs: Self) -> Self;
1180
1181 #[must_use = "this returns the result of the operation, without modifying the original"]
1187 unsafe fn unchecked_mul(self, rhs: Self) -> Self;
1188
1189 #[must_use = "this returns the result of the operation, without modifying the original"]
1191 fn checked_div(self, rhs: Self) -> Option<Self>;
1192
1193 #[must_use = "this returns the result of the operation, without modifying the original"]
1195 fn strict_div(self, rhs: Self) -> Self;
1196
1197 #[must_use = "this returns the result of the operation, without modifying the original"]
1199 fn checked_div_euclid(self, rhs: Self) -> Option<Self>;
1200
1201 #[must_use = "this returns the result of the operation, without modifying the original"]
1203 fn strict_div_euclid(self, rhs: Self) -> Self;
1204
1205 #[must_use = "this returns the result of the operation, without modifying the original"]
1207 fn checked_rem(self, rhs: Self) -> Option<Self>;
1208
1209 #[must_use = "this returns the result of the operation, without modifying the original"]
1211 fn strict_rem(self, rhs: Self) -> Self;
1212
1213 #[must_use = "this returns the result of the operation, without modifying the original"]
1215 fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;
1216
1217 #[must_use = "this returns the result of the operation, without modifying the original"]
1219 fn strict_rem_euclid(self, rhs: Self) -> Self;
1220
1221 #[must_use = "this returns the result of the operation, without modifying the original"]
1223 fn checked_neg(self) -> Option<Self>;
1224
1225 #[must_use = "this returns the result of the operation, without modifying the original"]
1227 fn strict_neg(self) -> Self;
1228
1229 #[must_use = "this returns the result of the operation, without modifying the original"]
1231 fn checked_shl(self, rhs: u32) -> Option<Self>;
1232
1233 #[must_use = "this returns the result of the operation, without modifying the original"]
1235 fn strict_shl(self, rhs: u32) -> Self;
1236
1237 #[must_use = "this returns the result of the operation, without modifying the original"]
1243 unsafe fn unchecked_shl(self, rhs: u32) -> Self;
1244
1245 #[must_use = "this returns the result of the operation, without modifying the original"]
1247 fn unbounded_shl(self, rhs: u32) -> Self;
1248
1249 #[must_use = "this returns the result of the operation, without modifying the original"]
1251 fn checked_shr(self, rhs: u32) -> Option<Self>;
1252
1253 #[must_use = "this returns the result of the operation, without modifying the original"]
1255 fn strict_shr(self, rhs: u32) -> Self;
1256
1257 #[must_use = "this returns the result of the operation, without modifying the original"]
1263 unsafe fn unchecked_shr(self, rhs: u32) -> Self;
1264
1265 #[must_use = "this returns the result of the operation, without modifying the original"]
1267 fn unbounded_shr(self, rhs: u32) -> Self;
1268
1269 #[must_use = "this returns the result of the operation, without modifying the original"]
1271 fn checked_pow(self, exp: u32) -> Option<Self>;
1272
1273 #[must_use = "this returns the result of the operation, without modifying the original"]
1275 fn strict_pow(self, exp: u32) -> Self;
1276
1277 #[must_use = "this returns the result of the operation, without modifying the original"]
1279 fn saturating_add(self, rhs: Self) -> Self;
1280
1281 #[must_use = "this returns the result of the operation, without modifying the original"]
1283 fn saturating_sub(self, rhs: Self) -> Self;
1284
1285 #[must_use = "this returns the result of the operation, without modifying the original"]
1287 fn saturating_mul(self, rhs: Self) -> Self;
1288
1289 #[must_use = "this returns the result of the operation, without modifying the original"]
1291 fn saturating_div(self, rhs: Self) -> Self;
1292
1293 #[must_use = "this returns the result of the operation, without modifying the original"]
1295 fn saturating_pow(self, exp: u32) -> Self;
1296
1297 #[must_use = "this returns the result of the operation, without modifying the original"]
1299 fn wrapping_add(self, rhs: Self) -> Self;
1300
1301 #[must_use = "this returns the result of the operation, without modifying the original"]
1303 fn wrapping_sub(self, rhs: Self) -> Self;
1304
1305 #[must_use = "this returns the result of the operation, without modifying the original"]
1307 fn wrapping_mul(self, rhs: Self) -> Self;
1308
1309 #[must_use = "this returns the result of the operation, without modifying the original"]
1311 fn wrapping_div(self, rhs: Self) -> Self;
1312
1313 #[must_use = "this returns the result of the operation, without modifying the original"]
1315 fn wrapping_div_euclid(self, rhs: Self) -> Self;
1316
1317 #[must_use = "this returns the result of the operation, without modifying the original"]
1319 fn wrapping_rem(self, rhs: Self) -> Self;
1320
1321 #[must_use = "this returns the result of the operation, without modifying the original"]
1323 fn wrapping_rem_euclid(self, rhs: Self) -> Self;
1324
1325 #[must_use = "this returns the result of the operation, without modifying the original"]
1327 fn wrapping_neg(self) -> Self;
1328
1329 #[must_use = "this returns the result of the operation, without modifying the original"]
1331 fn wrapping_shl(self, rhs: u32) -> Self;
1332
1333 #[must_use = "this returns the result of the operation, without modifying the original"]
1335 fn wrapping_shr(self, rhs: u32) -> Self;
1336
1337 #[must_use = "this returns the result of the operation, without modifying the original"]
1339 fn wrapping_pow(self, exp: u32) -> Self;
1340
1341 #[must_use = "this returns the result of the operation, without modifying the original"]
1343 fn overflowing_add(self, rhs: Self) -> (Self, bool);
1344
1345 #[must_use = "this returns the result of the operation, without modifying the original"]
1347 fn overflowing_sub(self, rhs: Self) -> (Self, bool);
1348
1349 #[must_use = "this returns the result of the operation, without modifying the original"]
1351 fn overflowing_mul(self, rhs: Self) -> (Self, bool);
1352
1353 #[must_use = "this returns the result of the operation, without modifying the original"]
1355 fn overflowing_div(self, rhs: Self) -> (Self, bool);
1356
1357 #[must_use = "this returns the result of the operation, without modifying the original"]
1359 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);
1360
1361 #[must_use = "this returns the result of the operation, without modifying the original"]
1363 fn overflowing_rem(self, rhs: Self) -> (Self, bool);
1364
1365 #[must_use = "this returns the result of the operation, without modifying the original"]
1367 fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool);
1368
1369 #[must_use = "this returns the result of the operation, without modifying the original"]
1371 fn overflowing_neg(self) -> (Self, bool);
1372
1373 #[must_use = "this returns the result of the operation, without modifying the original"]
1375 fn overflowing_shl(self, rhs: u32) -> (Self, bool);
1376
1377 #[must_use = "this returns the result of the operation, without modifying the original"]
1379 fn overflowing_shr(self, rhs: u32) -> (Self, bool);
1380
1381 #[must_use = "this returns the result of the operation, without modifying the original"]
1383 fn overflowing_pow(self, exp: u32) -> (Self, bool);
1384
1385 #[must_use = "this returns the result of the operation, without modifying the original"]
1387 fn pow(self, exp: u32) -> Self;
1388
1389 #[must_use = "this returns the result of the operation, without modifying the original"]
1391 fn isqrt(self) -> Self;
1392
1393 #[must_use = "this returns the result of the operation, without modifying the original"]
1395 fn ilog(self, base: Self) -> u32;
1396
1397 #[must_use = "this returns the result of the operation, without modifying the original"]
1399 fn ilog2(self) -> u32;
1400
1401 #[must_use = "this returns the result of the operation, without modifying the original"]
1403 fn ilog10(self) -> u32;
1404
1405 #[must_use = "this returns the result of the operation, without modifying the original"]
1407 fn checked_ilog(self, base: Self) -> Option<u32>;
1408
1409 #[must_use = "this returns the result of the operation, without modifying the original"]
1411 fn checked_ilog2(self) -> Option<u32>;
1412
1413 #[must_use = "this returns the result of the operation, without modifying the original"]
1415 fn checked_ilog10(self) -> Option<u32>;
1416
1417 #[must_use = "this returns the result of the operation, without modifying the original"]
1419 fn abs_diff(self, other: Self) -> Self::Unsigned;
1420
1421 }
1423
1424macro_rules! impl_integer {
1425 ($ty:ty, $unsigned:ty, $signed:ty, abs: $abs:expr, signum: $signum:expr) => {
1426 impl_number!(
1427 $ty,
1428 zero: 0,
1429 one: 1,
1430 min: Self::MIN,
1431 max: Self::MAX,
1432 abs: $abs,
1433 signum: $signum
1434 );
1435 impl Integer for $ty {
1436 type Unsigned = $unsigned;
1437 type Signed = $signed;
1438
1439 fn from_unsigned(v: Self::Unsigned) -> Self {
1440 #[allow(clippy::useless_transmute)]
1441 #[allow(unnecessary_transmutes)]
1442 unsafe { transmute::<Self::Unsigned, Self>(v) }
1443 }
1444
1445 fn from_signed(v: Self::Signed) -> Self {
1446 #[allow(clippy::useless_transmute)]
1447 #[allow(unnecessary_transmutes)]
1448 unsafe { transmute::<Self::Signed, Self>(v) }
1449 }
1450
1451 fn to_unsigned(self) -> Self::Unsigned {
1452 #[allow(clippy::useless_transmute)]
1453 #[allow(unnecessary_transmutes)]
1454 unsafe { transmute::<Self, Self::Unsigned>(self) }
1455 }
1456
1457 fn to_signed(self) -> Self::Signed {
1458 #[allow(clippy::useless_transmute)]
1459 #[allow(unnecessary_transmutes)]
1460 unsafe { transmute::<Self, Self::Signed>(self) }
1461 }
1462
1463 fn cast_ptr<T: Sized, P: Pointer<T>>(self) -> P {
1464 cast_ptr!(T, P, self)
1465 }
1466
1467 #[cfg(not(feature = "std"))]
1468 fn div_euclid(self, rhs: Self) -> Self {
1469 Self::div_euclid(self, rhs)
1470 }
1471
1472 #[cfg(not(feature = "std"))]
1473 fn rem_euclid(self, rhs: Self) -> Self {
1474 Self::rem_euclid(self, rhs)
1475 }
1476
1477 fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
1481 Self::from_str_radix(src, radix)
1482 }
1483
1484 fn count_ones(self) -> u32 {
1485 Self::count_ones(self)
1486 }
1487
1488 fn count_zeros(self) -> u32 {
1489 Self::count_zeros(self)
1490 }
1491
1492 fn leading_zeros(self) -> u32 {
1493 Self::leading_zeros(self)
1494 }
1495
1496 fn trailing_zeros(self) -> u32 {
1497 Self::trailing_zeros(self)
1498 }
1499
1500 fn leading_ones(self) -> u32 {
1501 Self::leading_ones(self)
1502 }
1503
1504 fn trailing_ones(self) -> u32 {
1505 Self::trailing_ones(self)
1506 }
1507
1508 fn isolate_highest_one(self) -> Self {
1509 Self::isolate_highest_one(self)
1510 }
1511
1512 fn isolate_lowest_one(self) -> Self {
1513 Self::isolate_lowest_one(self)
1514 }
1515
1516 fn highest_one(self) -> Option<u32> {
1517 Self::highest_one(self)
1518 }
1519
1520 fn lowest_one(self) -> Option<u32> {
1521 Self::lowest_one(self)
1522 }
1523
1524 fn rotate_left(self, n: u32) -> Self {
1525 Self::rotate_left(self, n)
1526 }
1527
1528 fn rotate_right(self, n: u32) -> Self {
1529 Self::rotate_right(self, n)
1530 }
1531
1532 fn swap_bytes(self) -> Self {
1533 Self::swap_bytes(self)
1534 }
1535
1536 fn reverse_bits(self) -> Self {
1537 Self::reverse_bits(self)
1538 }
1539
1540 fn from_be(x: Self) -> Self {
1541 Self::from_be(x)
1542 }
1543
1544 fn from_le(x: Self) -> Self {
1545 Self::from_le(x)
1546 }
1547
1548 fn to_be(self) -> Self {
1549 Self::to_be(self)
1550 }
1551
1552 fn to_le(self) -> Self {
1553 Self::to_le(self)
1554 }
1555
1556 fn checked_add(self, rhs: Self) -> Option<Self> {
1557 Self::checked_add(self, rhs)
1558 }
1559
1560 fn strict_add(self, rhs: Self) -> Self {
1561 Self::strict_add(self, rhs)
1562 }
1563
1564 unsafe fn unchecked_add(self, rhs: Self) -> Self {
1565 unsafe { Self::unchecked_add(self, rhs) }
1566 }
1567
1568 fn checked_sub(self, rhs: Self) -> Option<Self> {
1569 Self::checked_sub(self, rhs)
1570 }
1571
1572 fn strict_sub(self, rhs: Self) -> Self {
1573 Self::strict_sub(self, rhs)
1574 }
1575
1576 unsafe fn unchecked_sub(self, rhs: Self) -> Self {
1577 unsafe { Self::unchecked_sub(self, rhs) }
1578 }
1579
1580 fn checked_mul(self, rhs: Self) -> Option<Self> {
1581 Self::checked_mul(self, rhs)
1582 }
1583
1584 fn strict_mul(self, rhs: Self) -> Self {
1585 Self::strict_mul(self, rhs)
1586 }
1587
1588 unsafe fn unchecked_mul(self, rhs: Self) -> Self {
1589 unsafe { Self::unchecked_mul(self, rhs) }
1590 }
1591
1592 fn checked_div(self, rhs: Self) -> Option<Self> {
1593 Self::checked_div(self, rhs)
1594 }
1595
1596 fn strict_div(self, rhs: Self) -> Self {
1597 Self::strict_div(self, rhs)
1598 }
1599
1600 fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
1601 Self::checked_div_euclid(self, rhs)
1602 }
1603
1604 fn strict_div_euclid(self, rhs: Self) -> Self {
1605 Self::strict_div_euclid(self, rhs)
1606 }
1607
1608 fn checked_rem(self, rhs: Self) -> Option<Self> {
1609 Self::checked_rem(self, rhs)
1610 }
1611
1612 fn strict_rem(self, rhs: Self) -> Self {
1613 Self::strict_rem(self, rhs)
1614 }
1615
1616 fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1617 Self::checked_rem_euclid(self, rhs)
1618 }
1619
1620 fn strict_rem_euclid(self, rhs: Self) -> Self {
1621 Self::strict_rem_euclid(self, rhs)
1622 }
1623
1624 fn checked_neg(self) -> Option<Self> {
1625 Self::checked_neg(self)
1626 }
1627
1628 fn strict_neg(self) -> Self {
1629 Self::strict_neg(self)
1630 }
1631
1632 fn checked_shl(self, rhs: u32) -> Option<Self> {
1633 Self::checked_shl(self, rhs)
1634 }
1635
1636 fn strict_shl(self, rhs: u32) -> Self {
1637 Self::strict_shl(self, rhs)
1638 }
1639
1640 unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1641 unsafe { Self::unchecked_shl(self, rhs) }
1642 }
1643
1644 fn unbounded_shl(self, rhs: u32) -> Self {
1645 Self::unbounded_shl(self, rhs)
1646 }
1647
1648 fn checked_shr(self, rhs: u32) -> Option<Self> {
1649 Self::checked_shr(self, rhs)
1650 }
1651
1652 fn strict_shr(self, rhs: u32) -> Self {
1653 Self::strict_shr(self, rhs)
1654 }
1655
1656 unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1657 unsafe { Self::unchecked_shr(self, rhs) }
1658 }
1659
1660 fn unbounded_shr(self, rhs: u32) -> Self {
1661 Self::unbounded_shr(self, rhs)
1662 }
1663
1664 fn checked_pow(self, exp: u32) -> Option<Self> {
1665 Self::checked_pow(self, exp)
1666 }
1667
1668 fn strict_pow(self, exp: u32) -> Self {
1669 Self::strict_pow(self, exp)
1670 }
1671
1672 fn saturating_add(self, rhs: Self) -> Self {
1673 Self::saturating_add(self, rhs)
1674 }
1675
1676 fn saturating_sub(self, rhs: Self) -> Self {
1677 Self::saturating_sub(self, rhs)
1678 }
1679
1680 fn saturating_mul(self, rhs: Self) -> Self {
1681 Self::saturating_mul(self, rhs)
1682 }
1683
1684 fn saturating_div(self, rhs: Self) -> Self {
1685 Self::saturating_div(self, rhs)
1686 }
1687
1688 fn saturating_pow(self, exp: u32) -> Self {
1689 Self::saturating_pow(self, exp)
1690 }
1691
1692 fn wrapping_add(self, rhs: Self) -> Self {
1693 Self::wrapping_add(self, rhs)
1694 }
1695
1696 fn wrapping_sub(self, rhs: Self) -> Self {
1697 Self::wrapping_sub(self, rhs)
1698 }
1699
1700 fn wrapping_mul(self, rhs: Self) -> Self {
1701 Self::wrapping_mul(self, rhs)
1702 }
1703
1704 fn wrapping_div(self, rhs: Self) -> Self {
1705 Self::wrapping_div(self, rhs)
1706 }
1707
1708 fn wrapping_div_euclid(self, rhs: Self) -> Self {
1709 Self::wrapping_div_euclid(self, rhs)
1710 }
1711
1712 fn wrapping_rem(self, rhs: Self) -> Self {
1713 Self::wrapping_rem(self, rhs)
1714 }
1715
1716 fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1717 Self::wrapping_rem_euclid(self, rhs)
1718 }
1719
1720 fn wrapping_neg(self) -> Self {
1721 Self::wrapping_neg(self)
1722 }
1723
1724 fn wrapping_shl(self, rhs: u32) -> Self {
1725 Self::wrapping_shl(self, rhs)
1726 }
1727
1728 fn wrapping_shr(self, rhs: u32) -> Self {
1729 Self::wrapping_shr(self, rhs)
1730 }
1731
1732 fn wrapping_pow(self, exp: u32) -> Self {
1733 Self::wrapping_pow(self, exp)
1734 }
1735
1736 fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1737 Self::overflowing_add(self, rhs)
1738 }
1739
1740 fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1741 Self::overflowing_sub(self, rhs)
1742 }
1743
1744 fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1745 Self::overflowing_mul(self, rhs)
1746 }
1747
1748 fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1749 Self::overflowing_div(self, rhs)
1750 }
1751
1752 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1753 Self::overflowing_div_euclid(self, rhs)
1754 }
1755
1756 fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1757 Self::overflowing_rem(self, rhs)
1758 }
1759
1760 fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1761 Self::overflowing_rem_euclid(self, rhs)
1762 }
1763
1764 fn overflowing_neg(self) -> (Self, bool) {
1765 Self::overflowing_neg(self)
1766 }
1767
1768 fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1769 Self::overflowing_shl(self, rhs)
1770 }
1771
1772 fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1773 Self::overflowing_shr(self, rhs)
1774 }
1775
1776 fn overflowing_pow(self, exp: u32) -> (Self, bool) {
1777 Self::overflowing_pow(self, exp)
1778 }
1779
1780 fn pow(self, exp: u32) -> Self {
1781 Self::pow(self, exp)
1782 }
1783
1784 fn isqrt(self) -> Self {
1785 Self::isqrt(self)
1786 }
1787
1788 fn ilog(self, base: Self) -> u32 {
1789 Self::ilog(self, base)
1790 }
1791
1792 fn ilog2(self) -> u32 {
1793 Self::ilog2(self)
1794 }
1795
1796 fn ilog10(self) -> u32 {
1797 Self::ilog10(self)
1798 }
1799
1800 fn checked_ilog(self, base: Self) -> Option<u32> {
1801 Self::checked_ilog(self, base)
1802 }
1803
1804 fn checked_ilog2(self) -> Option<u32> {
1805 Self::checked_ilog2(self)
1806 }
1807
1808 fn checked_ilog10(self) -> Option<u32> {
1809 Self::checked_ilog10(self)
1810 }
1811
1812 fn abs_diff(self, other: Self) -> Self::Unsigned {
1813 Self::abs_diff(self, other)
1814 }
1815
1816 }
1818 };
1819}
1820
1821pub trait Unsigned: Integer<Unsigned = Self> + From<u8> {
1822 #[must_use = "this returns the result of the operation, without modifying the original"]
1827 fn bit_width(self) -> u32;
1828
1829 #[must_use = "this returns the result of the operation, without modifying the original"]
1831 fn cast_signed(self) -> Self::Signed;
1832
1833 #[must_use = "this returns the result of the operation, without modifying the original"]
1835 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
1836
1837 #[must_use = "this returns the result of the operation, without modifying the original"]
1839 fn strict_add_signed(self, rhs: Self::Signed) -> Self;
1840
1841 #[must_use = "this returns the result of the operation, without modifying the original"]
1843 fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self>;
1844
1845 #[must_use = "this returns the result of the operation, without modifying the original"]
1847 fn strict_sub_signed(self, rhs: Self::Signed) -> Self;
1848
1849 fn checked_signed_diff(self, rhs: Self) -> Option<Self::Signed>;
1851
1852 #[must_use = "this returns the result of the operation, without modifying the original"]
1854 fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
1855
1856 #[must_use = "this returns the result of the operation, without modifying the original"]
1858 fn saturating_sub_signed(self, rhs: Self::Signed) -> Self;
1859
1860 #[must_use = "this returns the result of the operation, without modifying the original"]
1862 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
1863
1864 #[must_use = "this returns the result of the operation, without modifying the original"]
1866 fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self;
1867
1868 #[must_use = "this returns the result of the operation, without modifying the original"]
1870 fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool);
1871
1872 #[must_use = "this returns the result of the operation, without modifying the original"]
1874 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
1875
1876 #[must_use = "this returns the result of the operation, without modifying the original"]
1878 fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool);
1879
1880 #[must_use = "this returns the result of the operation, without modifying the original"]
1882 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool);
1883
1884 #[must_use = "this returns the result of the operation, without modifying the original"]
1886 fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self);
1887
1888 #[must_use = "this returns the result of the operation, without modifying the original"]
1890 fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> (Self, Self);
1891
1892 #[must_use = "this returns the result of the operation, without modifying the original"]
1894 fn div_ceil(self, rhs: Self) -> Self;
1895
1896 #[must_use = "this returns the result of the operation, without modifying the original"]
1898 fn next_multiple_of(self, rhs: Self) -> Self;
1899
1900 #[must_use = "this returns the result of the operation, without modifying the original"]
1902 fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>;
1903
1904 #[must_use]
1906 fn is_multiple_of(self, rhs: Self) -> bool;
1907
1908 #[must_use]
1910 fn is_power_of_two(self) -> bool;
1911
1912 #[must_use = "this returns the result of the operation, without modifying the original"]
1914 fn next_power_of_two(self) -> Self;
1915
1916 #[must_use = "this returns the result of the operation, without modifying the original"]
1918 fn checked_next_power_of_two(self) -> Option<Self>;
1919
1920 }
1922
1923macro_rules! impl_unsigned {
1924 ($ty:ty, $signed:ty) => {
1925 impl_integer!(
1926 $ty,
1927 Self,
1928 $signed,
1929 abs: |v| v,
1930 signum: |v| (v > 0) as Self
1931 );
1932 impl Unsigned for $ty {
1933 fn bit_width(self) -> u32 {
1937 Self::bit_width(self)
1938 }
1939
1940 fn cast_signed(self) -> Self::Signed {
1941 Self::cast_signed(self)
1942 }
1943
1944 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self> {
1945 Self::checked_add_signed(self, rhs)
1946 }
1947
1948 fn strict_add_signed(self, rhs: Self::Signed) -> Self {
1949 Self::strict_add_signed(self, rhs)
1950 }
1951
1952 fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self> {
1953 Self::checked_sub_signed(self, rhs)
1954 }
1955
1956 fn strict_sub_signed(self, rhs: Self::Signed) -> Self {
1957 Self::strict_sub_signed(self, rhs)
1958 }
1959
1960 fn checked_signed_diff(self, rhs: Self) -> Option<Self::Signed> {
1961 Self::checked_signed_diff(self, rhs)
1962 }
1963
1964 fn saturating_add_signed(self, rhs: Self::Signed) -> Self {
1965 Self::saturating_add_signed(self, rhs)
1966 }
1967
1968 fn saturating_sub_signed(self, rhs: Self::Signed) -> Self {
1969 Self::saturating_sub_signed(self, rhs)
1970 }
1971
1972 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self {
1973 Self::wrapping_add_signed(self, rhs)
1974 }
1975
1976 fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self {
1977 Self::wrapping_sub_signed(self, rhs)
1978 }
1979
1980 fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
1981 Self::carrying_add(self, rhs, carry)
1982 }
1983
1984 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool) {
1985 Self::overflowing_add_signed(self, rhs)
1986 }
1987
1988 fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
1989 Self::borrowing_sub(self, rhs, borrow)
1990 }
1991
1992 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool) {
1993 Self::overflowing_sub_signed(self, rhs)
1994 }
1995
1996 fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
1997 Self::carrying_mul(self, rhs, carry)
1998 }
1999
2000 fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> (Self, Self) {
2001 Self::carrying_mul_add(self, rhs, carry, add)
2002 }
2003
2004 fn div_ceil(self, rhs: Self) -> Self {
2005 Self::div_ceil(self, rhs)
2006 }
2007
2008 fn next_multiple_of(self, rhs: Self) -> Self {
2009 Self::next_multiple_of(self, rhs)
2010 }
2011
2012 fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
2013 Self::checked_next_multiple_of(self, rhs)
2014 }
2015
2016 fn is_multiple_of(self, rhs: Self) -> bool {
2017 Self::is_multiple_of(self, rhs)
2018 }
2019
2020 fn is_power_of_two(self) -> bool {
2021 Self::is_power_of_two(self)
2022 }
2023
2024 fn next_power_of_two(self) -> Self {
2025 Self::next_power_of_two(self)
2026 }
2027
2028 fn checked_next_power_of_two(self) -> Option<Self> {
2029 Self::checked_next_power_of_two(self)
2030 }
2031
2032 }
2034 };
2035}
2036
2037impl_unsigned!(u8, i8);
2038impl_unsigned!(u16, i16);
2039impl_unsigned!(u32, i32);
2040impl_unsigned!(u64, i64);
2041impl_unsigned!(u128, i128);
2042impl_unsigned!(usize, isize);
2043
2044pub trait Signed: Integer<Signed = Self> + Neg<Output = Self> + From<i8> {
2045 #[must_use = "this returns the result of the operation, without modifying the original"]
2050 fn cast_unsigned(self) -> Self::Unsigned;
2051
2052 #[must_use = "this returns the result of the operation, without modifying the original"]
2054 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
2055
2056 #[must_use = "this returns the result of the operation, without modifying the original"]
2058 fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self;
2059
2060 #[must_use = "this returns the result of the operation, without modifying the original"]
2062 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
2063
2064 #[must_use = "this returns the result of the operation, without modifying the original"]
2066 fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
2067
2068 #[must_use = "this returns the result of the operation, without modifying the original"]
2074 unsafe fn unchecked_neg(self) -> Self;
2075
2076 #[must_use = "this returns the result of the operation, without modifying the original"]
2078 fn checked_abs(self) -> Option<Self>;
2079
2080 #[must_use = "this returns the result of the operation, without modifying the original"]
2082 fn strict_abs(self) -> Self;
2083
2084 #[must_use = "this returns the result of the operation, without modifying the original"]
2086 fn checked_isqrt(self) -> Option<Self>;
2087
2088 #[must_use = "this returns the result of the operation, without modifying the original"]
2090 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self;
2091
2092 #[must_use = "this returns the result of the operation, without modifying the original"]
2094 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
2095
2096 #[must_use = "this returns the result of the operation, without modifying the original"]
2098 fn saturating_neg(self) -> Self;
2099
2100 #[must_use = "this returns the result of the operation, without modifying the original"]
2102 fn saturating_abs(self) -> Self;
2103
2104 #[must_use = "this returns the result of the operation, without modifying the original"]
2106 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self;
2107
2108 #[must_use = "this returns the result of the operation, without modifying the original"]
2110 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
2111
2112 #[must_use = "this returns the result of the operation, without modifying the original"]
2114 fn wrapping_abs(self) -> Self;
2115
2116 #[must_use = "this returns the result of the operation, without modifying the original"]
2118 fn unsigned_abs(self) -> Self::Unsigned;
2119
2120 #[must_use = "this returns the result of the operation, without modifying the original"]
2122 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
2123
2124 #[must_use = "this returns the result of the operation, without modifying the original"]
2126 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
2127
2128 #[must_use = "this returns the result of the operation, without modifying the original"]
2130 fn overflowing_abs(self) -> (Self, bool);
2131
2132 #[must_use = "this returns the result of the operation, without modifying the original"]
2134 fn abs(self) -> Self;
2135
2136 #[must_use = "this returns the result of the operation, without modifying the original"]
2138 fn signum(self) -> Self;
2139
2140 #[must_use]
2142 fn is_positive(self) -> bool;
2143
2144 #[must_use]
2146 fn is_negative(self) -> bool;
2147
2148 }
2150
2151macro_rules! impl_signed {
2152 ($ty:ty, $unsigned:ty) => {
2153 impl_integer!($ty, $unsigned, Self, abs: Self::abs, signum: Self::signum);
2154 impl Signed for $ty {
2155 fn cast_unsigned(self) -> Self::Unsigned {
2159 Self::cast_unsigned(self)
2160 }
2161
2162 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self> {
2163 Self::checked_add_unsigned(self, rhs)
2164 }
2165
2166 fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self {
2167 Self::strict_add_unsigned(self, rhs)
2168 }
2169
2170 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self> {
2171 Self::checked_sub_unsigned(self, rhs)
2172 }
2173
2174 fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
2175 Self::strict_sub_unsigned(self, rhs)
2176 }
2177
2178 unsafe fn unchecked_neg(self) -> Self {
2179 unsafe { Self::unchecked_neg(self) }
2180 }
2181
2182 fn checked_abs(self) -> Option<Self> {
2183 Self::checked_abs(self)
2184 }
2185
2186 fn strict_abs(self) -> Self {
2187 Self::strict_abs(self)
2188 }
2189
2190 fn checked_isqrt(self) -> Option<Self> {
2191 Self::checked_isqrt(self)
2192 }
2193
2194 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self {
2195 Self::saturating_add_unsigned(self, rhs)
2196 }
2197
2198 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
2199 Self::saturating_sub_unsigned(self, rhs)
2200 }
2201
2202 fn saturating_neg(self) -> Self {
2203 Self::saturating_neg(self)
2204 }
2205
2206 fn saturating_abs(self) -> Self {
2207 Self::saturating_abs(self)
2208 }
2209
2210 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self {
2211 Self::wrapping_add_unsigned(self, rhs)
2212 }
2213
2214 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
2215 Self::wrapping_sub_unsigned(self, rhs)
2216 }
2217
2218 fn wrapping_abs(self) -> Self {
2219 Self::wrapping_abs(self)
2220 }
2221
2222 fn unsigned_abs(self) -> Self::Unsigned {
2223 Self::unsigned_abs(self)
2224 }
2225
2226 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool) {
2227 Self::overflowing_add_unsigned(self, rhs)
2228 }
2229
2230 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool) {
2231 Self::overflowing_sub_unsigned(self, rhs)
2232 }
2233
2234 fn overflowing_abs(self) -> (Self, bool) {
2235 Self::overflowing_abs(self)
2236 }
2237
2238 fn abs(self) -> Self {
2239 Self::abs(self)
2240 }
2241
2242 fn signum(self) -> Self {
2243 Self::signum(self)
2244 }
2245
2246 fn is_positive(self) -> bool {
2247 Self::is_positive(self)
2248 }
2249
2250 fn is_negative(self) -> bool {
2251 Self::is_negative(self)
2252 }
2253
2254 }
2256 };
2257}
2258
2259impl_signed!(i8, u8);
2260impl_signed!(i16, u16);
2261impl_signed!(i32, u32);
2262impl_signed!(i64, u64);
2263impl_signed!(i128, u128);
2264impl_signed!(isize, usize);
2265
2266#[cfg(test)]
2267mod test {
2268 use super::*;
2269
2270 #[test]
2271 fn test_subnormal_consts() {
2272 assert_eq!(f32::MIN_POSITIVE_SUBNORMAL, f32::from_bits(1));
2273 assert_eq!(f32::MAX_NEGATIVE_SUBNORMAL, -f32::from_bits(1));
2274 assert_eq!(f64::MIN_POSITIVE_SUBNORMAL, f64::from_bits(1));
2275 assert_eq!(f64::MAX_NEGATIVE_SUBNORMAL, -f64::from_bits(1));
2276 }
2277
2278 #[cfg(feature = "std")]
2279 #[test]
2280 fn test_float_floor() {
2281 assert_eq!(<f64 as Float>::floor(1.5), 1.0);
2282 }
2283
2284 #[test]
2285 fn test_euclid_core() {
2286 fn test_int<T: Integer>(a: T, b: T) -> (T, T) {
2287 (a.div_euclid(b), a.rem_euclid(b))
2288 }
2289
2290 assert_eq!(test_int(-7, 4), (-2, 1));
2291 }
2292
2293 #[cfg(feature = "std")]
2294 #[test]
2295 fn test_euclid_std() {
2296 fn test_num<T: Number>(a: T, b: T) -> (T, T) {
2297 (a.div_euclid(b), a.rem_euclid(b))
2298 }
2299
2300 assert_eq!(test_num(-7, 4), (-2, 1));
2301 assert_eq!(test_num(-7.0, 4.0), (-2.0, 1.0));
2302 }
2303
2304 #[test]
2305 fn test_abs() {
2306 fn test_abs<T: Number>(a: T) -> T {
2307 a.abs()
2308 }
2309
2310 assert_eq!(test_abs(1i32), 1);
2311 assert_eq!(test_abs(1u32), 1);
2312 assert_eq!(test_abs(1.0), 1.0);
2313
2314 assert_eq!(test_abs(-1i32), 1);
2315 assert_eq!(test_abs(-1.0), 1.0);
2316
2317 assert!(test_abs(f64::NAN).is_nan());
2318 }
2319
2320 #[test]
2321 fn test_signum() {
2322 fn test_signum<T: Number>(a: T) -> T {
2323 a.signum()
2324 }
2325
2326 assert_eq!(test_signum(123i32), 1);
2327 assert_eq!(test_signum(123u32), 1);
2328 assert_eq!(test_signum(123.0), 1.0);
2329
2330 assert_eq!(test_signum(0i32), 0);
2331 assert_eq!(test_signum(0u32), 0);
2332 assert_eq!(test_signum(0.0), 1.0);
2333 assert_eq!(test_signum(-0.0), -1.0);
2334
2335 assert_eq!(test_signum(-123i32), -1);
2336 assert_eq!(test_signum(-123.0), -1.0);
2337
2338 assert!(test_signum(f64::NAN).is_nan());
2339 }
2340
2341 #[test]
2342 fn test_int_conversions() {
2343 fn inner<T: Integer>(v: T) {
2344 let bytes = v.to_bytes();
2345 let v2: T = T::from_bytes(bytes);
2346 assert_eq!(v2, v);
2347
2348 let signed = v.to_signed();
2349 let v2 = T::from_signed(signed);
2350 assert_eq!(v2, v);
2351
2352 let unsigned = v.to_unsigned();
2353 let v2 = T::from_unsigned(unsigned);
2354 assert_eq!(v2, v);
2355 }
2356
2357 inner(123u64);
2358 inner(-123i64);
2359 }
2360
2361 #[test]
2362 fn test_number_ops() {
2363 #[allow(clippy::op_ref)]
2364 fn number_ops<T: Number>() {
2365 let three = T::TWO + T::ONE;
2366 let four = T::TWO + T::TWO;
2367 let eight = four * T::TWO;
2368
2369 assert_eq!(T::ONE + T::ONE, T::TWO);
2370 assert_eq!(T::ONE + &T::ONE, T::TWO);
2371
2372 assert_eq!(T::TWO - T::ONE, T::ONE);
2373 assert_eq!(T::TWO - &T::ONE, T::ONE);
2374
2375 assert_eq!(T::TWO * T::TWO, four);
2376 assert_eq!(T::TWO * &T::TWO, four);
2377
2378 assert_eq!(four / T::TWO, T::TWO);
2379 assert_eq!(four / &T::TWO, T::TWO);
2380
2381 assert_eq!(three % T::TWO, T::ONE);
2382 assert_eq!(three % &T::TWO, T::ONE);
2383
2384 let mut v = T::ZERO;
2385 v += T::TWO;
2386 v += &T::TWO;
2387 assert_eq!(v, four);
2388
2389 v -= T::ONE;
2390 v -= &T::ONE;
2391 assert_eq!(v, T::TWO);
2392
2393 v *= T::TWO;
2394 v *= &T::TWO;
2395 assert_eq!(v, eight);
2396
2397 v /= T::TWO;
2398 v /= &T::TWO;
2399 assert_eq!(v, T::TWO);
2400
2401 v += T::ONE;
2402 v %= three;
2403 assert_eq!(v, T::ZERO);
2404 v += three;
2405 v %= &T::TWO;
2406 assert_eq!(v, T::ONE);
2407 }
2408
2409 number_ops::<u8>();
2410 }
2411
2412 #[test]
2413 fn test_integer_ops() {
2414 #[allow(clippy::op_ref)]
2415 fn integer_ops<T: Integer>() {
2416 let three = T::TWO + T::ONE;
2417 let four = T::TWO + T::TWO;
2418 let seven = four * T::TWO - T::ONE;
2419
2420 assert_eq!(!!T::ONE, T::ONE);
2421
2422 assert_eq!(three & T::TWO, T::TWO);
2423 assert_eq!(three & &T::TWO, T::TWO);
2424
2425 assert_eq!(T::ONE | T::TWO, three);
2426 assert_eq!(T::ONE | &T::TWO, three);
2427
2428 assert_eq!(three ^ T::TWO, T::ONE);
2429 assert_eq!(three ^ &T::TWO, T::ONE);
2430
2431 assert_eq!(T::ONE << T::TWO, four);
2432 assert_eq!(T::ONE << &T::TWO, four);
2433
2434 assert_eq!(seven >> T::ONE, three);
2435 assert_eq!(seven >> &T::ONE, three);
2436
2437 let mut v = T::ZERO;
2438 v |= T::ONE;
2439 assert_eq!(v, T::ONE);
2440 v |= &T::TWO;
2441 assert_eq!(v, three);
2442
2443 v &= T::TWO;
2444 assert_eq!(v, T::TWO);
2445 v &= &T::ONE;
2446 assert_eq!(v, T::ZERO);
2447
2448 v ^= three;
2449 assert_eq!(v, three);
2450 v ^= &T::TWO;
2451 assert_eq!(v, T::ONE);
2452
2453 v <<= T::ONE;
2454 assert_eq!(v, T::TWO);
2455 v <<= &T::ZERO;
2456 assert_eq!(v, T::TWO);
2457
2458 v >>= T::ZERO;
2459 assert_eq!(v, T::TWO);
2460 v >>= &T::TWO;
2461 assert_eq!(v, T::ZERO);
2462 }
2463
2464 integer_ops::<u8>();
2465 }
2466
2467 #[test]
2468 fn casts() {
2469 assert_eq!(0u8.cast_int::<i8>(), 0i8);
2470 assert_eq!(0u8.cast_number::<i8>(), 0i8);
2471 assert_eq!(0u8.cast_float::<f32>(), 0f32);
2472 assert_eq!(0u8.cast_number::<f32>(), 0f32);
2473
2474 assert_eq!(0f32.cast_int::<u8>(), 0u8);
2475 assert_eq!(0f32.cast_number::<u8>(), 0u8);
2476 assert_eq!(0f32.cast_float::<f64>(), 0f64);
2477 assert_eq!(0f32.cast_number::<f64>(), 0f64);
2478 }
2479}