num_primitive/
unsigned.rs

1use crate::{PrimitiveInteger, PrimitiveIntegerRef, PrimitiveSigned};
2
3/// Trait for all primitive [unsigned integer types], including the supertraits
4/// [`PrimitiveInteger`] and [`PrimitiveNumber`][crate::PrimitiveNumber].
5///
6/// This encapsulates trait implementations and inherent methods that are common among all of the
7/// primitive unsigned integer types: [`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/// [unsigned integer types]: https://doc.rust-lang.org/reference/types/numeric.html#r-type.numeric.int.unsigned
16///
17/// # Examples
18///
19/// ```
20/// use num_primitive::PrimitiveUnsigned;
21///
22/// // Greatest Common Divisor (Euclidean algorithm)
23/// fn gcd<T: PrimitiveUnsigned>(mut a: T, mut b: T) -> T {
24///     let zero = T::from(0u8);
25///     while b != zero {
26///         (a, b) = (b, a % b);
27///     }
28///     a
29/// }
30///
31/// assert_eq!(gcd::<u8>(48, 18), 6);
32/// assert_eq!(gcd::<u16>(1071, 462), 21);
33/// assert_eq!(gcd::<u32>(6_700_417, 2_147_483_647), 1);
34/// ```
35pub trait PrimitiveUnsigned: PrimitiveInteger + From<u8> {
36    /// The signed integer type used by methods like
37    /// [`checked_add_signed`][Self::checked_add_signed].
38    type Signed: PrimitiveSigned;
39
40    /// Computes the absolute difference between `self` and `other`.
41    fn abs_diff(self, other: Self) -> Self;
42
43    /// Returns the bit pattern of `self` reinterpreted as a signed integer of the same size.
44    fn cast_signed(self) -> Self::Signed;
45
46    /// Checked addition with a signed integer. Computes `self + rhs`, returning `None` if overflow
47    /// occurred.
48    fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
49
50    /// Calculates the smallest value greater than or equal to `self` that is a multiple of `rhs`.
51    /// Returns `None` if `rhs` is zero or the operation would result in overflow.
52    fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>;
53
54    /// Returns the smallest power of two greater than or equal to `self`. If the next power of two
55    /// is greater than the type's maximum value, `None` is returned, otherwise the power of two is
56    /// wrapped in Some.
57    fn checked_next_power_of_two(self) -> Option<Self>;
58
59    /// Calculates the quotient of `self` and rhs, rounding the result towards positive infinity.
60    fn div_ceil(self, rhs: Self) -> Self;
61
62    /// Returns `true` if `self` is an integer multiple of `rhs`, and false otherwise.
63    fn is_multiple_of(self, rhs: Self) -> bool;
64
65    /// Returns `true` if and only if `self == 2^k` for some `k`.
66    fn is_power_of_two(self) -> bool;
67
68    /// Calculates the middle point of `self` and `other`.
69    fn midpoint(self, other: Self) -> Self;
70
71    /// Calculates the smallest value greater than or equal to `self` that is a multiple of `rhs`.
72    fn next_multiple_of(self, rhs: Self) -> Self;
73
74    /// Returns the smallest power of two greater than or equal to `self`.
75    fn next_power_of_two(self) -> Self;
76
77    /// Calculates `self + rhs` with a signed `rhs`. Returns a tuple of the addition along with a
78    /// boolean indicating whether an arithmetic overflow would occur.
79    fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
80
81    /// Saturating addition with a signed integer. Computes `self + rhs`, saturating at the numeric
82    /// bounds instead of overflowing.
83    fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
84
85    /// Wrapping (modular) addition with a signed integer. Computes `self + rhs`, wrapping around
86    /// at the boundary of the type.
87    fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
88}
89
90/// Trait for references to primitive unsigned integer types ([`PrimitiveUnsigned`]).
91///
92/// This enables traits like the standard operators in generic code,
93/// e.g. `where &T: PrimitiveUnsignedRef<T>`.
94pub trait PrimitiveUnsignedRef<T>: PrimitiveIntegerRef<T> {}
95
96macro_rules! impl_unsigned {
97    ($Unsigned:ident, $Signed:ty) => {
98        impl PrimitiveUnsigned for $Unsigned {
99            type Signed = $Signed;
100
101            forward! {
102                fn abs_diff(self, other: Self) -> Self;
103                fn cast_signed(self) -> Self::Signed;
104                fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
105                fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>;
106                fn checked_next_power_of_two(self) -> Option<Self>;
107                fn div_ceil(self, rhs: Self) -> Self;
108                fn is_multiple_of(self, rhs: Self) -> bool;
109                fn is_power_of_two(self) -> bool;
110                fn midpoint(self, other: Self) -> Self;
111                fn next_multiple_of(self, rhs: Self) -> Self;
112                fn next_power_of_two(self) -> Self;
113                fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
114                fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
115                fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
116            }
117        }
118
119        impl PrimitiveUnsignedRef<$Unsigned> for &$Unsigned {}
120    };
121}
122
123impl_unsigned!(u8, i8);
124impl_unsigned!(u16, i16);
125impl_unsigned!(u32, i32);
126impl_unsigned!(u64, i64);
127impl_unsigned!(u128, i128);
128impl_unsigned!(usize, isize);