num_primitive/
integer.rs

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