Skip to main content

std_traits/
num.rs

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