num_primitive/
integer.rs

1use crate::{PrimitiveError, PrimitiveNumber, PrimitiveNumberRef};
2
3/// Trait for all primitive [integer types], including the supertrait [`PrimitiveNumber`].
4///
5/// This encapsulates trait implementations, constants, and inherent methods that are common among
6/// all of the primitive integer types: [`i8`], [`i16`], [`i32`], [`i64`], [`i128`], [`isize`],
7/// [`u8`], [`u16`], [`u32`], [`u64`], [`u128`], and [`usize`].
8///
9/// See the corresponding items on the individual types for more documentation and examples.
10///
11/// This trait is sealed with a private trait to prevent downstream implementations, so we may
12/// continue to expand along with the standard library without worrying about breaking changes for
13/// implementors.
14///
15/// [integer types]: https://doc.rust-lang.org/reference/types/numeric.html#r-type.numeric.int
16///
17/// # Examples
18///
19/// ```
20/// use num_primitive::PrimitiveInteger;
21///
22/// fn div_rem<T: PrimitiveInteger>(a: T, b: T) -> (T, T) {
23///     (a / b, a % b)
24/// }
25///
26/// fn div_rem_euclid<T: PrimitiveInteger>(a: T, b: T) -> (T, T) {
27///     (a.div_euclid(b), a.rem_euclid(b))
28/// }
29///
30/// assert_eq!(div_rem::<u8>(48, 18), (2, 12));
31/// assert_eq!(div_rem::<i8>(-48, 18), (-2, -12));
32///
33/// assert_eq!(div_rem_euclid::<u8>(48, 18), (2, 12));
34/// assert_eq!(div_rem_euclid::<i8>(-48, 18), (-3, 6));
35/// ```
36///
37pub trait PrimitiveInteger:
38    PrimitiveNumber
39    + core::cmp::Eq
40    + core::cmp::Ord
41    + core::convert::TryFrom<i8, Error: PrimitiveError>
42    + core::convert::TryFrom<i16, Error: PrimitiveError>
43    + core::convert::TryFrom<i32, Error: PrimitiveError>
44    + core::convert::TryFrom<i64, Error: PrimitiveError>
45    + core::convert::TryFrom<i128, Error: PrimitiveError>
46    + core::convert::TryFrom<isize, Error: PrimitiveError>
47    + core::convert::TryFrom<u8, Error: PrimitiveError>
48    + core::convert::TryFrom<u16, Error: PrimitiveError>
49    + core::convert::TryFrom<u32, Error: PrimitiveError>
50    + core::convert::TryFrom<u64, Error: PrimitiveError>
51    + core::convert::TryFrom<u128, Error: PrimitiveError>
52    + core::convert::TryFrom<usize, Error: PrimitiveError>
53    + core::convert::TryInto<i8, Error: PrimitiveError>
54    + core::convert::TryInto<i16, Error: PrimitiveError>
55    + core::convert::TryInto<i32, Error: PrimitiveError>
56    + core::convert::TryInto<i64, Error: PrimitiveError>
57    + core::convert::TryInto<i128, Error: PrimitiveError>
58    + core::convert::TryInto<isize, Error: PrimitiveError>
59    + core::convert::TryInto<u8, Error: PrimitiveError>
60    + core::convert::TryInto<u16, Error: PrimitiveError>
61    + core::convert::TryInto<u32, Error: PrimitiveError>
62    + core::convert::TryInto<u64, Error: PrimitiveError>
63    + core::convert::TryInto<u128, Error: PrimitiveError>
64    + core::convert::TryInto<usize, Error: PrimitiveError>
65    + core::fmt::Binary
66    + core::fmt::LowerHex
67    + core::fmt::Octal
68    + core::fmt::UpperHex
69    + core::hash::Hash
70    + core::ops::BitAnd<Self, Output = Self>
71    + core::ops::BitAndAssign<Self>
72    + core::ops::BitOr<Self, Output = Self>
73    + core::ops::BitOrAssign<Self>
74    + core::ops::BitXor<Self, Output = Self>
75    + core::ops::BitXorAssign<Self>
76    + core::ops::Not<Output = Self>
77    + core::ops::Shl<Self, Output = Self>
78    + core::ops::Shl<i8, Output = Self>
79    + core::ops::Shl<i16, Output = Self>
80    + core::ops::Shl<i32, Output = Self>
81    + core::ops::Shl<i64, Output = Self>
82    + core::ops::Shl<i128, Output = Self>
83    + core::ops::Shl<isize, Output = Self>
84    + core::ops::Shl<u8, Output = Self>
85    + core::ops::Shl<u16, Output = Self>
86    + core::ops::Shl<u32, Output = Self>
87    + core::ops::Shl<u64, Output = Self>
88    + core::ops::Shl<u128, Output = Self>
89    + core::ops::Shl<usize, Output = Self>
90    + core::ops::ShlAssign<Self>
91    + core::ops::ShlAssign<i8>
92    + core::ops::ShlAssign<i16>
93    + core::ops::ShlAssign<i32>
94    + core::ops::ShlAssign<i64>
95    + core::ops::ShlAssign<i128>
96    + core::ops::ShlAssign<isize>
97    + core::ops::ShlAssign<u8>
98    + core::ops::ShlAssign<u16>
99    + core::ops::ShlAssign<u32>
100    + core::ops::ShlAssign<u64>
101    + core::ops::ShlAssign<u128>
102    + core::ops::ShlAssign<usize>
103    + core::ops::Shr<Self, Output = Self>
104    + core::ops::Shr<i8, Output = Self>
105    + core::ops::Shr<i16, Output = Self>
106    + core::ops::Shr<i32, Output = Self>
107    + core::ops::Shr<i64, Output = Self>
108    + core::ops::Shr<i128, Output = Self>
109    + core::ops::Shr<isize, Output = Self>
110    + core::ops::Shr<u8, Output = Self>
111    + core::ops::Shr<u16, Output = Self>
112    + core::ops::Shr<u32, Output = Self>
113    + core::ops::Shr<u64, Output = Self>
114    + core::ops::Shr<u128, Output = Self>
115    + core::ops::Shr<usize, Output = Self>
116    + core::ops::ShrAssign<Self>
117    + core::ops::ShrAssign<i8>
118    + core::ops::ShrAssign<i16>
119    + core::ops::ShrAssign<i32>
120    + core::ops::ShrAssign<i64>
121    + core::ops::ShrAssign<i128>
122    + core::ops::ShrAssign<isize>
123    + core::ops::ShrAssign<u8>
124    + core::ops::ShrAssign<u16>
125    + core::ops::ShrAssign<u32>
126    + core::ops::ShrAssign<u64>
127    + core::ops::ShrAssign<u128>
128    + core::ops::ShrAssign<usize>
129    + for<'a> core::ops::BitAnd<&'a Self, Output = Self>
130    + for<'a> core::ops::BitAndAssign<&'a Self>
131    + for<'a> core::ops::BitOr<&'a Self, Output = Self>
132    + for<'a> core::ops::BitOrAssign<&'a Self>
133    + for<'a> core::ops::BitXor<&'a Self, Output = Self>
134    + for<'a> core::ops::BitXorAssign<&'a Self>
135    + for<'a> core::ops::Shl<&'a Self, Output = Self>
136    + for<'a> core::ops::Shl<&'a i8, Output = Self>
137    + for<'a> core::ops::Shl<&'a i16, Output = Self>
138    + for<'a> core::ops::Shl<&'a i32, Output = Self>
139    + for<'a> core::ops::Shl<&'a i64, Output = Self>
140    + for<'a> core::ops::Shl<&'a i128, Output = Self>
141    + for<'a> core::ops::Shl<&'a isize, Output = Self>
142    + for<'a> core::ops::Shl<&'a u8, Output = Self>
143    + for<'a> core::ops::Shl<&'a u16, Output = Self>
144    + for<'a> core::ops::Shl<&'a u32, Output = Self>
145    + for<'a> core::ops::Shl<&'a u64, Output = Self>
146    + for<'a> core::ops::Shl<&'a u128, Output = Self>
147    + for<'a> core::ops::Shl<&'a usize, Output = Self>
148    + for<'a> core::ops::ShlAssign<&'a Self>
149    + for<'a> core::ops::ShlAssign<&'a i8>
150    + for<'a> core::ops::ShlAssign<&'a i16>
151    + for<'a> core::ops::ShlAssign<&'a i32>
152    + for<'a> core::ops::ShlAssign<&'a i64>
153    + for<'a> core::ops::ShlAssign<&'a i128>
154    + for<'a> core::ops::ShlAssign<&'a isize>
155    + for<'a> core::ops::ShlAssign<&'a u8>
156    + for<'a> core::ops::ShlAssign<&'a u16>
157    + for<'a> core::ops::ShlAssign<&'a u32>
158    + for<'a> core::ops::ShlAssign<&'a u64>
159    + for<'a> core::ops::ShlAssign<&'a u128>
160    + for<'a> core::ops::ShlAssign<&'a usize>
161    + for<'a> core::ops::Shr<&'a Self, Output = Self>
162    + for<'a> core::ops::Shr<&'a i8, Output = Self>
163    + for<'a> core::ops::Shr<&'a i16, Output = Self>
164    + for<'a> core::ops::Shr<&'a i32, Output = Self>
165    + for<'a> core::ops::Shr<&'a i64, Output = Self>
166    + for<'a> core::ops::Shr<&'a i128, Output = Self>
167    + for<'a> core::ops::Shr<&'a isize, Output = Self>
168    + for<'a> core::ops::Shr<&'a u8, Output = Self>
169    + for<'a> core::ops::Shr<&'a u16, Output = Self>
170    + for<'a> core::ops::Shr<&'a u32, Output = Self>
171    + for<'a> core::ops::Shr<&'a u64, Output = Self>
172    + for<'a> core::ops::Shr<&'a u128, Output = Self>
173    + for<'a> core::ops::Shr<&'a usize, Output = Self>
174    + for<'a> core::ops::ShrAssign<&'a Self>
175    + for<'a> core::ops::ShrAssign<&'a i8>
176    + for<'a> core::ops::ShrAssign<&'a i16>
177    + for<'a> core::ops::ShrAssign<&'a i32>
178    + for<'a> core::ops::ShrAssign<&'a i64>
179    + for<'a> core::ops::ShrAssign<&'a i128>
180    + for<'a> core::ops::ShrAssign<&'a isize>
181    + for<'a> core::ops::ShrAssign<&'a u8>
182    + for<'a> core::ops::ShrAssign<&'a u16>
183    + for<'a> core::ops::ShrAssign<&'a u32>
184    + for<'a> core::ops::ShrAssign<&'a u64>
185    + for<'a> core::ops::ShrAssign<&'a u128>
186    + for<'a> core::ops::ShrAssign<&'a usize>
187{
188    /// The size of this integer type in bits.
189    const BITS: u32;
190
191    /// The largest value that can be represented by this integer type.
192    const MAX: Self;
193
194    /// The smallest value that can be represented by this integer type.
195    const MIN: Self;
196
197    /// Checked integer addition. Computes `self + rhs`, returning `None` if overflow occurred.
198    fn checked_add(self, rhs: Self) -> Option<Self>;
199
200    /// Checked integer division. Computes `self / rhs`, returning `None` if `rhs == 0` or the
201    /// division results in overflow.
202    fn checked_div(self, rhs: Self) -> Option<Self>;
203
204    /// Checked Euclidean division. Computes `self.div_euclid(rhs)`, returning `None` if `rhs == 0`
205    /// or the division results in overflow.
206    fn checked_div_euclid(self, rhs: Self) -> Option<Self>;
207
208    /// Returns the logarithm of the number with respect to an arbitrary base, rounded down.
209    /// Returns `None` if the number is negative or zero, or if the base is not at least 2.
210    fn checked_ilog(self, base: Self) -> Option<u32>;
211
212    /// Returns the base 10 logarithm of the number, rounded down. Returns `None` if the number is
213    /// negative or zero.
214    fn checked_ilog10(self) -> Option<u32>;
215
216    /// Returns the base 2 logarithm of the number, rounded down. Returns `None` if the number is
217    /// negative or zero.
218    fn checked_ilog2(self) -> Option<u32>;
219
220    /// Checked integer multiplication. Computes `self * rhs`, returning `None` if overflow
221    /// occurred.
222    fn checked_mul(self, rhs: Self) -> Option<Self>;
223
224    /// Checked negation. Computes -self, returning `None` if `self == MIN`.
225    fn checked_neg(self) -> Option<Self>;
226
227    /// Checked exponentiation. Computes `self.pow(exp)`, returning `None` if overflow occurred.
228    fn checked_pow(self, exp: u32) -> Option<Self>;
229
230    /// Checked integer remainder. Computes `self % rhs`, returning `None` if `rhs == 0` or the
231    /// division results in overflow.
232    fn checked_rem(self, rhs: Self) -> Option<Self>;
233
234    /// Checked Euclidean remainder. Computes `self.rem_euclid(rhs)`, returning `None` if `rhs ==
235    /// 0` or the division results in overflow.
236    fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;
237
238    /// Checked shift left. Computes `self << rhs`, returning `None` if `rhs` is larger than or
239    /// equal to the number of bits in `self`.
240    fn checked_shl(self, rhs: u32) -> Option<Self>;
241
242    /// Checked shift right. Computes `self >> rhs`, returning `None` if `rhs` is larger than or
243    /// equal to the number of bits in `self`.
244    fn checked_shr(self, rhs: u32) -> Option<Self>;
245
246    /// Checked integer subtraction. Computes `self - rhs`, returning `None` if overflow occurred.
247    fn checked_sub(self, rhs: Self) -> Option<Self>;
248
249    /// Returns the number of ones in the binary representation of `self`.
250    fn count_ones(self) -> u32;
251
252    /// Returns the number of zeros in the binary representation of `self`.
253    fn count_zeros(self) -> u32;
254
255    /// Calculates the quotient of Euclidean division of `self` by `rhs`. This computes the integer
256    /// `q` such that `self = q * rhs + r`, with `r = self.rem_euclid(rhs)` and `0 <= r <
257    /// abs(rhs)`.
258    fn div_euclid(self, rhs: Self) -> Self;
259
260    /// Converts an integer from big endian to the target's endianness.
261    fn from_be(value: Self) -> Self;
262
263    /// Converts an integer from little endian to the target's endianness.
264    fn from_le(value: Self) -> Self;
265
266    /// Returns the logarithm of the number with respect to an arbitrary base, rounded down.
267    fn ilog(self, base: Self) -> u32;
268
269    /// Returns the base 10 logarithm of the number, rounded down.
270    fn ilog10(self) -> u32;
271
272    /// Returns the base 2 logarithm of the number, rounded down.
273    fn ilog2(self) -> u32;
274
275    /// Returns the square root of the number, rounded down.
276    fn isqrt(self) -> Self;
277
278    /// Returns the number of leading ones in the binary representation of `self`.
279    fn leading_ones(self) -> u32;
280
281    /// Returns the number of leading zeros in the binary representation of `self`.
282    fn leading_zeros(self) -> u32;
283
284    /// Calculates `self + rhs`. Returns a tuple of the addition along with a boolean indicating
285    /// whether an arithmetic overflow would occur.
286    fn overflowing_add(self, rhs: Self) -> (Self, bool);
287
288    /// Calculates the divisor when `self` is divided by `rhs`. Returns a tuple of the divisor
289    /// along with a boolean indicating whether an arithmetic overflow would occur.
290    fn overflowing_div(self, rhs: Self) -> (Self, bool);
291
292    /// Calculates the quotient of Euclidean division `self`.div_euclid(rhs). Returns a tuple of
293    /// the divisor along with a boolean indicating whether an arithmetic overflow would occur.
294    fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);
295
296    /// Calculates the multiplication of `self` and `rhs`. Returns a tuple of the multiplication
297    /// along with a boolean indicating whether an arithmetic overflow would occur.
298    fn overflowing_mul(self, rhs: Self) -> (Self, bool);
299
300    /// Negates self, overflowing if this is equal to the minimum value. Returns a tuple of the
301    /// negated version of `self` along with a boolean indicating whether an overflow happened.
302    fn overflowing_neg(self) -> (Self, bool);
303
304    /// Raises `self` to the power of `exp`, using exponentiation by squaring. Returns a tuple of
305    /// the exponentiation along with a bool indicating whether an overflow happened.
306    fn overflowing_pow(self, exp: u32) -> (Self, bool);
307
308    /// Calculates the remainder when `self` is divided by `rhs`. Returns a tuple of the remainder
309    /// after dividing along with a boolean indicating whether an arithmetic overflow would occur.
310    fn overflowing_rem(self, rhs: Self) -> (Self, bool);
311
312    /// Overflowing Euclidean remainder. Calculates `self`.rem_euclid(rhs). Returns a tuple of the
313    /// remainder after dividing along with a boolean indicating whether an arithmetic overflow
314    /// would occur.
315    fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool);
316
317    /// Shifts `self` left by `rhs` bits. Returns a tuple of the shifted version of `self` along
318    /// with a boolean indicating whether the shift value was larger than or equal to the number of
319    /// bits.
320    fn overflowing_shl(self, rhs: u32) -> (Self, bool);
321
322    /// Shifts `self` right by `rhs` bits. Returns a tuple of the shifted version of `self` along
323    /// with a boolean indicating whether the shift value was larger than or equal to the number of
324    /// bits.
325    fn overflowing_shr(self, rhs: u32) -> (Self, bool);
326
327    /// Calculates `self - rhs`. Returns a tuple of the subtraction along with a boolean indicating
328    /// whether an arithmetic overflow would occur.
329    fn overflowing_sub(self, rhs: Self) -> (Self, bool);
330
331    /// Raises `self` to the power of `exp`, using exponentiation by squaring.
332    fn pow(self, exp: u32) -> Self;
333
334    /// Calculates the least nonnegative remainder of `self (mod rhs)`. This is done as if by the
335    /// Euclidean division algorithm – given `r = self.rem_euclid(rhs)`, the result satisfies `self
336    /// = rhs * self.div_euclid(rhs) + r` and `0 <= r < abs(rhs)`.
337    fn rem_euclid(self, rhs: Self) -> Self;
338
339    /// Reverses the order of bits in the integer.
340    fn reverse_bits(self) -> Self;
341
342    /// Shifts the bits to the left by a specified amount, n, wrapping the truncated bits to the
343    /// end of the resulting integer.
344    fn rotate_left(self, n: u32) -> Self;
345
346    /// Shifts the bits to the right by a specified amount, n, wrapping the truncated bits to the
347    /// beginning of the resulting integer.
348    fn rotate_right(self, n: u32) -> Self;
349
350    /// Saturating integer addition. Computes `self + rhs`, saturating at the numeric bounds
351    /// instead of overflowing.
352    fn saturating_add(self, rhs: Self) -> Self;
353
354    /// Saturating integer division. Computes `self / rhs`, saturating at the numeric bounds
355    /// instead of overflowing.
356    fn saturating_div(self, rhs: Self) -> Self;
357
358    /// Saturating integer multiplication. Computes `self * rhs`, saturating at the numeric bounds
359    /// instead of overflowing.
360    fn saturating_mul(self, rhs: Self) -> Self;
361
362    /// Saturating integer exponentiation. Computes `self.pow(exp)`, saturating at the numeric
363    /// bounds instead of overflowing.
364    fn saturating_pow(self, exp: u32) -> Self;
365
366    /// Saturating integer subtraction. Computes `self - rhs`, saturating at the numeric bounds
367    /// instead of overflowing.
368    fn saturating_sub(self, rhs: Self) -> Self;
369
370    /// Reverses the byte order of the integer.
371    fn swap_bytes(self) -> Self;
372
373    /// Converts `self` to big endian from the target's endianness.
374    fn to_be(self) -> Self;
375
376    /// Converts `self` to little endian from the target's endianness.
377    fn to_le(self) -> Self;
378
379    /// Returns the number of trailing ones in the binary representation of `self`.
380    fn trailing_ones(self) -> u32;
381
382    /// Returns the number of trailing zeros in the binary representation of `self`.
383    fn trailing_zeros(self) -> u32;
384
385    /// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the boundary of the
386    /// type.
387    fn wrapping_add(self, rhs: Self) -> Self;
388
389    /// Wrapping (modular) division. Computes `self / rhs`, wrapping around at the boundary of the
390    /// type.
391    fn wrapping_div(self, rhs: Self) -> Self;
392
393    /// Wrapping Euclidean division. Computes `self.div_euclid(rhs)`, wrapping around at the
394    /// boundary of the type.
395    fn wrapping_div_euclid(self, rhs: Self) -> Self;
396
397    /// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at the boundary
398    /// of the type.
399    fn wrapping_mul(self, rhs: Self) -> Self;
400
401    /// Wrapping (modular) negation. Computes `-self`, wrapping around at the boundary of the type.
402    fn wrapping_neg(self) -> Self;
403
404    /// Wrapping (modular) exponentiation. Computes `self.pow(exp)`, wrapping around at the
405    /// boundary of the type.
406    fn wrapping_pow(self, exp: u32) -> Self;
407
408    /// Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at the boundary of the
409    /// type.
410    fn wrapping_rem(self, rhs: Self) -> Self;
411
412    /// Wrapping Euclidean remainder. Computes `self.rem_euclid(rhs)`, wrapping around at the
413    /// boundary of the type.
414    fn wrapping_rem_euclid(self, rhs: Self) -> Self;
415
416    /// Panic-free bitwise shift-left; yields `self << mask(rhs)`, where mask removes any
417    /// high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
418    fn wrapping_shl(self, rhs: u32) -> Self;
419
420    /// Panic-free bitwise shift-right; yields `self >> mask(rhs)`, where mask removes any
421    /// high-order bits of `rhs` that would cause the shift to exceed the bitwidth of the type.
422    fn wrapping_shr(self, rhs: u32) -> Self;
423
424    /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the boundary of
425    /// the type.
426    fn wrapping_sub(self, rhs: Self) -> Self;
427
428    /// Unchecked integer addition. Computes `self + rhs`, assuming overflow cannot occur.
429    ///
430    /// # Safety
431    ///
432    /// This results in undefined behavior when `self + rhs > Self::MAX` or `self + rhs <
433    /// Self::MIN`, i.e. when [`checked_add`][Self::checked_add] would return `None`.
434    unsafe fn unchecked_add(self, rhs: Self) -> Self;
435
436    /// Unchecked integer multiplication. Computes `self * rhs`, assuming overflow cannot occur.
437    ///
438    /// # Safety
439    ///
440    /// This results in undefined behavior when `self * rhs > Self::MAX` or `self * rhs <
441    /// Self::MIN`, i.e. when [`checked_mul`][Self::checked_mul] would return `None`.
442    unsafe fn unchecked_mul(self, rhs: Self) -> Self;
443
444    /// Unchecked integer subtraction. Computes `self - rhs`, assuming overflow cannot occur.
445    ///
446    /// # Safety
447    ///
448    /// This results in undefined behavior when `self - rhs > Self::MAX` or `self - rhs <
449    /// Self::MIN`, i.e. when [`checked_sub`][Self::checked_sub] would return `None`.
450    unsafe fn unchecked_sub(self, rhs: Self) -> Self;
451}
452
453/// Trait for references to primitive integer types ([`PrimitiveInteger`]).
454///
455/// This enables traits like the standard operators in generic code,
456/// e.g. `where &T: PrimitiveIntegerRef<T>`.
457pub trait PrimitiveIntegerRef<T>:
458    PrimitiveNumberRef<T>
459    + core::cmp::Eq
460    + core::cmp::Ord
461    + core::fmt::Binary
462    + core::fmt::LowerHex
463    + core::fmt::Octal
464    + core::fmt::UpperHex
465    + core::hash::Hash
466    + core::ops::BitAnd<T, Output = T>
467    + core::ops::BitOr<T, Output = T>
468    + core::ops::BitXor<T, Output = T>
469    + core::ops::Not<Output = T>
470    + core::ops::Shl<T, Output = T>
471    + core::ops::Shl<i8, Output = T>
472    + core::ops::Shl<i16, Output = T>
473    + core::ops::Shl<i32, Output = T>
474    + core::ops::Shl<i64, Output = T>
475    + core::ops::Shl<i128, Output = T>
476    + core::ops::Shl<isize, Output = T>
477    + core::ops::Shl<u8, Output = T>
478    + core::ops::Shl<u16, Output = T>
479    + core::ops::Shl<u32, Output = T>
480    + core::ops::Shl<u64, Output = T>
481    + core::ops::Shl<u128, Output = T>
482    + core::ops::Shl<usize, Output = T>
483    + core::ops::Shr<T, Output = T>
484    + core::ops::Shr<i8, Output = T>
485    + core::ops::Shr<i16, Output = T>
486    + core::ops::Shr<i32, Output = T>
487    + core::ops::Shr<i64, Output = T>
488    + core::ops::Shr<i128, Output = T>
489    + core::ops::Shr<isize, Output = T>
490    + core::ops::Shr<u8, Output = T>
491    + core::ops::Shr<u16, Output = T>
492    + core::ops::Shr<u32, Output = T>
493    + core::ops::Shr<u64, Output = T>
494    + core::ops::Shr<u128, Output = T>
495    + core::ops::Shr<usize, Output = T>
496    + for<'a> core::ops::BitAnd<&'a T, Output = T>
497    + for<'a> core::ops::BitOr<&'a T, Output = T>
498    + for<'a> core::ops::BitXor<&'a T, Output = T>
499    + for<'a> core::ops::Shl<&'a T, Output = T>
500    + for<'a> core::ops::Shl<&'a i8, Output = T>
501    + for<'a> core::ops::Shl<&'a i16, Output = T>
502    + for<'a> core::ops::Shl<&'a i32, Output = T>
503    + for<'a> core::ops::Shl<&'a i64, Output = T>
504    + for<'a> core::ops::Shl<&'a i128, Output = T>
505    + for<'a> core::ops::Shl<&'a isize, Output = T>
506    + for<'a> core::ops::Shl<&'a u8, Output = T>
507    + for<'a> core::ops::Shl<&'a u16, Output = T>
508    + for<'a> core::ops::Shl<&'a u32, Output = T>
509    + for<'a> core::ops::Shl<&'a u64, Output = T>
510    + for<'a> core::ops::Shl<&'a u128, Output = T>
511    + for<'a> core::ops::Shl<&'a usize, Output = T>
512    + for<'a> core::ops::Shr<&'a T, Output = T>
513    + for<'a> core::ops::Shr<&'a i8, Output = T>
514    + for<'a> core::ops::Shr<&'a i16, Output = T>
515    + for<'a> core::ops::Shr<&'a i32, Output = T>
516    + for<'a> core::ops::Shr<&'a i64, Output = T>
517    + for<'a> core::ops::Shr<&'a i128, Output = T>
518    + for<'a> core::ops::Shr<&'a isize, Output = T>
519    + for<'a> core::ops::Shr<&'a u8, Output = T>
520    + for<'a> core::ops::Shr<&'a u16, Output = T>
521    + for<'a> core::ops::Shr<&'a u32, Output = T>
522    + for<'a> core::ops::Shr<&'a u64, Output = T>
523    + for<'a> core::ops::Shr<&'a u128, Output = T>
524    + for<'a> core::ops::Shr<&'a usize, Output = T>
525{
526}
527
528macro_rules! impl_integer {
529    ($($Integer:ident),*) => {$(
530        impl PrimitiveInteger for $Integer {
531            use_consts!(Self::{
532                BITS: u32,
533                MAX: Self,
534                MIN: Self,
535            });
536
537            forward! {
538                fn from_be(value: Self) -> Self;
539                fn from_le(value: Self) -> Self;
540            }
541            forward! {
542                fn checked_add(self, rhs: Self) -> Option<Self>;
543                fn checked_div(self, rhs: Self) -> Option<Self>;
544                fn checked_div_euclid(self, rhs: Self) -> Option<Self>;
545                fn checked_ilog(self, base: Self) -> Option<u32>;
546                fn checked_ilog10(self) -> Option<u32>;
547                fn checked_ilog2(self) -> Option<u32>;
548                fn checked_mul(self, rhs: Self) -> Option<Self>;
549                fn checked_neg(self) -> Option<Self>;
550                fn checked_pow(self, exp: u32) -> Option<Self>;
551                fn checked_rem(self, rhs: Self) -> Option<Self>;
552                fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;
553                fn checked_shl(self, rhs: u32) -> Option<Self>;
554                fn checked_shr(self, rhs: u32) -> Option<Self>;
555                fn checked_sub(self, rhs: Self) -> Option<Self>;
556                fn count_ones(self) -> u32;
557                fn count_zeros(self) -> u32;
558                fn div_euclid(self, rhs: Self) -> Self;
559                fn ilog(self, base: Self) -> u32;
560                fn ilog10(self) -> u32;
561                fn ilog2(self) -> u32;
562                fn isqrt(self) -> Self;
563                fn leading_ones(self) -> u32;
564                fn leading_zeros(self) -> u32;
565                fn overflowing_add(self, rhs: Self) -> (Self, bool);
566                fn overflowing_div(self, rhs: Self) -> (Self, bool);
567                fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);
568                fn overflowing_mul(self, rhs: Self) -> (Self, bool);
569                fn overflowing_neg(self) -> (Self, bool);
570                fn overflowing_pow(self, exp: u32) -> (Self, bool);
571                fn overflowing_rem(self, rhs: Self) -> (Self, bool);
572                fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool);
573                fn overflowing_shl(self, rhs: u32) -> (Self, bool);
574                fn overflowing_shr(self, rhs: u32) -> (Self, bool);
575                fn overflowing_sub(self, rhs: Self) -> (Self, bool);
576                fn pow(self, exp: u32) -> Self;
577                fn rem_euclid(self, rhs: Self) -> Self;
578                fn reverse_bits(self) -> Self;
579                fn rotate_left(self, n: u32) -> Self;
580                fn rotate_right(self, n: u32) -> Self;
581                fn saturating_add(self, rhs: Self) -> Self;
582                fn saturating_div(self, rhs: Self) -> Self;
583                fn saturating_mul(self, rhs: Self) -> Self;
584                fn saturating_pow(self, exp: u32) -> Self;
585                fn saturating_sub(self, rhs: Self) -> Self;
586                fn swap_bytes(self) -> Self;
587                fn to_be(self) -> Self;
588                fn to_le(self) -> Self;
589                fn trailing_ones(self) -> u32;
590                fn trailing_zeros(self) -> u32;
591                fn wrapping_add(self, rhs: Self) -> Self;
592                fn wrapping_div(self, rhs: Self) -> Self;
593                fn wrapping_div_euclid(self, rhs: Self) -> Self;
594                fn wrapping_mul(self, rhs: Self) -> Self;
595                fn wrapping_neg(self) -> Self;
596                fn wrapping_pow(self, exp: u32) -> Self;
597                fn wrapping_rem(self, rhs: Self) -> Self;
598                fn wrapping_rem_euclid(self, rhs: Self) -> Self;
599                fn wrapping_shl(self, rhs: u32) -> Self;
600                fn wrapping_shr(self, rhs: u32) -> Self;
601                fn wrapping_sub(self, rhs: Self) -> Self;
602            }
603            forward! {
604                unsafe fn unchecked_add(self, rhs: Self) -> Self;
605                unsafe fn unchecked_mul(self, rhs: Self) -> Self;
606                unsafe fn unchecked_sub(self, rhs: Self) -> Self;
607            }
608        }
609
610        impl PrimitiveIntegerRef<$Integer> for &$Integer {}
611    )*}
612}
613
614impl_integer!(i8, i16, i32, i64, i128, isize);
615impl_integer!(u8, u16, u32, u64, u128, usize);