1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/// Wrapping operations.
pub trait WrappingOps {
/// Wrapping (modular) addition. Computes `self + rhs`, wrapping around at the
/// boundary of the type.
fn wrapping_add(self, rhs: Self) -> Self;
/// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the
/// boundary of the type.
fn wrapping_sub(self, rhs: Self) -> Self;
/// Wrapping (modular) multiplication. Computes `self * rhs`, wrapping around at
/// the boundary of the type.
fn wrapping_mul(self, rhs: Self) -> Self;
/// Wrapping (modular) division. Computes `self / rhs`, wrapping around at the
/// boundary of the type.
///
/// The only case where such wrapping can occur is when one divides `MIN / -1` on a signed type (where
/// `MIN` is the negative minimal value for the type); this is equivalent to `-MIN`, a positive value
/// that is too large to represent in the type. In such a case, this function returns `MIN` itself.
///
/// # Panics
///
/// This function will panic if `rhs` is 0.
fn wrapping_div(self, rhs: Self) -> Self;
/// Wrapping (modular) remainder. Computes `self % rhs`, wrapping around at the
/// boundary of the type.
///
/// Such wrap-around never actually occurs mathematically; implementation artifacts make `x % y`
/// invalid for `MIN / -1` on a signed type (where `MIN` is the negative minimal value). In such a case,
/// this function returns `0`.
///
/// # Panics
///
/// This function will panic if `rhs` is 0.
fn wrapping_rem(self, rhs: Self) -> Self;
}
/// Implements [`WrappingOps`].
macro_rules! impl_wrapping {
($($t:ty),*) => {
$(impl WrappingOps for $t {
#[inline] fn wrapping_add(self, rhs: Self) -> Self { self.wrapping_add(rhs) }
#[inline] fn wrapping_sub(self, rhs: Self) -> Self { self.wrapping_sub(rhs) }
#[inline] fn wrapping_mul(self, rhs: Self) -> Self { self.wrapping_mul(rhs) }
#[inline] fn wrapping_div(self, rhs: Self) -> Self { self.wrapping_div(rhs) }
#[inline] fn wrapping_rem(self, rhs: Self) -> Self { self.wrapping_rem(rhs) }
})*
};
}
impl_wrapping!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize);