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]
223 fn from_be_bytes(bytes: Self::ByteArray) -> Self;
224 #[must_use]
225 fn from_le_bytes(bytes: Self::ByteArray) -> Self;
226 #[must_use]
227 fn from_ne_bytes(bytes: Self::ByteArray) -> Self;
228
229 #[must_use]
230 fn cast_float<T: Float>(self) -> T;
231 #[must_use]
232 fn cast_number<T: Number>(self) -> T;
233
234 #[must_use = "this returns the result of the operation, without modifying the original"]
236 fn abs(self) -> Self;
237
238 #[must_use = "this returns the result of the operation, without modifying the original"]
240 fn signum(self) -> Self;
241
242 #[cfg(feature = "std")]
243 #[must_use = "this returns the result of the operation, without modifying the original"]
244 fn div_euclid(self, rhs: Self) -> Self;
245
246 #[cfg(feature = "std")]
247 #[must_use = "this returns the result of the operation, without modifying the original"]
248 fn rem_euclid(self, rhs: Self) -> Self;
249}
250
251macro_rules! impl_number {
252 (
253 $ty:ty,
254 zero: $zero:expr,
255 one: $one:expr,
256 min: $min:expr,
257 max: $max:expr,
258 abs: $abs:expr,
259 signum: $signum:expr
260 ) => {
261 impl_number_like!($ty,
262 underlying: Self,
263 min: $min,
264 max: $max,
265 try_from_underlying: |v| Some(v)
266 );
267 impl Number for $ty {
268 const ZERO: Self = $zero;
269 const ONE: Self = $one;
270 const TWO: Self = $one + $one;
271
272 fn from_bytes(bytes: Self::ByteArray) -> Self {
273 #[allow(unnecessary_transmutes)]
274 unsafe { transmute::<Self::ByteArray, Self>(bytes) }
275 }
276
277 fn as_mut_bytes(&mut self) -> &mut Self::ByteArray {
278 unsafe { transmute::<&mut Self, &mut Self::ByteArray>(self) }
279 }
280
281 fn from_be_bytes(bytes: Self::ByteArray) -> Self {
282 Self::from_be_bytes(bytes)
283 }
284
285 fn from_le_bytes(bytes: Self::ByteArray) -> Self {
286 Self::from_le_bytes(bytes)
287 }
288
289 fn from_ne_bytes(bytes: Self::ByteArray) -> Self {
290 Self::from_ne_bytes(bytes)
291 }
292
293 fn cast_float<T: Float>(self) -> T {
294 cast_generic!(self => T; f32 f64)
295 }
296
297 fn cast_number<T: Number>(self) -> T {
298 cast_generic!(self => T;
299 i8 i16 i32 i64 i128 isize
300 u8 u16 u32 u64 u128 usize
301 f32 f64
302 )
303 }
304
305 fn abs(self) -> Self {
306 $abs(self)
307 }
308
309 fn signum(self) -> Self {
310 $signum(self)
311 }
312
313 #[cfg(feature = "std")]
314 fn div_euclid(self, rhs: Self) -> Self {
315 Self::div_euclid(self, rhs)
316 }
317
318 #[cfg(feature = "std")]
319 fn rem_euclid(self, rhs: Self) -> Self {
320 Self::rem_euclid(self, rhs)
321 }
322 }
323 };
324}
325
326#[rustfmt::skip] pub trait Float:
328 Number
329 + Neg<Output = Self>
330 + From<f32>
331 + Into<f64>
332 + From<i8>
333 + From<i16>
334 + From<u8>
335 + From<u16>
336{
337 const RADIX: u32;
338 const MANTISSA_DIGITS: u32;
339 const DIGITS: u32;
340 const EPSILON: Self;
341
342 const MIN_FINITE: Self;
343 const MIN_POSITIVE_SUBNORMAL: Self;
344 const MIN_POSITIVE_NORMAL: Self;
345 const MIN_EXP: i32;
346 const MIN_10_EXP: i32;
347
348 const MAX_FINITE: Self;
349 const MAX_NEGATIVE_SUBNORMAL: Self;
350 const MAX_NEGATIVE_NORMAL: Self;
351 const MAX_EXP: i32;
352 const MAX_10_EXP: i32;
353
354 const NAN: Self;
355 const INFINITY: Self;
356 const NEG_INFINITY: Self;
357
358 const NEG_ZERO: Self;
359
360 type Bits: Unsigned;
361
362 #[must_use]
367 fn is_nan(self) -> bool;
368
369 #[must_use]
371 fn is_infinite(self) -> bool;
372
373 #[must_use]
375 fn is_finite(self) -> bool;
376
377 #[must_use]
379 fn is_subnormal(self) -> bool;
380
381 #[must_use]
383 fn is_normal(self) -> bool;
384
385 #[must_use]
387 fn classify(self) -> FpCategory;
388
389 #[must_use]
391 fn is_sign_positive(self) -> bool;
392
393 #[must_use]
395 fn is_sign_negative(self) -> bool;
396
397 #[must_use = "method returns a new number and does not mutate the original value"]
399 fn next_up(self) -> Self;
400
401 #[must_use = "method returns a new number and does not mutate the original value"]
403 fn next_down(self) -> Self;
404
405 #[must_use = "this returns the result of the operation, without modifying the original"]
407 fn recip(self) -> Self;
408
409 #[must_use = "this returns the result of the operation, without modifying the original"]
411 fn to_degrees(self) -> Self;
412
413 #[must_use = "this returns the result of the operation, without modifying the original"]
415 fn to_radians(self) -> Self;
416
417 #[must_use = "this returns the result of the comparison, without modifying either input"]
419 fn max(self, other: Self) -> Self;
420
421 #[must_use = "this returns the result of the comparison, without modifying either input"]
423 fn min(self, other: Self) -> Self;
424
425 #[must_use = "this returns the result of the operation, without modifying the original"]
427 fn midpoint(self, other: Self) -> Self;
428
429 #[must_use = "this returns the result of the operation, without modifying the original"]
431 fn to_bits(self) -> Self::Bits;
432
433 #[must_use]
435 fn from_bits(v: Self::Bits) -> Self;
436
437 #[must_use]
439 fn total_cmp(&self, other: &Self) -> core::cmp::Ordering;
440
441 #[must_use = "method returns a new number and does not mutate the original value"]
443 fn clamp(self, min: Self, max: Self) -> Self;
444
445 #[must_use = "method returns a new number and does not mutate the original value"]
447 fn copysign(self, sign: Self) -> Self;
448
449 #[must_use = "method returns a new number and does not mutate the original value"]
451 fn algebraic_add(self, rhs: Self) -> Self;
452
453 #[must_use = "method returns a new number and does not mutate the original value"]
455 fn algebraic_sub(self, rhs: Self) -> Self;
456
457 #[must_use = "method returns a new number and does not mutate the original value"]
459 fn algebraic_mul(self, rhs: Self) -> Self;
460
461 #[must_use = "method returns a new number and does not mutate the original value"]
463 fn algebraic_div(self, rhs: Self) -> Self;
464
465 #[must_use = "method returns a new number and does not mutate the original value"]
467 fn algebraic_rem(self, rhs: Self) -> Self;
468
469 #[cfg(feature = "std")]
471 #[must_use = "method returns a new number and does not mutate the original value"]
472 fn floor(self) -> Self;
473
474 #[cfg(feature = "std")]
476 #[must_use = "method returns a new number and does not mutate the original value"]
477 fn ceil(self) -> Self;
478
479 #[cfg(feature = "std")]
481 #[must_use = "method returns a new number and does not mutate the original value"]
482 fn round(self) -> Self;
483
484 #[cfg(feature = "std")]
486 #[must_use = "method returns a new number and does not mutate the original value"]
487 fn round_ties_even(self) -> Self;
488
489 #[cfg(feature = "std")]
491 #[must_use = "method returns a new number and does not mutate the original value"]
492 fn trunc(self) -> Self;
493
494 #[cfg(feature = "std")]
496 #[must_use = "method returns a new number and does not mutate the original value"]
497 fn fract(self) -> Self;
498
499 #[cfg(feature = "std")]
501 #[must_use = "method returns a new number and does not mutate the original value"]
502 fn mul_add(self, a: Self, b: Self) -> Self;
503
504 #[cfg(feature = "std")]
506 #[must_use = "method returns a new number and does not mutate the original value"]
507 fn powi(self, n: i32) -> Self;
508
509 #[cfg(feature = "std")]
511 #[must_use = "method returns a new number and does not mutate the original value"]
512 fn powf(self, n: Self) -> Self;
513
514 #[cfg(feature = "std")]
516 #[must_use = "method returns a new number and does not mutate the original value"]
517 fn sqrt(self) -> Self;
518
519 #[cfg(feature = "std")]
521 #[must_use = "method returns a new number and does not mutate the original value"]
522 fn exp(self) -> Self;
523
524 #[cfg(feature = "std")]
526 #[must_use = "method returns a new number and does not mutate the original value"]
527 fn exp2(self) -> Self;
528
529 #[cfg(feature = "std")]
531 #[must_use = "method returns a new number and does not mutate the original value"]
532 fn ln(self) -> Self;
533
534 #[cfg(feature = "std")]
536 #[must_use = "method returns a new number and does not mutate the original value"]
537 fn log(self, base: Self) -> Self;
538
539 #[cfg(feature = "std")]
541 #[must_use = "method returns a new number and does not mutate the original value"]
542 fn log2(self) -> Self;
543
544 #[cfg(feature = "std")]
546 #[must_use = "method returns a new number and does not mutate the original value"]
547 fn log10(self) -> Self;
548
549 #[cfg(feature = "std")]
551 #[must_use = "method returns a new number and does not mutate the original value"]
552 fn cbrt(self) -> Self;
553
554 #[cfg(feature = "std")]
556 #[must_use = "method returns a new number and does not mutate the original value"]
557 fn hypot(self, other: Self) -> Self;
558
559 #[cfg(feature = "std")]
561 #[must_use = "method returns a new number and does not mutate the original value"]
562 fn sin(self) -> Self;
563
564 #[cfg(feature = "std")]
566 #[must_use = "method returns a new number and does not mutate the original value"]
567 fn cos(self) -> Self;
568
569 #[cfg(feature = "std")]
571 #[must_use = "method returns a new number and does not mutate the original value"]
572 fn tan(self) -> Self;
573
574 #[cfg(feature = "std")]
576 #[must_use = "method returns a new number and does not mutate the original value"]
577 fn asin(self) -> Self;
578
579 #[cfg(feature = "std")]
581 #[must_use = "method returns a new number and does not mutate the original value"]
582 fn acos(self) -> Self;
583
584 #[cfg(feature = "std")]
586 #[must_use = "method returns a new number and does not mutate the original value"]
587 fn atan(self) -> Self;
588
589 #[cfg(feature = "std")]
591 #[must_use = "method returns a new number and does not mutate the original value"]
592 fn atan2(self, other: Self) -> Self;
593
594 #[cfg(feature = "std")]
596 #[must_use = "this returns the result of the operation, without modifying the original"]
597 fn sin_cos(self) -> (Self, Self);
598
599 #[cfg(feature = "std")]
601 #[must_use = "method returns a new number and does not mutate the original value"]
602 fn exp_m1(self) -> Self;
603
604 #[cfg(feature = "std")]
606 #[must_use = "method returns a new number and does not mutate the original value"]
607 fn ln_1p(self) -> Self;
608
609 #[cfg(feature = "std")]
611 #[must_use = "method returns a new number and does not mutate the original value"]
612 fn sinh(self) -> Self;
613
614 #[cfg(feature = "std")]
616 #[must_use = "method returns a new number and does not mutate the original value"]
617 fn cosh(self) -> Self;
618
619 #[cfg(feature = "std")]
621 #[must_use = "method returns a new number and does not mutate the original value"]
622 fn tanh(self) -> Self;
623
624 #[cfg(feature = "std")]
626 #[must_use = "method returns a new number and does not mutate the original value"]
627 fn asinh(self) -> Self;
628
629 #[cfg(feature = "std")]
631 #[must_use = "method returns a new number and does not mutate the original value"]
632 fn acosh(self) -> Self;
633
634 #[cfg(feature = "std")]
636 #[must_use = "method returns a new number and does not mutate the original value"]
637 fn atanh(self) -> Self;
638
639 }
641
642macro_rules! impl_float {
643 ($ty:ty, $bits:ty, $min_positive_subnormal:expr) => {
644 impl_number!(
645 $ty,
646 zero: 0.0,
647 one: 1.0,
648 min: Self::NEG_INFINITY,
649 max: Self::INFINITY,
650 abs: Self::abs,
651 signum: Self::signum
652 );
653 impl Float for $ty {
654 const RADIX: u32 = Self::RADIX;
655 const MANTISSA_DIGITS: u32 = Self::MANTISSA_DIGITS;
656 const DIGITS: u32 = Self::DIGITS;
657 const EPSILON: Self = Self::EPSILON;
658
659 const MIN_FINITE: Self = Self::MIN;
660 const MIN_POSITIVE_SUBNORMAL: Self = $min_positive_subnormal;
661 const MIN_POSITIVE_NORMAL: Self = Self::MIN_POSITIVE;
662 const MIN_EXP: i32 = Self::MIN_EXP;
663 const MIN_10_EXP: i32 = Self::MIN_10_EXP;
664
665 const MAX_FINITE: Self = Self::MAX;
666 const MAX_NEGATIVE_SUBNORMAL: Self = -Self::MIN_POSITIVE_SUBNORMAL;
667 const MAX_NEGATIVE_NORMAL: Self = -Self::MIN_POSITIVE_NORMAL;
668 const MAX_EXP: i32 = Self::MAX_EXP;
669 const MAX_10_EXP: i32 = Self::MAX_10_EXP;
670
671 const NAN: Self = Self::NAN;
672 const INFINITY: Self = Self::INFINITY;
673 const NEG_INFINITY: Self = Self::NEG_INFINITY;
674
675 const NEG_ZERO: Self = -0.0;
676
677 type Bits = $bits;
678
679 fn is_nan(self) -> bool {
683 Self::is_nan(self)
684 }
685
686 fn is_infinite(self) -> bool {
687 Self::is_infinite(self)
688 }
689
690 fn is_finite(self) -> bool {
691 Self::is_finite(self)
692 }
693
694 fn is_subnormal(self) -> bool {
695 Self::is_subnormal(self)
696 }
697
698 fn is_normal(self) -> bool {
699 Self::is_normal(self)
700 }
701
702 fn classify(self) -> FpCategory {
703 Self::classify(self)
704 }
705
706 fn is_sign_positive(self) -> bool {
707 Self::is_sign_positive(self)
708 }
709
710 fn is_sign_negative(self) -> bool {
711 Self::is_sign_negative(self)
712 }
713
714 fn next_up(self) -> Self {
715 Self::next_up(self)
716 }
717
718 fn next_down(self) -> Self {
719 Self::next_down(self)
720 }
721
722 fn recip(self) -> Self {
723 Self::recip(self)
724 }
725
726 fn to_degrees(self) -> Self {
727 Self::to_degrees(self)
728 }
729
730 fn to_radians(self) -> Self {
731 Self::to_radians(self)
732 }
733
734 fn max(self, other: Self) -> Self {
735 Self::max(self, other)
736 }
737
738 fn min(self, other: Self) -> Self {
739 Self::min(self, other)
740 }
741
742 fn midpoint(self, other: Self) -> Self {
743 Self::midpoint(self, other)
744 }
745
746 fn to_bits(self) -> Self::Bits {
747 Self::to_bits(self)
748 }
749
750 fn from_bits(v: Self::Bits) -> Self {
751 Self::from_bits(v)
752 }
753
754 fn total_cmp(&self, other: &Self) -> core::cmp::Ordering {
755 Self::total_cmp(self, other)
756 }
757
758 fn clamp(self, min: Self, max: Self) -> Self {
759 Self::clamp(self, min, max)
760 }
761
762 fn copysign(self, sign: Self) -> Self {
763 Self::copysign(self, sign)
764 }
765
766 fn algebraic_add(self, rhs: Self) -> Self {
767 Self::algebraic_add(self, rhs)
768 }
769
770 fn algebraic_sub(self, rhs: Self) -> Self {
771 Self::algebraic_sub(self, rhs)
772 }
773
774 fn algebraic_mul(self, rhs: Self) -> Self {
775 Self::algebraic_mul(self, rhs)
776 }
777
778 fn algebraic_div(self, rhs: Self) -> Self {
779 Self::algebraic_div(self, rhs)
780 }
781
782 fn algebraic_rem(self, rhs: Self) -> Self {
783 Self::algebraic_rem(self, rhs)
784 }
785
786 #[cfg(feature = "std")]
787 fn floor(self) -> Self {
788 Self::floor(self)
789 }
790
791 #[cfg(feature = "std")]
792 fn ceil(self) -> Self {
793 Self::ceil(self)
794 }
795
796 #[cfg(feature = "std")]
797 fn round(self) -> Self {
798 Self::round(self)
799 }
800
801 #[cfg(feature = "std")]
802 fn round_ties_even(self) -> Self {
803 Self::round_ties_even(self)
804 }
805
806 #[cfg(feature = "std")]
807 fn trunc(self) -> Self {
808 Self::trunc(self)
809 }
810
811 #[cfg(feature = "std")]
812 fn fract(self) -> Self {
813 Self::fract(self)
814 }
815
816 #[cfg(feature = "std")]
817 fn mul_add(self, a: Self, b: Self) -> Self {
818 Self::mul_add(self, a, b)
819 }
820
821 #[cfg(feature = "std")]
822 fn powi(self, n: i32) -> Self {
823 Self::powi(self, n)
824 }
825
826 #[cfg(feature = "std")]
827 fn powf(self, n: Self) -> Self {
828 Self::powf(self, n)
829 }
830
831 #[cfg(feature = "std")]
832 fn sqrt(self) -> Self {
833 Self::sqrt(self)
834 }
835
836 #[cfg(feature = "std")]
837 fn exp(self) -> Self {
838 Self::exp(self)
839 }
840
841 #[cfg(feature = "std")]
842 fn exp2(self) -> Self {
843 Self::exp2(self)
844 }
845
846 #[cfg(feature = "std")]
847 fn ln(self) -> Self {
848 Self::ln(self)
849 }
850
851 #[cfg(feature = "std")]
852 fn log(self, base: Self) -> Self {
853 Self::log(self, base)
854 }
855
856 #[cfg(feature = "std")]
857 fn log2(self) -> Self {
858 Self::log2(self)
859 }
860
861 #[cfg(feature = "std")]
862 fn log10(self) -> Self {
863 Self::log10(self)
864 }
865
866 #[cfg(feature = "std")]
867 fn cbrt(self) -> Self {
868 Self::cbrt(self)
869 }
870
871 #[cfg(feature = "std")]
872 fn hypot(self, other: Self) -> Self {
873 Self::hypot(self, other)
874 }
875
876 #[cfg(feature = "std")]
877 fn sin(self) -> Self {
878 Self::sin(self)
879 }
880
881 #[cfg(feature = "std")]
882 fn cos(self) -> Self {
883 Self::cos(self)
884 }
885
886 #[cfg(feature = "std")]
887 fn tan(self) -> Self {
888 Self::tan(self)
889 }
890
891 #[cfg(feature = "std")]
892 fn asin(self) -> Self {
893 Self::asin(self)
894 }
895
896 #[cfg(feature = "std")]
897 fn acos(self) -> Self {
898 Self::acos(self)
899 }
900
901 #[cfg(feature = "std")]
902 fn atan(self) -> Self {
903 Self::atan(self)
904 }
905
906 #[cfg(feature = "std")]
907 fn atan2(self, other: Self) -> Self {
908 Self::atan2(self, other)
909 }
910
911 #[cfg(feature = "std")]
912 fn sin_cos(self) -> (Self, Self) {
913 Self::sin_cos(self)
914 }
915
916 #[cfg(feature = "std")]
917 fn exp_m1(self) -> Self {
918 Self::exp_m1(self)
919 }
920
921 #[cfg(feature = "std")]
922 fn ln_1p(self) -> Self {
923 Self::ln_1p(self)
924 }
925
926 #[cfg(feature = "std")]
927 fn sinh(self) -> Self {
928 Self::sinh(self)
929 }
930
931 #[cfg(feature = "std")]
932 fn cosh(self) -> Self {
933 Self::cosh(self)
934 }
935
936 #[cfg(feature = "std")]
937 fn tanh(self) -> Self {
938 Self::tanh(self)
939 }
940
941 #[cfg(feature = "std")]
942 fn asinh(self) -> Self {
943 Self::asinh(self)
944 }
945
946 #[cfg(feature = "std")]
947 fn acosh(self) -> Self {
948 Self::acosh(self)
949 }
950
951 #[cfg(feature = "std")]
952 fn atanh(self) -> Self {
953 Self::atanh(self)
954 }
955
956 }
958 };
959}
960
961impl_float!(f32, u32, 1e-45);
962impl_float!(f64, u64, 5e-324);
963
964pub trait Integer:
965 Number
966 + Ord
967 + Eq
968 + Not<Output = Self>
969 + BitAnd<Self, Output = Self>
970 + for<'a> BitAnd<&'a Self, Output = Self>
971 + BitAndAssign<Self>
972 + for<'a> BitAndAssign<&'a Self>
973 + BitOr<Self, Output = Self>
974 + for<'a> BitOr<&'a Self, Output = Self>
975 + BitOrAssign<Self>
976 + for<'a> BitOrAssign<&'a Self>
977 + BitXor<Self, Output = Self>
978 + for<'a> BitXor<&'a Self, Output = Self>
979 + BitXorAssign<Self>
980 + for<'a> BitXorAssign<&'a Self>
981 + Shl<Self, Output = Self>
982 + for<'a> Shl<&'a Self, Output = Self>
983 + ShlAssign<Self>
984 + for<'a> ShlAssign<&'a Self>
985 + Shr<Self, Output = Self>
986 + for<'a> Shr<&'a Self, Output = Self>
987 + ShrAssign<Self>
988 + for<'a> ShrAssign<&'a Self>
989 + TryFrom<u32>
990 + TryFrom<u64>
991 + TryFrom<u128>
992 + TryFrom<usize>
993 + TryFrom<i32>
994 + TryFrom<i64>
995 + TryFrom<i128>
996 + TryFrom<isize>
997 + TryInto<u8>
998 + TryInto<u16>
999 + TryInto<u32>
1000 + TryInto<u64>
1001 + TryInto<u128>
1002 + TryInto<usize>
1003 + TryInto<i8>
1004 + TryInto<i16>
1005 + TryInto<i32>
1006 + TryInto<i64>
1007 + TryInto<i128>
1008 + TryInto<isize>
1009 + Hash
1010 + Binary
1011 + Octal
1012 + LowerHex
1013 + UpperHex
1014{
1015 type Unsigned: Unsigned;
1016 type Signed: Signed;
1017
1018 #[must_use]
1019 fn from_unsigned(v: Self::Unsigned) -> Self;
1020 #[must_use]
1021 fn from_signed(v: Self::Signed) -> Self;
1022 #[must_use]
1023 fn to_unsigned(self) -> Self::Unsigned;
1024 #[must_use]
1025 fn to_signed(self) -> Self::Signed;
1026
1027 #[must_use]
1028 fn cast_ptr<T: Sized, P: Pointer<T>>(self) -> P;
1029
1030 #[cfg(not(feature = "std"))]
1032 #[must_use = "this returns the result of the operation, without modifying the original"]
1033 fn div_euclid(self, rhs: Self) -> Self;
1034
1035 #[cfg(not(feature = "std"))]
1037 #[must_use = "this returns the result of the operation, without modifying the original"]
1038 fn rem_euclid(self, rhs: Self) -> Self;
1039
1040 fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>;
1045
1046 #[must_use = "this returns the result of the operation, without modifying the original"]
1048 fn count_ones(self) -> u32;
1049
1050 #[must_use = "this returns the result of the operation, without modifying the original"]
1052 fn count_zeros(self) -> u32;
1053
1054 #[must_use = "this returns the result of the operation, without modifying the original"]
1056 fn leading_zeros(self) -> u32;
1057
1058 #[must_use = "this returns the result of the operation, without modifying the original"]
1060 fn trailing_zeros(self) -> u32;
1061
1062 #[must_use = "this returns the result of the operation, without modifying the original"]
1064 fn leading_ones(self) -> u32;
1065
1066 #[must_use = "this returns the result of the operation, without modifying the original"]
1068 fn trailing_ones(self) -> u32;
1069
1070 #[must_use = "this returns the result of the operation, without modifying the original"]
1072 fn isolate_highest_one(self) -> Self;
1073
1074 #[must_use = "this returns the result of the operation, without modifying the original"]
1076 fn isolate_lowest_one(self) -> Self;
1077
1078 #[must_use = "this returns the result of the operation, without modifying the original"]
1080 fn highest_one(self) -> Option<u32>;
1081
1082 #[must_use = "this returns the result of the operation, without modifying the original"]
1084 fn lowest_one(self) -> Option<u32>;
1085
1086 #[must_use = "this returns the result of the operation, without modifying the original"]
1088 fn rotate_left(self, n: u32) -> Self;
1089
1090 #[must_use = "this returns the result of the operation, without modifying the original"]
1092 fn rotate_right(self, n: u32) -> Self;
1093
1094 #[must_use = "this returns the result of the operation, without modifying the original"]
1096 fn swap_bytes(self) -> Self;
1097
1098 #[must_use = "this returns the result of the operation, without modifying the original"]
1100 fn reverse_bits(self) -> Self;
1101
1102 #[must_use]
1104 fn from_be(x: Self) -> Self;
1105
1106 #[must_use]
1108 fn from_le(x: Self) -> Self;
1109
1110 #[must_use = "this returns the result of the operation, without modifying the original"]
1112 fn to_be(self) -> Self;
1113
1114 #[must_use = "this returns the result of the operation, without modifying the original"]
1116 fn to_le(self) -> Self;
1117
1118 #[must_use = "this returns the result of the operation, without modifying the original"]
1120 fn checked_add(self, rhs: Self) -> Option<Self>;
1121
1122 #[must_use = "this returns the result of the operation, without modifying the original"]
1124 fn strict_add(self, rhs: Self) -> Self;
1125
1126 #[must_use = "this returns the result of the operation, without modifying the original"]
1132 unsafe fn unchecked_add(self, rhs: Self) -> Self;
1133
1134 #[must_use = "this returns the result of the operation, without modifying the original"]
1136 fn checked_sub(self, rhs: Self) -> Option<Self>;
1137
1138 #[must_use = "this returns the result of the operation, without modifying the original"]
1140 fn strict_sub(self, rhs: Self) -> Self;
1141
1142 #[must_use = "this returns the result of the operation, without modifying the original"]
1148 unsafe fn unchecked_sub(self, rhs: Self) -> Self;
1149
1150 #[must_use = "this returns the result of the operation, without modifying the original"]
1152 fn checked_mul(self, rhs: Self) -> Option<Self>;
1153
1154 #[must_use = "this returns the result of the operation, without modifying the original"]
1156 fn strict_mul(self, rhs: Self) -> Self;
1157
1158 #[must_use = "this returns the result of the operation, without modifying the original"]
1164 unsafe fn unchecked_mul(self, rhs: Self) -> Self;
1165
1166 #[must_use = "this returns the result of the operation, without modifying the original"]
1168 fn checked_div(self, rhs: Self) -> Option<Self>;
1169
1170 #[must_use = "this returns the result of the operation, without modifying the original"]
1172 fn strict_div(self, rhs: Self) -> Self;
1173
1174 #[must_use = "this returns the result of the operation, without modifying the original"]
1176 fn checked_div_euclid(self, rhs: Self) -> Option<Self>;
1177
1178 #[must_use = "this returns the result of the operation, without modifying the original"]
1180 fn strict_div_euclid(self, rhs: Self) -> Self;
1181
1182 #[must_use = "this returns the result of the operation, without modifying the original"]
1184 fn checked_rem(self, rhs: Self) -> Option<Self>;
1185
1186 #[must_use = "this returns the result of the operation, without modifying the original"]
1188 fn strict_rem(self, rhs: Self) -> Self;
1189
1190 #[must_use = "this returns the result of the operation, without modifying the original"]
1192 fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;
1193
1194 #[must_use = "this returns the result of the operation, without modifying the original"]
1196 fn strict_rem_euclid(self, rhs: Self) -> Self;
1197
1198 #[must_use = "this returns the result of the operation, without modifying the original"]
1200 fn checked_neg(self) -> Option<Self>;
1201
1202 #[must_use = "this returns the result of the operation, without modifying the original"]
1204 fn strict_neg(self) -> Self;
1205
1206 #[must_use = "this returns the result of the operation, without modifying the original"]
1208 fn checked_shl(self, rhs: u32) -> Option<Self>;
1209
1210 #[must_use = "this returns the result of the operation, without modifying the original"]
1212 fn strict_shl(self, rhs: u32) -> Self;
1213
1214 #[must_use = "this returns the result of the operation, without modifying the original"]
1220 unsafe fn unchecked_shl(self, rhs: u32) -> Self;
1221
1222 #[must_use = "this returns the result of the operation, without modifying the original"]
1224 fn unbounded_shl(self, rhs: u32) -> Self;
1225
1226 #[must_use = "this returns the result of the operation, without modifying the original"]
1228 fn checked_shr(self, rhs: u32) -> Option<Self>;
1229
1230 #[must_use = "this returns the result of the operation, without modifying the original"]
1232 fn strict_shr(self, rhs: u32) -> Self;
1233
1234 #[must_use = "this returns the result of the operation, without modifying the original"]
1240 unsafe fn unchecked_shr(self, rhs: u32) -> Self;
1241
1242 #[must_use = "this returns the result of the operation, without modifying the original"]
1244 fn unbounded_shr(self, rhs: u32) -> Self;
1245
1246 #[must_use = "this returns the result of the operation, without modifying the original"]
1248 fn checked_pow(self, exp: u32) -> Option<Self>;
1249
1250 #[must_use = "this returns the result of the operation, without modifying the original"]
1252 fn strict_pow(self, exp: u32) -> Self;
1253
1254 #[must_use = "this returns the result of the operation, without modifying the original"]
1256 fn saturating_add(self, rhs: Self) -> Self;
1257
1258 #[must_use = "this returns the result of the operation, without modifying the original"]
1260 fn saturating_sub(self, rhs: Self) -> Self;
1261
1262 #[must_use = "this returns the result of the operation, without modifying the original"]
1264 fn saturating_mul(self, rhs: Self) -> Self;
1265
1266 #[must_use = "this returns the result of the operation, without modifying the original"]
1268 fn saturating_div(self, rhs: Self) -> Self;
1269
1270 #[must_use = "this returns the result of the operation, without modifying the original"]
1272 fn saturating_pow(self, exp: u32) -> Self;
1273
1274 #[must_use = "this returns the result of the operation, without modifying the original"]
1276 fn wrapping_add(self, rhs: Self) -> Self;
1277
1278 #[must_use = "this returns the result of the operation, without modifying the original"]
1280 fn wrapping_sub(self, rhs: Self) -> Self;
1281
1282 #[must_use = "this returns the result of the operation, without modifying the original"]
1284 fn wrapping_mul(self, rhs: Self) -> Self;
1285
1286 #[must_use = "this returns the result of the operation, without modifying the original"]
1288 fn wrapping_div(self, rhs: Self) -> Self;
1289
1290 #[must_use = "this returns the result of the operation, without modifying the original"]
1292 fn wrapping_div_euclid(self, rhs: Self) -> Self;
1293
1294 #[must_use = "this returns the result of the operation, without modifying the original"]
1296 fn wrapping_rem(self, rhs: Self) -> Self;
1297
1298 #[must_use = "this returns the result of the operation, without modifying the original"]
1300 fn wrapping_rem_euclid(self, rhs: Self) -> Self;
1301
1302 #[must_use = "this returns the result of the operation, without modifying the original"]
1304 fn wrapping_neg(self) -> Self;
1305
1306 #[must_use = "this returns the result of the operation, without modifying the original"]
1308 fn wrapping_shl(self, rhs: u32) -> Self;
1309
1310 #[must_use = "this returns the result of the operation, without modifying the original"]
1312 fn wrapping_shr(self, rhs: u32) -> Self;
1313
1314 #[must_use = "this returns the result of the operation, without modifying the original"]
1316 fn wrapping_pow(self, exp: u32) -> Self;
1317
1318 #[must_use = "this returns the result of the operation, without modifying the original"]
1320 fn overflowing_add(self, rhs: Self) -> (Self, bool);
1321
1322 #[must_use = "this returns the result of the operation, without modifying the original"]
1324 fn overflowing_sub(self, rhs: Self) -> (Self, bool);
1325
1326 #[must_use = "this returns the result of the operation, without modifying the original"]
1328 fn overflowing_mul(self, rhs: Self) -> (Self, bool);
1329
1330 #[must_use = "this returns the result of the operation, without modifying the original"]
1332 fn overflowing_div(self, rhs: Self) -> (Self, bool);
1333
1334 #[must_use = "this returns the result of the operation, without modifying the original"]
1336 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);
1337
1338 #[must_use = "this returns the result of the operation, without modifying the original"]
1340 fn overflowing_rem(self, rhs: Self) -> (Self, bool);
1341
1342 #[must_use = "this returns the result of the operation, without modifying the original"]
1344 fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool);
1345
1346 #[must_use = "this returns the result of the operation, without modifying the original"]
1348 fn overflowing_neg(self) -> (Self, bool);
1349
1350 #[must_use = "this returns the result of the operation, without modifying the original"]
1352 fn overflowing_shl(self, rhs: u32) -> (Self, bool);
1353
1354 #[must_use = "this returns the result of the operation, without modifying the original"]
1356 fn overflowing_shr(self, rhs: u32) -> (Self, bool);
1357
1358 #[must_use = "this returns the result of the operation, without modifying the original"]
1360 fn overflowing_pow(self, exp: u32) -> (Self, bool);
1361
1362 #[must_use = "this returns the result of the operation, without modifying the original"]
1364 fn pow(self, exp: u32) -> Self;
1365
1366 #[must_use = "this returns the result of the operation, without modifying the original"]
1368 fn isqrt(self) -> Self;
1369
1370 #[must_use = "this returns the result of the operation, without modifying the original"]
1372 fn ilog(self, base: Self) -> u32;
1373
1374 #[must_use = "this returns the result of the operation, without modifying the original"]
1376 fn ilog2(self) -> u32;
1377
1378 #[must_use = "this returns the result of the operation, without modifying the original"]
1380 fn ilog10(self) -> u32;
1381
1382 #[must_use = "this returns the result of the operation, without modifying the original"]
1384 fn checked_ilog(self, base: Self) -> Option<u32>;
1385
1386 #[must_use = "this returns the result of the operation, without modifying the original"]
1388 fn checked_ilog2(self) -> Option<u32>;
1389
1390 #[must_use = "this returns the result of the operation, without modifying the original"]
1392 fn checked_ilog10(self) -> Option<u32>;
1393
1394 #[must_use = "this returns the result of the operation, without modifying the original"]
1396 fn abs_diff(self, other: Self) -> Self::Unsigned;
1397
1398 #[must_use = "this returns the result of the operation, without modifying the original"]
1400 fn midpoint(self, rhs: Self) -> Self;
1401
1402 }
1404
1405macro_rules! impl_integer {
1406 ($ty:ty, $unsigned:ty, $signed:ty, abs: $abs:expr, signum: $signum:expr) => {
1407 impl_number!(
1408 $ty,
1409 zero: 0,
1410 one: 1,
1411 min: Self::MIN,
1412 max: Self::MAX,
1413 abs: $abs,
1414 signum: $signum
1415 );
1416 impl Integer for $ty {
1417 type Unsigned = $unsigned;
1418 type Signed = $signed;
1419
1420 fn from_unsigned(v: Self::Unsigned) -> Self {
1421 #[allow(clippy::useless_transmute)]
1422 #[allow(unnecessary_transmutes)]
1423 unsafe { transmute::<Self::Unsigned, Self>(v) }
1424 }
1425
1426 fn from_signed(v: Self::Signed) -> Self {
1427 #[allow(clippy::useless_transmute)]
1428 #[allow(unnecessary_transmutes)]
1429 unsafe { transmute::<Self::Signed, Self>(v) }
1430 }
1431
1432 fn to_unsigned(self) -> Self::Unsigned {
1433 #[allow(clippy::useless_transmute)]
1434 #[allow(unnecessary_transmutes)]
1435 unsafe { transmute::<Self, Self::Unsigned>(self) }
1436 }
1437
1438 fn to_signed(self) -> Self::Signed {
1439 #[allow(clippy::useless_transmute)]
1440 #[allow(unnecessary_transmutes)]
1441 unsafe { transmute::<Self, Self::Signed>(self) }
1442 }
1443
1444 fn cast_ptr<T: Sized, P: Pointer<T>>(self) -> P {
1445 cast_ptr!(T, P, self)
1446 }
1447
1448 #[cfg(not(feature = "std"))]
1449 fn div_euclid(self, rhs: Self) -> Self {
1450 Self::div_euclid(self, rhs)
1451 }
1452
1453 #[cfg(not(feature = "std"))]
1454 fn rem_euclid(self, rhs: Self) -> Self {
1455 Self::rem_euclid(self, rhs)
1456 }
1457
1458 fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError> {
1462 Self::from_str_radix(src, radix)
1463 }
1464
1465 fn count_ones(self) -> u32 {
1466 Self::count_ones(self)
1467 }
1468
1469 fn count_zeros(self) -> u32 {
1470 Self::count_zeros(self)
1471 }
1472
1473 fn leading_zeros(self) -> u32 {
1474 Self::leading_zeros(self)
1475 }
1476
1477 fn trailing_zeros(self) -> u32 {
1478 Self::trailing_zeros(self)
1479 }
1480
1481 fn leading_ones(self) -> u32 {
1482 Self::leading_ones(self)
1483 }
1484
1485 fn trailing_ones(self) -> u32 {
1486 Self::trailing_ones(self)
1487 }
1488
1489 fn isolate_highest_one(self) -> Self {
1490 Self::isolate_highest_one(self)
1491 }
1492
1493 fn isolate_lowest_one(self) -> Self {
1494 Self::isolate_lowest_one(self)
1495 }
1496
1497 fn highest_one(self) -> Option<u32> {
1498 Self::highest_one(self)
1499 }
1500
1501 fn lowest_one(self) -> Option<u32> {
1502 Self::lowest_one(self)
1503 }
1504
1505 fn rotate_left(self, n: u32) -> Self {
1506 Self::rotate_left(self, n)
1507 }
1508
1509 fn rotate_right(self, n: u32) -> Self {
1510 Self::rotate_right(self, n)
1511 }
1512
1513 fn swap_bytes(self) -> Self {
1514 Self::swap_bytes(self)
1515 }
1516
1517 fn reverse_bits(self) -> Self {
1518 Self::reverse_bits(self)
1519 }
1520
1521 fn from_be(x: Self) -> Self {
1522 Self::from_be(x)
1523 }
1524
1525 fn from_le(x: Self) -> Self {
1526 Self::from_le(x)
1527 }
1528
1529 fn to_be(self) -> Self {
1530 Self::to_be(self)
1531 }
1532
1533 fn to_le(self) -> Self {
1534 Self::to_le(self)
1535 }
1536
1537 fn checked_add(self, rhs: Self) -> Option<Self> {
1538 Self::checked_add(self, rhs)
1539 }
1540
1541 fn strict_add(self, rhs: Self) -> Self {
1542 Self::strict_add(self, rhs)
1543 }
1544
1545 unsafe fn unchecked_add(self, rhs: Self) -> Self {
1546 unsafe { Self::unchecked_add(self, rhs) }
1547 }
1548
1549 fn checked_sub(self, rhs: Self) -> Option<Self> {
1550 Self::checked_sub(self, rhs)
1551 }
1552
1553 fn strict_sub(self, rhs: Self) -> Self {
1554 Self::strict_sub(self, rhs)
1555 }
1556
1557 unsafe fn unchecked_sub(self, rhs: Self) -> Self {
1558 unsafe { Self::unchecked_sub(self, rhs) }
1559 }
1560
1561 fn checked_mul(self, rhs: Self) -> Option<Self> {
1562 Self::checked_mul(self, rhs)
1563 }
1564
1565 fn strict_mul(self, rhs: Self) -> Self {
1566 Self::strict_mul(self, rhs)
1567 }
1568
1569 unsafe fn unchecked_mul(self, rhs: Self) -> Self {
1570 unsafe { Self::unchecked_mul(self, rhs) }
1571 }
1572
1573 fn checked_div(self, rhs: Self) -> Option<Self> {
1574 Self::checked_div(self, rhs)
1575 }
1576
1577 fn strict_div(self, rhs: Self) -> Self {
1578 Self::strict_div(self, rhs)
1579 }
1580
1581 fn checked_div_euclid(self, rhs: Self) -> Option<Self> {
1582 Self::checked_div_euclid(self, rhs)
1583 }
1584
1585 fn strict_div_euclid(self, rhs: Self) -> Self {
1586 Self::strict_div_euclid(self, rhs)
1587 }
1588
1589 fn checked_rem(self, rhs: Self) -> Option<Self> {
1590 Self::checked_rem(self, rhs)
1591 }
1592
1593 fn strict_rem(self, rhs: Self) -> Self {
1594 Self::strict_rem(self, rhs)
1595 }
1596
1597 fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
1598 Self::checked_rem_euclid(self, rhs)
1599 }
1600
1601 fn strict_rem_euclid(self, rhs: Self) -> Self {
1602 Self::strict_rem_euclid(self, rhs)
1603 }
1604
1605 fn checked_neg(self) -> Option<Self> {
1606 Self::checked_neg(self)
1607 }
1608
1609 fn strict_neg(self) -> Self {
1610 Self::strict_neg(self)
1611 }
1612
1613 fn checked_shl(self, rhs: u32) -> Option<Self> {
1614 Self::checked_shl(self, rhs)
1615 }
1616
1617 fn strict_shl(self, rhs: u32) -> Self {
1618 Self::strict_shl(self, rhs)
1619 }
1620
1621 unsafe fn unchecked_shl(self, rhs: u32) -> Self {
1622 unsafe { Self::unchecked_shl(self, rhs) }
1623 }
1624
1625 fn unbounded_shl(self, rhs: u32) -> Self {
1626 Self::unbounded_shl(self, rhs)
1627 }
1628
1629 fn checked_shr(self, rhs: u32) -> Option<Self> {
1630 Self::checked_shr(self, rhs)
1631 }
1632
1633 fn strict_shr(self, rhs: u32) -> Self {
1634 Self::strict_shr(self, rhs)
1635 }
1636
1637 unsafe fn unchecked_shr(self, rhs: u32) -> Self {
1638 unsafe { Self::unchecked_shr(self, rhs) }
1639 }
1640
1641 fn unbounded_shr(self, rhs: u32) -> Self {
1642 Self::unbounded_shr(self, rhs)
1643 }
1644
1645 fn checked_pow(self, exp: u32) -> Option<Self> {
1646 Self::checked_pow(self, exp)
1647 }
1648
1649 fn strict_pow(self, exp: u32) -> Self {
1650 Self::strict_pow(self, exp)
1651 }
1652
1653 fn saturating_add(self, rhs: Self) -> Self {
1654 Self::saturating_add(self, rhs)
1655 }
1656
1657 fn saturating_sub(self, rhs: Self) -> Self {
1658 Self::saturating_sub(self, rhs)
1659 }
1660
1661 fn saturating_mul(self, rhs: Self) -> Self {
1662 Self::saturating_mul(self, rhs)
1663 }
1664
1665 fn saturating_div(self, rhs: Self) -> Self {
1666 Self::saturating_div(self, rhs)
1667 }
1668
1669 fn saturating_pow(self, exp: u32) -> Self {
1670 Self::saturating_pow(self, exp)
1671 }
1672
1673 fn wrapping_add(self, rhs: Self) -> Self {
1674 Self::wrapping_add(self, rhs)
1675 }
1676
1677 fn wrapping_sub(self, rhs: Self) -> Self {
1678 Self::wrapping_sub(self, rhs)
1679 }
1680
1681 fn wrapping_mul(self, rhs: Self) -> Self {
1682 Self::wrapping_mul(self, rhs)
1683 }
1684
1685 fn wrapping_div(self, rhs: Self) -> Self {
1686 Self::wrapping_div(self, rhs)
1687 }
1688
1689 fn wrapping_div_euclid(self, rhs: Self) -> Self {
1690 Self::wrapping_div_euclid(self, rhs)
1691 }
1692
1693 fn wrapping_rem(self, rhs: Self) -> Self {
1694 Self::wrapping_rem(self, rhs)
1695 }
1696
1697 fn wrapping_rem_euclid(self, rhs: Self) -> Self {
1698 Self::wrapping_rem_euclid(self, rhs)
1699 }
1700
1701 fn wrapping_neg(self) -> Self {
1702 Self::wrapping_neg(self)
1703 }
1704
1705 fn wrapping_shl(self, rhs: u32) -> Self {
1706 Self::wrapping_shl(self, rhs)
1707 }
1708
1709 fn wrapping_shr(self, rhs: u32) -> Self {
1710 Self::wrapping_shr(self, rhs)
1711 }
1712
1713 fn wrapping_pow(self, exp: u32) -> Self {
1714 Self::wrapping_pow(self, exp)
1715 }
1716
1717 fn overflowing_add(self, rhs: Self) -> (Self, bool) {
1718 Self::overflowing_add(self, rhs)
1719 }
1720
1721 fn overflowing_sub(self, rhs: Self) -> (Self, bool) {
1722 Self::overflowing_sub(self, rhs)
1723 }
1724
1725 fn overflowing_mul(self, rhs: Self) -> (Self, bool) {
1726 Self::overflowing_mul(self, rhs)
1727 }
1728
1729 fn overflowing_div(self, rhs: Self) -> (Self, bool) {
1730 Self::overflowing_div(self, rhs)
1731 }
1732
1733 fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool) {
1734 Self::overflowing_div_euclid(self, rhs)
1735 }
1736
1737 fn overflowing_rem(self, rhs: Self) -> (Self, bool) {
1738 Self::overflowing_rem(self, rhs)
1739 }
1740
1741 fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool) {
1742 Self::overflowing_rem_euclid(self, rhs)
1743 }
1744
1745 fn overflowing_neg(self) -> (Self, bool) {
1746 Self::overflowing_neg(self)
1747 }
1748
1749 fn overflowing_shl(self, rhs: u32) -> (Self, bool) {
1750 Self::overflowing_shl(self, rhs)
1751 }
1752
1753 fn overflowing_shr(self, rhs: u32) -> (Self, bool) {
1754 Self::overflowing_shr(self, rhs)
1755 }
1756
1757 fn overflowing_pow(self, exp: u32) -> (Self, bool) {
1758 Self::overflowing_pow(self, exp)
1759 }
1760
1761 fn pow(self, exp: u32) -> Self {
1762 Self::pow(self, exp)
1763 }
1764
1765 fn isqrt(self) -> Self {
1766 Self::isqrt(self)
1767 }
1768
1769 fn ilog(self, base: Self) -> u32 {
1770 Self::ilog(self, base)
1771 }
1772
1773 fn ilog2(self) -> u32 {
1774 Self::ilog2(self)
1775 }
1776
1777 fn ilog10(self) -> u32 {
1778 Self::ilog10(self)
1779 }
1780
1781 fn checked_ilog(self, base: Self) -> Option<u32> {
1782 Self::checked_ilog(self, base)
1783 }
1784
1785 fn checked_ilog2(self) -> Option<u32> {
1786 Self::checked_ilog2(self)
1787 }
1788
1789 fn checked_ilog10(self) -> Option<u32> {
1790 Self::checked_ilog10(self)
1791 }
1792
1793 fn abs_diff(self, other: Self) -> Self::Unsigned {
1794 Self::abs_diff(self, other)
1795 }
1796
1797 fn midpoint(self, rhs: Self) -> Self {
1798 Self::midpoint(self, rhs)
1799 }
1800
1801 }
1803 };
1804}
1805
1806pub trait Unsigned: Integer<Unsigned = Self> + From<u8> {
1807 #[must_use = "this returns the result of the operation, without modifying the original"]
1812 fn bit_width(self) -> u32;
1813
1814 #[must_use = "this returns the result of the operation, without modifying the original"]
1816 fn cast_signed(self) -> Self::Signed;
1817
1818 #[must_use = "this returns the result of the operation, without modifying the original"]
1820 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
1821
1822 #[must_use = "this returns the result of the operation, without modifying the original"]
1824 fn strict_add_signed(self, rhs: Self::Signed) -> Self;
1825
1826 #[must_use = "this returns the result of the operation, without modifying the original"]
1828 fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self>;
1829
1830 #[must_use = "this returns the result of the operation, without modifying the original"]
1832 fn strict_sub_signed(self, rhs: Self::Signed) -> Self;
1833
1834 fn checked_signed_diff(self, rhs: Self) -> Option<Self::Signed>;
1836
1837 #[must_use = "this returns the result of the operation, without modifying the original"]
1839 fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
1840
1841 #[must_use = "this returns the result of the operation, without modifying the original"]
1843 fn saturating_sub_signed(self, rhs: Self::Signed) -> Self;
1844
1845 #[must_use = "this returns the result of the operation, without modifying the original"]
1847 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
1848
1849 #[must_use = "this returns the result of the operation, without modifying the original"]
1851 fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self;
1852
1853 #[must_use = "this returns the result of the operation, without modifying the original"]
1855 fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool);
1856
1857 #[must_use = "this returns the result of the operation, without modifying the original"]
1859 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
1860
1861 #[must_use = "this returns the result of the operation, without modifying the original"]
1863 fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool);
1864
1865 #[must_use = "this returns the result of the operation, without modifying the original"]
1867 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool);
1868
1869 #[must_use = "this returns the result of the operation, without modifying the original"]
1871 fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self);
1872
1873 #[must_use = "this returns the result of the operation, without modifying the original"]
1875 fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> (Self, Self);
1876
1877 #[must_use = "this returns the result of the operation, without modifying the original"]
1879 fn div_ceil(self, rhs: Self) -> Self;
1880
1881 #[must_use = "this returns the result of the operation, without modifying the original"]
1883 fn next_multiple_of(self, rhs: Self) -> Self;
1884
1885 #[must_use = "this returns the result of the operation, without modifying the original"]
1887 fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>;
1888
1889 #[must_use]
1891 fn is_multiple_of(self, rhs: Self) -> bool;
1892
1893 #[must_use]
1895 fn is_power_of_two(self) -> bool;
1896
1897 #[must_use = "this returns the result of the operation, without modifying the original"]
1899 fn next_power_of_two(self) -> Self;
1900
1901 #[must_use = "this returns the result of the operation, without modifying the original"]
1903 fn checked_next_power_of_two(self) -> Option<Self>;
1904
1905 }
1907
1908macro_rules! impl_unsigned {
1909 ($ty:ty, $signed:ty) => {
1910 impl_integer!(
1911 $ty,
1912 Self,
1913 $signed,
1914 abs: |v| v,
1915 signum: |v| (v > 0) as Self
1916 );
1917 impl Unsigned for $ty {
1918 fn bit_width(self) -> u32 {
1922 Self::bit_width(self)
1923 }
1924
1925 fn cast_signed(self) -> Self::Signed {
1926 Self::cast_signed(self)
1927 }
1928
1929 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self> {
1930 Self::checked_add_signed(self, rhs)
1931 }
1932
1933 fn strict_add_signed(self, rhs: Self::Signed) -> Self {
1934 Self::strict_add_signed(self, rhs)
1935 }
1936
1937 fn checked_sub_signed(self, rhs: Self::Signed) -> Option<Self> {
1938 Self::checked_sub_signed(self, rhs)
1939 }
1940
1941 fn strict_sub_signed(self, rhs: Self::Signed) -> Self {
1942 Self::strict_sub_signed(self, rhs)
1943 }
1944
1945 fn checked_signed_diff(self, rhs: Self) -> Option<Self::Signed> {
1946 Self::checked_signed_diff(self, rhs)
1947 }
1948
1949 fn saturating_add_signed(self, rhs: Self::Signed) -> Self {
1950 Self::saturating_add_signed(self, rhs)
1951 }
1952
1953 fn saturating_sub_signed(self, rhs: Self::Signed) -> Self {
1954 Self::saturating_sub_signed(self, rhs)
1955 }
1956
1957 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self {
1958 Self::wrapping_add_signed(self, rhs)
1959 }
1960
1961 fn wrapping_sub_signed(self, rhs: Self::Signed) -> Self {
1962 Self::wrapping_sub_signed(self, rhs)
1963 }
1964
1965 fn carrying_add(self, rhs: Self, carry: bool) -> (Self, bool) {
1966 Self::carrying_add(self, rhs, carry)
1967 }
1968
1969 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool) {
1970 Self::overflowing_add_signed(self, rhs)
1971 }
1972
1973 fn borrowing_sub(self, rhs: Self, borrow: bool) -> (Self, bool) {
1974 Self::borrowing_sub(self, rhs, borrow)
1975 }
1976
1977 fn overflowing_sub_signed(self, rhs: Self::Signed) -> (Self, bool) {
1978 Self::overflowing_sub_signed(self, rhs)
1979 }
1980
1981 fn carrying_mul(self, rhs: Self, carry: Self) -> (Self, Self) {
1982 Self::carrying_mul(self, rhs, carry)
1983 }
1984
1985 fn carrying_mul_add(self, rhs: Self, carry: Self, add: Self) -> (Self, Self) {
1986 Self::carrying_mul_add(self, rhs, carry, add)
1987 }
1988
1989 fn div_ceil(self, rhs: Self) -> Self {
1990 Self::div_ceil(self, rhs)
1991 }
1992
1993 fn next_multiple_of(self, rhs: Self) -> Self {
1994 Self::next_multiple_of(self, rhs)
1995 }
1996
1997 fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
1998 Self::checked_next_multiple_of(self, rhs)
1999 }
2000
2001 fn is_multiple_of(self, rhs: Self) -> bool {
2002 Self::is_multiple_of(self, rhs)
2003 }
2004
2005 fn is_power_of_two(self) -> bool {
2006 Self::is_power_of_two(self)
2007 }
2008
2009 fn next_power_of_two(self) -> Self {
2010 Self::next_power_of_two(self)
2011 }
2012
2013 fn checked_next_power_of_two(self) -> Option<Self> {
2014 Self::checked_next_power_of_two(self)
2015 }
2016
2017 }
2019 };
2020}
2021
2022impl_unsigned!(u8, i8);
2023impl_unsigned!(u16, i16);
2024impl_unsigned!(u32, i32);
2025impl_unsigned!(u64, i64);
2026impl_unsigned!(u128, i128);
2027impl_unsigned!(usize, isize);
2028
2029pub trait Signed: Integer<Signed = Self> + Neg<Output = Self> + From<i8> {
2030 #[must_use = "this returns the result of the operation, without modifying the original"]
2035 fn cast_unsigned(self) -> Self::Unsigned;
2036
2037 #[must_use = "this returns the result of the operation, without modifying the original"]
2039 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
2040
2041 #[must_use = "this returns the result of the operation, without modifying the original"]
2043 fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self;
2044
2045 #[must_use = "this returns the result of the operation, without modifying the original"]
2047 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self>;
2048
2049 #[must_use = "this returns the result of the operation, without modifying the original"]
2051 fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
2052
2053 #[must_use = "this returns the result of the operation, without modifying the original"]
2059 unsafe fn unchecked_neg(self) -> Self;
2060
2061 #[must_use = "this returns the result of the operation, without modifying the original"]
2063 fn checked_abs(self) -> Option<Self>;
2064
2065 #[must_use = "this returns the result of the operation, without modifying the original"]
2067 fn strict_abs(self) -> Self;
2068
2069 #[must_use = "this returns the result of the operation, without modifying the original"]
2071 fn checked_isqrt(self) -> Option<Self>;
2072
2073 #[must_use = "this returns the result of the operation, without modifying the original"]
2075 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self;
2076
2077 #[must_use = "this returns the result of the operation, without modifying the original"]
2079 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
2080
2081 #[must_use = "this returns the result of the operation, without modifying the original"]
2083 fn saturating_neg(self) -> Self;
2084
2085 #[must_use = "this returns the result of the operation, without modifying the original"]
2087 fn saturating_abs(self) -> Self;
2088
2089 #[must_use = "this returns the result of the operation, without modifying the original"]
2091 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self;
2092
2093 #[must_use = "this returns the result of the operation, without modifying the original"]
2095 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self;
2096
2097 #[must_use = "this returns the result of the operation, without modifying the original"]
2099 fn wrapping_abs(self) -> Self;
2100
2101 #[must_use = "this returns the result of the operation, without modifying the original"]
2103 fn unsigned_abs(self) -> Self::Unsigned;
2104
2105 #[must_use = "this returns the result of the operation, without modifying the original"]
2107 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
2108
2109 #[must_use = "this returns the result of the operation, without modifying the original"]
2111 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool);
2112
2113 #[must_use = "this returns the result of the operation, without modifying the original"]
2115 fn overflowing_abs(self) -> (Self, bool);
2116
2117 #[must_use = "this returns the result of the operation, without modifying the original"]
2119 fn abs(self) -> Self;
2120
2121 #[must_use = "this returns the result of the operation, without modifying the original"]
2123 fn signum(self) -> Self;
2124
2125 #[must_use]
2127 fn is_positive(self) -> bool;
2128
2129 #[must_use]
2131 fn is_negative(self) -> bool;
2132
2133 }
2135
2136macro_rules! impl_signed {
2137 ($ty:ty, $unsigned:ty) => {
2138 impl_integer!($ty, $unsigned, Self, abs: Self::abs, signum: Self::signum);
2139 impl Signed for $ty {
2140 fn cast_unsigned(self) -> Self::Unsigned {
2144 Self::cast_unsigned(self)
2145 }
2146
2147 fn checked_add_unsigned(self, rhs: Self::Unsigned) -> Option<Self> {
2148 Self::checked_add_unsigned(self, rhs)
2149 }
2150
2151 fn strict_add_unsigned(self, rhs: Self::Unsigned) -> Self {
2152 Self::strict_add_unsigned(self, rhs)
2153 }
2154
2155 fn checked_sub_unsigned(self, rhs: Self::Unsigned) -> Option<Self> {
2156 Self::checked_sub_unsigned(self, rhs)
2157 }
2158
2159 fn strict_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
2160 Self::strict_sub_unsigned(self, rhs)
2161 }
2162
2163 unsafe fn unchecked_neg(self) -> Self {
2164 unsafe { Self::unchecked_neg(self) }
2165 }
2166
2167 fn checked_abs(self) -> Option<Self> {
2168 Self::checked_abs(self)
2169 }
2170
2171 fn strict_abs(self) -> Self {
2172 Self::strict_abs(self)
2173 }
2174
2175 fn checked_isqrt(self) -> Option<Self> {
2176 Self::checked_isqrt(self)
2177 }
2178
2179 fn saturating_add_unsigned(self, rhs: Self::Unsigned) -> Self {
2180 Self::saturating_add_unsigned(self, rhs)
2181 }
2182
2183 fn saturating_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
2184 Self::saturating_sub_unsigned(self, rhs)
2185 }
2186
2187 fn saturating_neg(self) -> Self {
2188 Self::saturating_neg(self)
2189 }
2190
2191 fn saturating_abs(self) -> Self {
2192 Self::saturating_abs(self)
2193 }
2194
2195 fn wrapping_add_unsigned(self, rhs: Self::Unsigned) -> Self {
2196 Self::wrapping_add_unsigned(self, rhs)
2197 }
2198
2199 fn wrapping_sub_unsigned(self, rhs: Self::Unsigned) -> Self {
2200 Self::wrapping_sub_unsigned(self, rhs)
2201 }
2202
2203 fn wrapping_abs(self) -> Self {
2204 Self::wrapping_abs(self)
2205 }
2206
2207 fn unsigned_abs(self) -> Self::Unsigned {
2208 Self::unsigned_abs(self)
2209 }
2210
2211 fn overflowing_add_unsigned(self, rhs: Self::Unsigned) -> (Self, bool) {
2212 Self::overflowing_add_unsigned(self, rhs)
2213 }
2214
2215 fn overflowing_sub_unsigned(self, rhs: Self::Unsigned) -> (Self, bool) {
2216 Self::overflowing_sub_unsigned(self, rhs)
2217 }
2218
2219 fn overflowing_abs(self) -> (Self, bool) {
2220 Self::overflowing_abs(self)
2221 }
2222
2223 fn abs(self) -> Self {
2224 Self::abs(self)
2225 }
2226
2227 fn signum(self) -> Self {
2228 Self::signum(self)
2229 }
2230
2231 fn is_positive(self) -> bool {
2232 Self::is_positive(self)
2233 }
2234
2235 fn is_negative(self) -> bool {
2236 Self::is_negative(self)
2237 }
2238
2239 }
2241 };
2242}
2243
2244impl_signed!(i8, u8);
2245impl_signed!(i16, u16);
2246impl_signed!(i32, u32);
2247impl_signed!(i64, u64);
2248impl_signed!(i128, u128);
2249impl_signed!(isize, usize);
2250
2251#[cfg(test)]
2252mod test {
2253 use super::*;
2254
2255 #[test]
2256 fn test_subnormal_consts() {
2257 assert_eq!(f32::MIN_POSITIVE_SUBNORMAL, f32::from_bits(1));
2258 assert_eq!(f32::MAX_NEGATIVE_SUBNORMAL, -f32::from_bits(1));
2259 assert_eq!(f64::MIN_POSITIVE_SUBNORMAL, f64::from_bits(1));
2260 assert_eq!(f64::MAX_NEGATIVE_SUBNORMAL, -f64::from_bits(1));
2261 }
2262
2263 #[cfg(feature = "std")]
2264 #[test]
2265 fn test_float_floor() {
2266 assert_eq!(<f64 as Float>::floor(1.5), 1.0);
2267 }
2268
2269 #[test]
2270 fn test_euclid_core() {
2271 fn test_int<T: Integer>(a: T, b: T) -> (T, T) {
2272 (a.div_euclid(b), a.rem_euclid(b))
2273 }
2274
2275 assert_eq!(test_int(-7, 4), (-2, 1));
2276 }
2277
2278 #[cfg(feature = "std")]
2279 #[test]
2280 fn test_euclid_std() {
2281 fn test_num<T: Number>(a: T, b: T) -> (T, T) {
2282 (a.div_euclid(b), a.rem_euclid(b))
2283 }
2284
2285 assert_eq!(test_num(-7, 4), (-2, 1));
2286 assert_eq!(test_num(-7.0, 4.0), (-2.0, 1.0));
2287 }
2288
2289 #[test]
2290 fn test_abs() {
2291 fn test_abs<T: Number>(a: T) -> T {
2292 a.abs()
2293 }
2294
2295 assert_eq!(test_abs(1i32), 1);
2296 assert_eq!(test_abs(1u32), 1);
2297 assert_eq!(test_abs(1.0), 1.0);
2298
2299 assert_eq!(test_abs(-1i32), 1);
2300 assert_eq!(test_abs(-1.0), 1.0);
2301
2302 assert!(test_abs(f64::NAN).is_nan());
2303 }
2304
2305 #[test]
2306 fn test_signum() {
2307 fn test_signum<T: Number>(a: T) -> T {
2308 a.signum()
2309 }
2310
2311 assert_eq!(test_signum(123i32), 1);
2312 assert_eq!(test_signum(123u32), 1);
2313 assert_eq!(test_signum(123.0), 1.0);
2314
2315 assert_eq!(test_signum(0i32), 0);
2316 assert_eq!(test_signum(0u32), 0);
2317 assert_eq!(test_signum(0.0), 1.0);
2318 assert_eq!(test_signum(-0.0), -1.0);
2319
2320 assert_eq!(test_signum(-123i32), -1);
2321 assert_eq!(test_signum(-123.0), -1.0);
2322
2323 assert!(test_signum(f64::NAN).is_nan());
2324 }
2325
2326 #[test]
2327 fn test_int_conversions() {
2328 fn inner<T: Integer>(v: T) {
2329 let bytes = v.to_bytes();
2330 let v2: T = T::from_bytes(bytes);
2331 assert_eq!(v2, v);
2332
2333 let signed = v.to_signed();
2334 let v2 = T::from_signed(signed);
2335 assert_eq!(v2, v);
2336
2337 let unsigned = v.to_unsigned();
2338 let v2 = T::from_unsigned(unsigned);
2339 assert_eq!(v2, v);
2340 }
2341
2342 inner(123u64);
2343 inner(-123i64);
2344 }
2345
2346 #[test]
2347 fn test_number_ops() {
2348 #[allow(clippy::op_ref)]
2349 fn number_ops<T: Number>() {
2350 let three = T::TWO + T::ONE;
2351 let four = T::TWO + T::TWO;
2352 let eight = four * T::TWO;
2353
2354 assert_eq!(T::ONE + T::ONE, T::TWO);
2355 assert_eq!(T::ONE + &T::ONE, T::TWO);
2356
2357 assert_eq!(T::TWO - T::ONE, T::ONE);
2358 assert_eq!(T::TWO - &T::ONE, T::ONE);
2359
2360 assert_eq!(T::TWO * T::TWO, four);
2361 assert_eq!(T::TWO * &T::TWO, four);
2362
2363 assert_eq!(four / T::TWO, T::TWO);
2364 assert_eq!(four / &T::TWO, T::TWO);
2365
2366 assert_eq!(three % T::TWO, T::ONE);
2367 assert_eq!(three % &T::TWO, T::ONE);
2368
2369 let mut v = T::ZERO;
2370 v += T::TWO;
2371 v += &T::TWO;
2372 assert_eq!(v, four);
2373
2374 v -= T::ONE;
2375 v -= &T::ONE;
2376 assert_eq!(v, T::TWO);
2377
2378 v *= T::TWO;
2379 v *= &T::TWO;
2380 assert_eq!(v, eight);
2381
2382 v /= T::TWO;
2383 v /= &T::TWO;
2384 assert_eq!(v, T::TWO);
2385
2386 v += T::ONE;
2387 v %= three;
2388 assert_eq!(v, T::ZERO);
2389 v += three;
2390 v %= &T::TWO;
2391 assert_eq!(v, T::ONE);
2392 }
2393
2394 number_ops::<u8>();
2395 }
2396
2397 #[test]
2398 fn test_integer_ops() {
2399 #[allow(clippy::op_ref)]
2400 fn integer_ops<T: Integer>() {
2401 let three = T::TWO + T::ONE;
2402 let four = T::TWO + T::TWO;
2403 let seven = four * T::TWO - T::ONE;
2404
2405 assert_eq!(!!T::ONE, T::ONE);
2406
2407 assert_eq!(three & T::TWO, T::TWO);
2408 assert_eq!(three & &T::TWO, T::TWO);
2409
2410 assert_eq!(T::ONE | T::TWO, three);
2411 assert_eq!(T::ONE | &T::TWO, three);
2412
2413 assert_eq!(three ^ T::TWO, T::ONE);
2414 assert_eq!(three ^ &T::TWO, T::ONE);
2415
2416 assert_eq!(T::ONE << T::TWO, four);
2417 assert_eq!(T::ONE << &T::TWO, four);
2418
2419 assert_eq!(seven >> T::ONE, three);
2420 assert_eq!(seven >> &T::ONE, three);
2421
2422 let mut v = T::ZERO;
2423 v |= T::ONE;
2424 assert_eq!(v, T::ONE);
2425 v |= &T::TWO;
2426 assert_eq!(v, three);
2427
2428 v &= T::TWO;
2429 assert_eq!(v, T::TWO);
2430 v &= &T::ONE;
2431 assert_eq!(v, T::ZERO);
2432
2433 v ^= three;
2434 assert_eq!(v, three);
2435 v ^= &T::TWO;
2436 assert_eq!(v, T::ONE);
2437
2438 v <<= T::ONE;
2439 assert_eq!(v, T::TWO);
2440 v <<= &T::ZERO;
2441 assert_eq!(v, T::TWO);
2442
2443 v >>= T::ZERO;
2444 assert_eq!(v, T::TWO);
2445 v >>= &T::TWO;
2446 assert_eq!(v, T::ZERO);
2447 }
2448
2449 integer_ops::<u8>();
2450 }
2451
2452 #[test]
2453 fn casts() {
2454 assert_eq!(0u8.cast_int::<i8>(), 0i8);
2455 assert_eq!(0u8.cast_number::<i8>(), 0i8);
2456 assert_eq!(0u8.cast_float::<f32>(), 0f32);
2457 assert_eq!(0u8.cast_number::<f32>(), 0f32);
2458
2459 assert_eq!(0f32.cast_int::<u8>(), 0u8);
2460 assert_eq!(0f32.cast_number::<u8>(), 0u8);
2461 assert_eq!(0f32.cast_float::<f64>(), 0f64);
2462 assert_eq!(0f32.cast_number::<f64>(), 0f64);
2463 }
2464}