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 smallest value greater than or equal to `self` that is a multiple of `rhs`.
69 fn next_multiple_of(self, rhs: Self) -> Self;
70
71 /// Returns the smallest power of two greater than or equal to `self`.
72 fn next_power_of_two(self) -> Self;
73
74 /// Calculates `self + rhs` with a signed `rhs`. Returns a tuple of the addition along with a
75 /// boolean indicating whether an arithmetic overflow would occur.
76 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
77
78 /// Saturating addition with a signed integer. Computes `self + rhs`, saturating at the numeric
79 /// bounds instead of overflowing.
80 fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
81
82 /// Wrapping (modular) addition with a signed integer. Computes `self + rhs`, wrapping around
83 /// at the boundary of the type.
84 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
85}
86
87/// Trait for references to primitive unsigned integer types ([`PrimitiveUnsigned`]).
88///
89/// This enables traits like the standard operators in generic code,
90/// e.g. `where &T: PrimitiveUnsignedRef<T>`.
91pub trait PrimitiveUnsignedRef<T>: PrimitiveIntegerRef<T> {}
92
93macro_rules! impl_unsigned {
94 ($Unsigned:ident, $Signed:ty) => {
95 impl PrimitiveUnsigned for $Unsigned {
96 type Signed = $Signed;
97
98 forward! {
99 fn abs_diff(self, other: Self) -> Self;
100 fn cast_signed(self) -> Self::Signed;
101 fn checked_add_signed(self, rhs: Self::Signed) -> Option<Self>;
102 fn checked_next_multiple_of(self, rhs: Self) -> Option<Self>;
103 fn checked_next_power_of_two(self) -> Option<Self>;
104 fn div_ceil(self, rhs: Self) -> Self;
105 fn is_multiple_of(self, rhs: Self) -> bool;
106 fn is_power_of_two(self) -> bool;
107 fn next_multiple_of(self, rhs: Self) -> Self;
108 fn next_power_of_two(self) -> Self;
109 fn overflowing_add_signed(self, rhs: Self::Signed) -> (Self, bool);
110 fn saturating_add_signed(self, rhs: Self::Signed) -> Self;
111 fn wrapping_add_signed(self, rhs: Self::Signed) -> Self;
112 }
113 }
114
115 impl PrimitiveUnsignedRef<$Unsigned> for &$Unsigned {}
116 };
117}
118
119impl_unsigned!(u8, i8);
120impl_unsigned!(u16, i16);
121impl_unsigned!(u32, i32);
122impl_unsigned!(u64, i64);
123impl_unsigned!(u128, i128);
124impl_unsigned!(usize, isize);