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::{
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    /// Same as the builtin `MIN` associated constant, except that this is
57    /// [`NEG_INFINITY`](Float::NEG_INFINITY) for floats instead of the minimum
58    /// finite value.
59    const MIN: Self;
60    /// Same as the builtin `MAX` associated constant, except that this is
61    /// [`INFINITY`](Float::INFINITY) for floats instead of the maximum finite
62    /// value.
63    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    /// See [`i32::abs`].
235    #[must_use = "this returns the result of the operation, without modifying the original"]
236    fn abs(self) -> Self;
237
238    /// See [`i32::signum`].
239    #[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] // rustfmt thinks the traits can fit on one line, but they can't
327pub 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    // @START@ DECL FLOAT
363    // Generated by generate_delegates.py
364
365    /// See [`f32::is_nan`].
366    #[must_use]
367    fn is_nan(self) -> bool;
368
369    /// See [`f32::is_infinite`].
370    #[must_use]
371    fn is_infinite(self) -> bool;
372
373    /// See [`f32::is_finite`].
374    #[must_use]
375    fn is_finite(self) -> bool;
376
377    /// See [`f32::is_subnormal`].
378    #[must_use]
379    fn is_subnormal(self) -> bool;
380
381    /// See [`f32::is_normal`].
382    #[must_use]
383    fn is_normal(self) -> bool;
384
385    /// See [`f32::classify`].
386    #[must_use]
387    fn classify(self) -> FpCategory;
388
389    /// See [`f32::is_sign_positive`].
390    #[must_use]
391    fn is_sign_positive(self) -> bool;
392
393    /// See [`f32::is_sign_negative`].
394    #[must_use]
395    fn is_sign_negative(self) -> bool;
396
397    /// See [`f32::next_up`].
398    #[must_use = "method returns a new number and does not mutate the original value"]
399    fn next_up(self) -> Self;
400
401    /// See [`f32::next_down`].
402    #[must_use = "method returns a new number and does not mutate the original value"]
403    fn next_down(self) -> Self;
404
405    /// See [`f32::recip`].
406    #[must_use = "this returns the result of the operation, without modifying the original"]
407    fn recip(self) -> Self;
408
409    /// See [`f32::to_degrees`].
410    #[must_use = "this returns the result of the operation, without modifying the original"]
411    fn to_degrees(self) -> Self;
412
413    /// See [`f32::to_radians`].
414    #[must_use = "this returns the result of the operation, without modifying the original"]
415    fn to_radians(self) -> Self;
416
417    /// See [`f32::max`].
418    #[must_use = "this returns the result of the comparison, without modifying either input"]
419    fn max(self, other: Self) -> Self;
420
421    /// See [`f32::min`].
422    #[must_use = "this returns the result of the comparison, without modifying either input"]
423    fn min(self, other: Self) -> Self;
424
425    /// See [`f32::midpoint`].
426    #[must_use = "this returns the result of the operation, without modifying the original"]
427    fn midpoint(self, other: Self) -> Self;
428
429    /// See [`f32::to_bits`].
430    #[must_use = "this returns the result of the operation, without modifying the original"]
431    fn to_bits(self) -> Self::Bits;
432
433    /// See [`f32::from_bits`].
434    #[must_use]
435    fn from_bits(v: Self::Bits) -> Self;
436
437    /// See [`f32::total_cmp`].
438    #[must_use]
439    fn total_cmp(&self, other: &Self) -> core::cmp::Ordering;
440
441    /// See [`f32::clamp`].
442    #[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    /// See [`f32::copysign`].
446    #[must_use = "method returns a new number and does not mutate the original value"]
447    fn copysign(self, sign: Self) -> Self;
448
449    /// See [`f32::algebraic_add`].
450    #[must_use = "method returns a new number and does not mutate the original value"]
451    fn algebraic_add(self, rhs: Self) -> Self;
452
453    /// See [`f32::algebraic_sub`].
454    #[must_use = "method returns a new number and does not mutate the original value"]
455    fn algebraic_sub(self, rhs: Self) -> Self;
456
457    /// See [`f32::algebraic_mul`].
458    #[must_use = "method returns a new number and does not mutate the original value"]
459    fn algebraic_mul(self, rhs: Self) -> Self;
460
461    /// See [`f32::algebraic_div`].
462    #[must_use = "method returns a new number and does not mutate the original value"]
463    fn algebraic_div(self, rhs: Self) -> Self;
464
465    /// See [`f32::algebraic_rem`].
466    #[must_use = "method returns a new number and does not mutate the original value"]
467    fn algebraic_rem(self, rhs: Self) -> Self;
468
469    /// See [`f32::floor`].
470    #[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    /// See [`f32::ceil`].
475    #[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    /// See [`f32::round`].
480    #[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    /// See [`f32::round_ties_even`].
485    #[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    /// See [`f32::trunc`].
490    #[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    /// See [`f32::fract`].
495    #[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    /// See [`f32::mul_add`].
500    #[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    /// See [`f32::powi`].
505    #[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    /// See [`f32::powf`].
510    #[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    /// See [`f32::sqrt`].
515    #[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    /// See [`f32::exp`].
520    #[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    /// See [`f32::exp2`].
525    #[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    /// See [`f32::ln`].
530    #[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    /// See [`f32::log`].
535    #[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    /// See [`f32::log2`].
540    #[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    /// See [`f32::log10`].
545    #[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    /// See [`f32::cbrt`].
550    #[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    /// See [`f32::hypot`].
555    #[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    /// See [`f32::sin`].
560    #[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    /// See [`f32::cos`].
565    #[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    /// See [`f32::tan`].
570    #[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    /// See [`f32::asin`].
575    #[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    /// See [`f32::acos`].
580    #[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    /// See [`f32::atan`].
585    #[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    /// See [`f32::atan2`].
590    #[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    /// See [`f32::sin_cos`].
595    #[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    /// See [`f32::exp_m1`].
600    #[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    /// See [`f32::ln_1p`].
605    #[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    /// See [`f32::sinh`].
610    #[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    /// See [`f32::cosh`].
615    #[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    /// See [`f32::tanh`].
620    #[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    /// See [`f32::asinh`].
625    #[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    /// See [`f32::acosh`].
630    #[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    /// See [`f32::atanh`].
635    #[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    // @END@ DECL FLOAT
640}
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            // @START@ IMPL FLOAT
680            // Generated by generate_delegates.py
681
682            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            // @END@ IMPL FLOAT
957        }
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    /// See [`i32::div_euclid`].
1031    #[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    /// See [`i32::rem_euclid`].
1036    #[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    // @START@ DECL INTEGER
1041    // Generated by generate_delegates.py
1042
1043    /// See [`i32::from_str_radix`].
1044    fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>;
1045
1046    /// See [`i32::count_ones`].
1047    #[must_use = "this returns the result of the operation, without modifying the original"]
1048    fn count_ones(self) -> u32;
1049
1050    /// See [`i32::count_zeros`].
1051    #[must_use = "this returns the result of the operation, without modifying the original"]
1052    fn count_zeros(self) -> u32;
1053
1054    /// See [`i32::leading_zeros`].
1055    #[must_use = "this returns the result of the operation, without modifying the original"]
1056    fn leading_zeros(self) -> u32;
1057
1058    /// See [`i32::trailing_zeros`].
1059    #[must_use = "this returns the result of the operation, without modifying the original"]
1060    fn trailing_zeros(self) -> u32;
1061
1062    /// See [`i32::leading_ones`].
1063    #[must_use = "this returns the result of the operation, without modifying the original"]
1064    fn leading_ones(self) -> u32;
1065
1066    /// See [`i32::trailing_ones`].
1067    #[must_use = "this returns the result of the operation, without modifying the original"]
1068    fn trailing_ones(self) -> u32;
1069
1070    /// See [`i32::isolate_highest_one`].
1071    #[must_use = "this returns the result of the operation, without modifying the original"]
1072    fn isolate_highest_one(self) -> Self;
1073
1074    /// See [`i32::isolate_lowest_one`].
1075    #[must_use = "this returns the result of the operation, without modifying the original"]
1076    fn isolate_lowest_one(self) -> Self;
1077
1078    /// See [`i32::highest_one`].
1079    #[must_use = "this returns the result of the operation, without modifying the original"]
1080    fn highest_one(self) -> Option<u32>;
1081
1082    /// See [`i32::lowest_one`].
1083    #[must_use = "this returns the result of the operation, without modifying the original"]
1084    fn lowest_one(self) -> Option<u32>;
1085
1086    /// See [`i32::rotate_left`].
1087    #[must_use = "this returns the result of the operation, without modifying the original"]
1088    fn rotate_left(self, n: u32) -> Self;
1089
1090    /// See [`i32::rotate_right`].
1091    #[must_use = "this returns the result of the operation, without modifying the original"]
1092    fn rotate_right(self, n: u32) -> Self;
1093
1094    /// See [`i32::swap_bytes`].
1095    #[must_use = "this returns the result of the operation, without modifying the original"]
1096    fn swap_bytes(self) -> Self;
1097
1098    /// See [`i32::reverse_bits`].
1099    #[must_use = "this returns the result of the operation, without modifying the original"]
1100    fn reverse_bits(self) -> Self;
1101
1102    /// See [`i32::from_be`].
1103    #[must_use]
1104    fn from_be(x: Self) -> Self;
1105
1106    /// See [`i32::from_le`].
1107    #[must_use]
1108    fn from_le(x: Self) -> Self;
1109
1110    /// See [`i32::to_be`].
1111    #[must_use = "this returns the result of the operation, without modifying the original"]
1112    fn to_be(self) -> Self;
1113
1114    /// See [`i32::to_le`].
1115    #[must_use = "this returns the result of the operation, without modifying the original"]
1116    fn to_le(self) -> Self;
1117
1118    /// See [`i32::checked_add`].
1119    #[must_use = "this returns the result of the operation, without modifying the original"]
1120    fn checked_add(self, rhs: Self) -> Option<Self>;
1121
1122    /// See [`i32::strict_add`].
1123    #[must_use = "this returns the result of the operation, without modifying the original"]
1124    fn strict_add(self, rhs: Self) -> Self;
1125
1126    /// See [`i32::unchecked_add`].
1127    ///
1128    /// # Safety
1129    ///
1130    /// See [`i32::unchecked_add`].
1131    #[must_use = "this returns the result of the operation, without modifying the original"]
1132    unsafe fn unchecked_add(self, rhs: Self) -> Self;
1133
1134    /// See [`i32::checked_sub`].
1135    #[must_use = "this returns the result of the operation, without modifying the original"]
1136    fn checked_sub(self, rhs: Self) -> Option<Self>;
1137
1138    /// See [`i32::strict_sub`].
1139    #[must_use = "this returns the result of the operation, without modifying the original"]
1140    fn strict_sub(self, rhs: Self) -> Self;
1141
1142    /// See [`i32::unchecked_sub`].
1143    ///
1144    /// # Safety
1145    ///
1146    /// See [`i32::unchecked_sub`].
1147    #[must_use = "this returns the result of the operation, without modifying the original"]
1148    unsafe fn unchecked_sub(self, rhs: Self) -> Self;
1149
1150    /// See [`i32::checked_mul`].
1151    #[must_use = "this returns the result of the operation, without modifying the original"]
1152    fn checked_mul(self, rhs: Self) -> Option<Self>;
1153
1154    /// See [`i32::strict_mul`].
1155    #[must_use = "this returns the result of the operation, without modifying the original"]
1156    fn strict_mul(self, rhs: Self) -> Self;
1157
1158    /// See [`i32::unchecked_mul`].
1159    ///
1160    /// # Safety
1161    ///
1162    /// See [`i32::unchecked_mul`].
1163    #[must_use = "this returns the result of the operation, without modifying the original"]
1164    unsafe fn unchecked_mul(self, rhs: Self) -> Self;
1165
1166    /// See [`i32::checked_div`].
1167    #[must_use = "this returns the result of the operation, without modifying the original"]
1168    fn checked_div(self, rhs: Self) -> Option<Self>;
1169
1170    /// See [`i32::strict_div`].
1171    #[must_use = "this returns the result of the operation, without modifying the original"]
1172    fn strict_div(self, rhs: Self) -> Self;
1173
1174    /// See [`i32::checked_div_euclid`].
1175    #[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    /// See [`i32::strict_div_euclid`].
1179    #[must_use = "this returns the result of the operation, without modifying the original"]
1180    fn strict_div_euclid(self, rhs: Self) -> Self;
1181
1182    /// See [`i32::checked_rem`].
1183    #[must_use = "this returns the result of the operation, without modifying the original"]
1184    fn checked_rem(self, rhs: Self) -> Option<Self>;
1185
1186    /// See [`i32::strict_rem`].
1187    #[must_use = "this returns the result of the operation, without modifying the original"]
1188    fn strict_rem(self, rhs: Self) -> Self;
1189
1190    /// See [`i32::checked_rem_euclid`].
1191    #[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    /// See [`i32::strict_rem_euclid`].
1195    #[must_use = "this returns the result of the operation, without modifying the original"]
1196    fn strict_rem_euclid(self, rhs: Self) -> Self;
1197
1198    /// See [`i32::checked_neg`].
1199    #[must_use = "this returns the result of the operation, without modifying the original"]
1200    fn checked_neg(self) -> Option<Self>;
1201
1202    /// See [`i32::strict_neg`].
1203    #[must_use = "this returns the result of the operation, without modifying the original"]
1204    fn strict_neg(self) -> Self;
1205
1206    /// See [`i32::checked_shl`].
1207    #[must_use = "this returns the result of the operation, without modifying the original"]
1208    fn checked_shl(self, rhs: u32) -> Option<Self>;
1209
1210    /// See [`i32::strict_shl`].
1211    #[must_use = "this returns the result of the operation, without modifying the original"]
1212    fn strict_shl(self, rhs: u32) -> Self;
1213
1214    /// See [`i32::unchecked_shl`].
1215    ///
1216    /// # Safety
1217    ///
1218    /// See [`i32::unchecked_shl`].
1219    #[must_use = "this returns the result of the operation, without modifying the original"]
1220    unsafe fn unchecked_shl(self, rhs: u32) -> Self;
1221
1222    /// See [`i32::unbounded_shl`].
1223    #[must_use = "this returns the result of the operation, without modifying the original"]
1224    fn unbounded_shl(self, rhs: u32) -> Self;
1225
1226    /// See [`i32::checked_shr`].
1227    #[must_use = "this returns the result of the operation, without modifying the original"]
1228    fn checked_shr(self, rhs: u32) -> Option<Self>;
1229
1230    /// See [`i32::strict_shr`].
1231    #[must_use = "this returns the result of the operation, without modifying the original"]
1232    fn strict_shr(self, rhs: u32) -> Self;
1233
1234    /// See [`i32::unchecked_shr`].
1235    ///
1236    /// # Safety
1237    ///
1238    /// See [`i32::unchecked_shr`].
1239    #[must_use = "this returns the result of the operation, without modifying the original"]
1240    unsafe fn unchecked_shr(self, rhs: u32) -> Self;
1241
1242    /// See [`i32::unbounded_shr`].
1243    #[must_use = "this returns the result of the operation, without modifying the original"]
1244    fn unbounded_shr(self, rhs: u32) -> Self;
1245
1246    /// See [`i32::checked_pow`].
1247    #[must_use = "this returns the result of the operation, without modifying the original"]
1248    fn checked_pow(self, exp: u32) -> Option<Self>;
1249
1250    /// See [`i32::strict_pow`].
1251    #[must_use = "this returns the result of the operation, without modifying the original"]
1252    fn strict_pow(self, exp: u32) -> Self;
1253
1254    /// See [`i32::saturating_add`].
1255    #[must_use = "this returns the result of the operation, without modifying the original"]
1256    fn saturating_add(self, rhs: Self) -> Self;
1257
1258    /// See [`i32::saturating_sub`].
1259    #[must_use = "this returns the result of the operation, without modifying the original"]
1260    fn saturating_sub(self, rhs: Self) -> Self;
1261
1262    /// See [`i32::saturating_mul`].
1263    #[must_use = "this returns the result of the operation, without modifying the original"]
1264    fn saturating_mul(self, rhs: Self) -> Self;
1265
1266    /// See [`i32::saturating_div`].
1267    #[must_use = "this returns the result of the operation, without modifying the original"]
1268    fn saturating_div(self, rhs: Self) -> Self;
1269
1270    /// See [`i32::saturating_pow`].
1271    #[must_use = "this returns the result of the operation, without modifying the original"]
1272    fn saturating_pow(self, exp: u32) -> Self;
1273
1274    /// See [`i32::wrapping_add`].
1275    #[must_use = "this returns the result of the operation, without modifying the original"]
1276    fn wrapping_add(self, rhs: Self) -> Self;
1277
1278    /// See [`i32::wrapping_sub`].
1279    #[must_use = "this returns the result of the operation, without modifying the original"]
1280    fn wrapping_sub(self, rhs: Self) -> Self;
1281
1282    /// See [`i32::wrapping_mul`].
1283    #[must_use = "this returns the result of the operation, without modifying the original"]
1284    fn wrapping_mul(self, rhs: Self) -> Self;
1285
1286    /// See [`i32::wrapping_div`].
1287    #[must_use = "this returns the result of the operation, without modifying the original"]
1288    fn wrapping_div(self, rhs: Self) -> Self;
1289
1290    /// See [`i32::wrapping_div_euclid`].
1291    #[must_use = "this returns the result of the operation, without modifying the original"]
1292    fn wrapping_div_euclid(self, rhs: Self) -> Self;
1293
1294    /// See [`i32::wrapping_rem`].
1295    #[must_use = "this returns the result of the operation, without modifying the original"]
1296    fn wrapping_rem(self, rhs: Self) -> Self;
1297
1298    /// See [`i32::wrapping_rem_euclid`].
1299    #[must_use = "this returns the result of the operation, without modifying the original"]
1300    fn wrapping_rem_euclid(self, rhs: Self) -> Self;
1301
1302    /// See [`i32::wrapping_neg`].
1303    #[must_use = "this returns the result of the operation, without modifying the original"]
1304    fn wrapping_neg(self) -> Self;
1305
1306    /// See [`i32::wrapping_shl`].
1307    #[must_use = "this returns the result of the operation, without modifying the original"]
1308    fn wrapping_shl(self, rhs: u32) -> Self;
1309
1310    /// See [`i32::wrapping_shr`].
1311    #[must_use = "this returns the result of the operation, without modifying the original"]
1312    fn wrapping_shr(self, rhs: u32) -> Self;
1313
1314    /// See [`i32::wrapping_pow`].
1315    #[must_use = "this returns the result of the operation, without modifying the original"]
1316    fn wrapping_pow(self, exp: u32) -> Self;
1317
1318    /// See [`i32::overflowing_add`].
1319    #[must_use = "this returns the result of the operation, without modifying the original"]
1320    fn overflowing_add(self, rhs: Self) -> (Self, bool);
1321
1322    /// See [`i32::overflowing_sub`].
1323    #[must_use = "this returns the result of the operation, without modifying the original"]
1324    fn overflowing_sub(self, rhs: Self) -> (Self, bool);
1325
1326    /// See [`i32::overflowing_mul`].
1327    #[must_use = "this returns the result of the operation, without modifying the original"]
1328    fn overflowing_mul(self, rhs: Self) -> (Self, bool);
1329
1330    /// See [`i32::overflowing_div`].
1331    #[must_use = "this returns the result of the operation, without modifying the original"]
1332    fn overflowing_div(self, rhs: Self) -> (Self, bool);
1333
1334    /// See [`i32::overflowing_div_euclid`].
1335    #[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    /// See [`i32::overflowing_rem`].
1339    #[must_use = "this returns the result of the operation, without modifying the original"]
1340    fn overflowing_rem(self, rhs: Self) -> (Self, bool);
1341
1342    /// See [`i32::overflowing_rem_euclid`].
1343    #[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    /// See [`i32::overflowing_neg`].
1347    #[must_use = "this returns the result of the operation, without modifying the original"]
1348    fn overflowing_neg(self) -> (Self, bool);
1349
1350    /// See [`i32::overflowing_shl`].
1351    #[must_use = "this returns the result of the operation, without modifying the original"]
1352    fn overflowing_shl(self, rhs: u32) -> (Self, bool);
1353
1354    /// See [`i32::overflowing_shr`].
1355    #[must_use = "this returns the result of the operation, without modifying the original"]
1356    fn overflowing_shr(self, rhs: u32) -> (Self, bool);
1357
1358    /// See [`i32::overflowing_pow`].
1359    #[must_use = "this returns the result of the operation, without modifying the original"]
1360    fn overflowing_pow(self, exp: u32) -> (Self, bool);
1361
1362    /// See [`i32::pow`].
1363    #[must_use = "this returns the result of the operation, without modifying the original"]
1364    fn pow(self, exp: u32) -> Self;
1365
1366    /// See [`i32::isqrt`].
1367    #[must_use = "this returns the result of the operation, without modifying the original"]
1368    fn isqrt(self) -> Self;
1369
1370    /// See [`i32::ilog`].
1371    #[must_use = "this returns the result of the operation, without modifying the original"]
1372    fn ilog(self, base: Self) -> u32;
1373
1374    /// See [`i32::ilog2`].
1375    #[must_use = "this returns the result of the operation, without modifying the original"]
1376    fn ilog2(self) -> u32;
1377
1378    /// See [`i32::ilog10`].
1379    #[must_use = "this returns the result of the operation, without modifying the original"]
1380    fn ilog10(self) -> u32;
1381
1382    /// See [`i32::checked_ilog`].
1383    #[must_use = "this returns the result of the operation, without modifying the original"]
1384    fn checked_ilog(self, base: Self) -> Option<u32>;
1385
1386    /// See [`i32::checked_ilog2`].
1387    #[must_use = "this returns the result of the operation, without modifying the original"]
1388    fn checked_ilog2(self) -> Option<u32>;
1389
1390    /// See [`i32::checked_ilog10`].
1391    #[must_use = "this returns the result of the operation, without modifying the original"]
1392    fn checked_ilog10(self) -> Option<u32>;
1393
1394    /// See [`i32::abs_diff`].
1395    #[must_use = "this returns the result of the operation, without modifying the original"]
1396    fn abs_diff(self, other: Self) -> Self::Unsigned;
1397
1398    /// See [`i32::midpoint`].
1399    #[must_use = "this returns the result of the operation, without modifying the original"]
1400    fn midpoint(self, rhs: Self) -> Self;
1401
1402    // @END@ DECL INTEGER
1403}
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            // @START@ IMPL INTEGER
1459            // Generated by generate_delegates.py
1460
1461            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            // @END@ IMPL INTEGER
1802        }
1803    };
1804}
1805
1806pub trait Unsigned: Integer<Unsigned = Self> + From<u8> {
1807    // @START@ DECL UNSIGNED
1808    // Generated by generate_delegates.py
1809
1810    /// See [`u32::bit_width`].
1811    #[must_use = "this returns the result of the operation, without modifying the original"]
1812    fn bit_width(self) -> u32;
1813
1814    /// See [`u32::cast_signed`].
1815    #[must_use = "this returns the result of the operation, without modifying the original"]
1816    fn cast_signed(self) -> Self::Signed;
1817
1818    /// See [`u32::checked_add_signed`].
1819    #[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    /// See [`u32::strict_add_signed`].
1823    #[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    /// See [`u32::checked_sub_signed`].
1827    #[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    /// See [`u32::strict_sub_signed`].
1831    #[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    /// See [`u32::checked_signed_diff`].
1835    fn checked_signed_diff(self, rhs: Self) -> Option<Self::Signed>;
1836
1837    /// See [`u32::saturating_add_signed`].
1838    #[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    /// See [`u32::saturating_sub_signed`].
1842    #[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    /// See [`u32::wrapping_add_signed`].
1846    #[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    /// See [`u32::wrapping_sub_signed`].
1850    #[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    /// See [`u32::carrying_add`].
1854    #[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    /// See [`u32::overflowing_add_signed`].
1858    #[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    /// See [`u32::borrowing_sub`].
1862    #[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    /// See [`u32::overflowing_sub_signed`].
1866    #[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    /// See [`u32::carrying_mul`].
1870    #[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    /// See [`u32::carrying_mul_add`].
1874    #[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    /// See [`u32::div_ceil`].
1878    #[must_use = "this returns the result of the operation, without modifying the original"]
1879    fn div_ceil(self, rhs: Self) -> Self;
1880
1881    /// See [`u32::next_multiple_of`].
1882    #[must_use = "this returns the result of the operation, without modifying the original"]
1883    fn next_multiple_of(self, rhs: Self) -> Self;
1884
1885    /// See [`u32::checked_next_multiple_of`].
1886    #[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    /// See [`u32::is_multiple_of`].
1890    #[must_use]
1891    fn is_multiple_of(self, rhs: Self) -> bool;
1892
1893    /// See [`u32::is_power_of_two`].
1894    #[must_use]
1895    fn is_power_of_two(self) -> bool;
1896
1897    /// See [`u32::next_power_of_two`].
1898    #[must_use = "this returns the result of the operation, without modifying the original"]
1899    fn next_power_of_two(self) -> Self;
1900
1901    /// See [`u32::checked_next_power_of_two`].
1902    #[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    // @END@ IMPL UNSIGNED
1906}
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            // @START@ IMPL UNSIGNED
1919            // Generated by generate_delegates.py
1920
1921            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            // @END@ IMPL UNSIGNED
2018        }
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    // @START@ DECL SIGNED
2031    // Generated by generate_delegates.py
2032
2033    /// See [`i32::cast_unsigned`].
2034    #[must_use = "this returns the result of the operation, without modifying the original"]
2035    fn cast_unsigned(self) -> Self::Unsigned;
2036
2037    /// See [`i32::checked_add_unsigned`].
2038    #[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    /// See [`i32::strict_add_unsigned`].
2042    #[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    /// See [`i32::checked_sub_unsigned`].
2046    #[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    /// See [`i32::strict_sub_unsigned`].
2050    #[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    /// See [`i32::unchecked_neg`].
2054    ///
2055    /// # Safety
2056    ///
2057    /// See [`i32::unchecked_neg`].
2058    #[must_use = "this returns the result of the operation, without modifying the original"]
2059    unsafe fn unchecked_neg(self) -> Self;
2060
2061    /// See [`i32::checked_abs`].
2062    #[must_use = "this returns the result of the operation, without modifying the original"]
2063    fn checked_abs(self) -> Option<Self>;
2064
2065    /// See [`i32::strict_abs`].
2066    #[must_use = "this returns the result of the operation, without modifying the original"]
2067    fn strict_abs(self) -> Self;
2068
2069    /// See [`i32::checked_isqrt`].
2070    #[must_use = "this returns the result of the operation, without modifying the original"]
2071    fn checked_isqrt(self) -> Option<Self>;
2072
2073    /// See [`i32::saturating_add_unsigned`].
2074    #[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    /// See [`i32::saturating_sub_unsigned`].
2078    #[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    /// See [`i32::saturating_neg`].
2082    #[must_use = "this returns the result of the operation, without modifying the original"]
2083    fn saturating_neg(self) -> Self;
2084
2085    /// See [`i32::saturating_abs`].
2086    #[must_use = "this returns the result of the operation, without modifying the original"]
2087    fn saturating_abs(self) -> Self;
2088
2089    /// See [`i32::wrapping_add_unsigned`].
2090    #[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    /// See [`i32::wrapping_sub_unsigned`].
2094    #[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    /// See [`i32::wrapping_abs`].
2098    #[must_use = "this returns the result of the operation, without modifying the original"]
2099    fn wrapping_abs(self) -> Self;
2100
2101    /// See [`i32::unsigned_abs`].
2102    #[must_use = "this returns the result of the operation, without modifying the original"]
2103    fn unsigned_abs(self) -> Self::Unsigned;
2104
2105    /// See [`i32::overflowing_add_unsigned`].
2106    #[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    /// See [`i32::overflowing_sub_unsigned`].
2110    #[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    /// See [`i32::overflowing_abs`].
2114    #[must_use = "this returns the result of the operation, without modifying the original"]
2115    fn overflowing_abs(self) -> (Self, bool);
2116
2117    /// See [`i32::abs`].
2118    #[must_use = "this returns the result of the operation, without modifying the original"]
2119    fn abs(self) -> Self;
2120
2121    /// See [`i32::signum`].
2122    #[must_use = "this returns the result of the operation, without modifying the original"]
2123    fn signum(self) -> Self;
2124
2125    /// See [`i32::is_positive`].
2126    #[must_use]
2127    fn is_positive(self) -> bool;
2128
2129    /// See [`i32::is_negative`].
2130    #[must_use]
2131    fn is_negative(self) -> bool;
2132
2133    // @END@ DECL SIGNED
2134}
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            // @START@ IMPL SIGNED
2141            // Generated by generate_delegates.py
2142
2143            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            // @END@ IMPL SIGNED
2240        }
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}