pub trait IsInteger: IsNumber + Hash + Eq + Ord + Binary + LowerHex + UpperHex + Octal + BitAnd<Self, Output = Self> + for<'a> BitAnd<&'a Self, Output = Self> + BitAndAssign<Self> + for<'a> BitAndAssign<&'a Self> + BitOr<Self, Output = Self> + for<'a> BitOr<&'a Self, Output = Self> + BitOrAssign<Self> + for<'a> BitOrAssign<&'a Self> + BitXor<Self, Output = Self> + for<'a> BitXor<&'a Self, Output = Self> + BitXorAssign<Self> + for<'a> BitXorAssign<&'a Self> + Not<Output = Self> + TryFrom<i8> + TryFrom<u8> + TryFrom<i16> + TryFrom<u16> + TryFrom<i32> + TryFrom<u32> + TryFrom<i64> + TryFrom<u64> + TryFrom<i128> + TryFrom<u128> + TryFrom<isize> + TryFrom<usize> + TryInto<i8> + TryInto<u8> + TryInto<i16> + TryInto<u16> + TryInto<i32> + TryInto<u32> + TryInto<i64> + TryInto<u64> + TryInto<i128> + TryInto<u128> + TryInto<isize> + TryInto<usize> + Shl<i8, Output = Self> + for<'a> Shl<&'a i8, Output = Self> + ShlAssign<i8> + for<'a> ShlAssign<&'a i8> + Shr<i8, Output = Self> + for<'a> Shr<&'a i8, Output = Self> + ShrAssign<i8> + for<'a> ShrAssign<&'a i8> + Shl<u8, Output = Self> + for<'a> Shl<&'a u8, Output = Self> + ShlAssign<u8> + for<'a> ShlAssign<&'a u8> + Shr<u8, Output = Self> + for<'a> Shr<&'a u8, Output = Self> + ShrAssign<u8> + for<'a> ShrAssign<&'a u8> + Shl<i16, Output = Self> + for<'a> Shl<&'a i16, Output = Self> + ShlAssign<i16> + for<'a> ShlAssign<&'a i16> + Shr<i16, Output = Self> + for<'a> Shr<&'a i16, Output = Self> + ShrAssign<i16> + for<'a> ShrAssign<&'a i16> + Shl<u16, Output = Self> + for<'a> Shl<&'a u16, Output = Self> + ShlAssign<u16> + for<'a> ShlAssign<&'a u16> + Shr<u16, Output = Self> + for<'a> Shr<&'a u16, Output = Self> + ShrAssign<u16> + for<'a> ShrAssign<&'a u16> + Shl<i32, Output = Self> + for<'a> Shl<&'a i32, Output = Self> + ShlAssign<i32> + for<'a> ShlAssign<&'a i32> + Shr<i32, Output = Self> + for<'a> Shr<&'a i32, Output = Self> + ShrAssign<i32> + for<'a> ShrAssign<&'a i32> + Shl<u32, Output = Self> + for<'a> Shl<&'a u32, Output = Self> + ShlAssign<u32> + for<'a> ShlAssign<&'a u32> + Shr<u32, Output = Self> + for<'a> Shr<&'a u32, Output = Self> + ShrAssign<u32> + for<'a> ShrAssign<&'a u32> + Shl<i64, Output = Self> + for<'a> Shl<&'a i64, Output = Self> + ShlAssign<i64> + for<'a> ShlAssign<&'a i64> + Shr<i64, Output = Self> + for<'a> Shr<&'a i64, Output = Self> + ShrAssign<i64> + for<'a> ShrAssign<&'a i64> + Shl<u64, Output = Self> + for<'a> Shl<&'a u64, Output = Self> + ShlAssign<u64> + for<'a> ShlAssign<&'a u64> + Shr<u64, Output = Self> + for<'a> Shr<&'a u64, Output = Self> + ShrAssign<u64> + for<'a> ShrAssign<&'a u64> + Shl<i128, Output = Self> + for<'a> Shl<&'a i128, Output = Self> + ShlAssign<i128> + for<'a> ShlAssign<&'a i128> + Shr<i128, Output = Self> + for<'a> Shr<&'a i128, Output = Self> + ShrAssign<i128> + for<'a> ShrAssign<&'a i128> + Shl<u128, Output = Self> + for<'a> Shl<&'a u128, Output = Self> + ShlAssign<u128> + for<'a> ShlAssign<&'a u128> + Shr<u128, Output = Self> + for<'a> Shr<&'a u128, Output = Self> + ShrAssign<u128> + for<'a> ShrAssign<&'a u128> + Shl<isize, Output = Self> + for<'a> Shl<&'a isize, Output = Self> + ShlAssign<isize> + for<'a> ShlAssign<&'a isize> + Shr<isize, Output = Self> + for<'a> Shr<&'a isize, Output = Self> + ShrAssign<isize> + for<'a> ShrAssign<&'a isize> + Shl<usize, Output = Self> + for<'a> Shl<&'a usize, Output = Self> + ShlAssign<usize> + for<'a> ShlAssign<&'a usize> + Shr<usize, Output = Self> + for<'a> Shr<&'a usize, Output = Self> + ShrAssign<usize> + for<'a> ShrAssign<&'a usize> {
const ZERO: Self;
const MIN: Self;
const MAX: Self;
Show 57 methods
fn min_value() -> Self;
fn max_value() -> Self;
fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>;
fn count_ones(self) -> u32;
fn count_zeros(self) -> u32;
fn leading_zeros(self) -> u32;
fn trailing_zeros(self) -> u32;
fn leading_ones(self) -> u32;
fn trailing_ones(self) -> u32;
fn rotate_left(self, n: u32) -> Self;
fn rotate_right(self, n: u32) -> Self;
fn swap_bytes(self) -> Self;
fn reverse_bits(self) -> Self;
fn from_be(self) -> Self;
fn from_le(self) -> Self;
fn to_be(self) -> Self;
fn to_le(self) -> Self;
fn checked_add(self, rhs: Self) -> Option<Self>;
fn checked_sub(self, rhs: Self) -> Option<Self>;
fn checked_mul(self, rhs: Self) -> Option<Self>;
fn checked_div(self, rhs: Self) -> Option<Self>;
fn checked_div_euclid(self, rhs: Self) -> Option<Self>;
fn checked_rem(self, rhs: Self) -> Option<Self>;
fn checked_rem_euclid(self, rhs: Self) -> Option<Self>;
fn checked_neg(self) -> Option<Self>;
fn checked_shl(self, rhs: u32) -> Option<Self>;
fn checked_shr(self, rhs: u32) -> Option<Self>;
fn checked_pow(self, rhs: u32) -> Option<Self>;
fn saturating_add(self, rhs: Self) -> Self;
fn saturating_sub(self, rhs: Self) -> Self;
fn saturating_mul(self, rhs: Self) -> Self;
fn saturating_pow(self, rhs: u32) -> Self;
fn wrapping_add(self, rhs: Self) -> Self;
fn wrapping_sub(self, rhs: Self) -> Self;
fn wrapping_mul(self, rhs: Self) -> Self;
fn wrapping_div(self, rhs: Self) -> Self;
fn wrapping_div_euclid(self, rhs: Self) -> Self;
fn wrapping_rem(self, rhs: Self) -> Self;
fn wrapping_rem_euclid(self, rhs: Self) -> Self;
fn wrapping_neg(self) -> Self;
fn wrapping_shl(self, rhs: u32) -> Self;
fn wrapping_shr(self, rhs: u32) -> Self;
fn wrapping_pow(self, rhs: u32) -> Self;
fn overflowing_add(self, rhs: Self) -> (Self, bool);
fn overflowing_sub(self, rhs: Self) -> (Self, bool);
fn overflowing_mul(self, rhs: Self) -> (Self, bool);
fn overflowing_div(self, rhs: Self) -> (Self, bool);
fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool);
fn overflowing_rem(self, rhs: Self) -> (Self, bool);
fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool);
fn overflowing_neg(self) -> (Self, bool);
fn overflowing_shl(self, rhs: u32) -> (Self, bool);
fn overflowing_shr(self, rhs: u32) -> (Self, bool);
fn overflowing_pow(self, rhs: u32) -> (Self, bool);
fn pow(self, rhs: u32) -> Self;
fn div_euclid(self, rhs: Self) -> Self;
fn rem_euclid(self, rhs: Self) -> Self;
}
Expand description
Declare that a type is a fixed-point integer.
This unifies all of the signed and unsigned integral types.
Associated Constants
Required methods
Returns the smallest value that can be represented by this integer type.
fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>
fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>
Converts a string slice in a given base to an integer.
The string is expected to be an optional +
or -
sign followed by
digits. Leading and trailing whitespace represent an error. Digits are a
subset of these characters, depending on radix
:
0-9
a-z
A-Z
Panics
This function panics if radix
is not in the range from 2 to 36.
fn count_ones(self) -> u32
fn count_ones(self) -> u32
Returns the number of ones in the binary representation of self
.
fn count_zeros(self) -> u32
fn count_zeros(self) -> u32
Returns the number of zeros in the binary representation of self
.
fn leading_zeros(self) -> u32
fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of
self
.
fn trailing_zeros(self) -> u32
fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation of
self
.
fn leading_ones(self) -> u32
fn leading_ones(self) -> u32
Returns the number of leading ones in the binary representation of
self
.
fn trailing_ones(self) -> u32
fn trailing_ones(self) -> u32
Returns the number of trailing ones in the binary representation of
self
.
fn rotate_left(self, n: u32) -> Self
fn rotate_left(self, n: u32) -> Self
Shifts the bits to the left by a specified amount, n
, wrapping the
truncated bits to the end of the resulting integer.
Please note this isn’t the same operation as the <<
shifting operator!
fn rotate_right(self, n: u32) -> Self
fn rotate_right(self, n: u32) -> Self
Shifts the bits to the right by a specified amount, n
, wrapping the
truncated bits to the beginning of the resulting integer.
Please note this isn’t the same operation as the >>
shifting operator!
fn swap_bytes(self) -> Self
fn swap_bytes(self) -> Self
Reverses the byte order of the integer.
fn reverse_bits(self) -> Self
fn reverse_bits(self) -> Self
Reverses the bit pattern of the integer.
Converts an integer from big endian to the target’s endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
Converts an integer frm little endian to the target’s endianness.
On little endian this is a no-op. On big endian the bytes are swapped.
Converts self
to big endian from the target’s endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
Converts self
to little endian from the target’s endianness.
On little endian this is a no-op. On big endian the bytes are swapped.
fn checked_add(self, rhs: Self) -> Option<Self>
fn checked_add(self, rhs: Self) -> Option<Self>
Checked integer addition. Computes self + rhs
, returning None
if
overflow occurred.
fn checked_sub(self, rhs: Self) -> Option<Self>
fn checked_sub(self, rhs: Self) -> Option<Self>
Checked integer subtraction. Computes self - rhs
, returning None
if
overflow occurred.
fn checked_mul(self, rhs: Self) -> Option<Self>
fn checked_mul(self, rhs: Self) -> Option<Self>
Checked integer multiplication. Computes self * rhs
, returning None
if overflow occurred.
fn checked_div(self, rhs: Self) -> Option<Self>
fn checked_div(self, rhs: Self) -> Option<Self>
Checked integer division. Computes self / rhs
, returning None
if
rhs == 0
or the division results in overflow.
fn checked_div_euclid(self, rhs: Self) -> Option<Self>
fn checked_div_euclid(self, rhs: Self) -> Option<Self>
Checked Euclidean division. Computes self.div_euclid(rhs)
, returning
None
if rhs == 0
or the division results in overflow.
fn checked_rem(self, rhs: Self) -> Option<Self>
fn checked_rem(self, rhs: Self) -> Option<Self>
Checked integer remainder. Computes self % rhs
, returning None
if
rhs == 0
or the division results in overflow.
fn checked_rem_euclid(self, rhs: Self) -> Option<Self>
fn checked_rem_euclid(self, rhs: Self) -> Option<Self>
Checked Euclidean remainder. Computes self.rem_euclid(rhs)
, returning
None
if rhs == 0
or the division results in overflow.
fn checked_neg(self) -> Option<Self>
fn checked_neg(self) -> Option<Self>
Checked negation. Computes -self
, returning None
if self == MIN
.
Note that negating any positive integer will overflow.
fn checked_shl(self, rhs: u32) -> Option<Self>
fn checked_shl(self, rhs: u32) -> Option<Self>
Checked shift left. Computes self << rhs
, returning None
if rhs
is
larger than or equal to the number of bits in self
.
fn checked_shr(self, rhs: u32) -> Option<Self>
fn checked_shr(self, rhs: u32) -> Option<Self>
Checked shift right. Computes self >> rhs
, returning None
if rhs
is larger than or equal to the number of bits in self
.
fn checked_pow(self, rhs: u32) -> Option<Self>
fn checked_pow(self, rhs: u32) -> Option<Self>
Checked exponentiation. Computes self.pow(exp)
, returning None
if
overflow occurred.
fn saturating_add(self, rhs: Self) -> Self
fn saturating_add(self, rhs: Self) -> Self
Saturating integer addition. Computes self + rhs
, saturating at the
numeric bounds instead of overflowing.
fn saturating_sub(self, rhs: Self) -> Self
fn saturating_sub(self, rhs: Self) -> Self
Saturating integer subtraction. Computes self - rhs
, saturating at the
numeric bounds instead of overflowing.
fn saturating_mul(self, rhs: Self) -> Self
fn saturating_mul(self, rhs: Self) -> Self
Saturating integer multiplication. Computes self * rhs
, saturating at
the numeric bounds instead of overflowing.
fn saturating_pow(self, rhs: u32) -> Self
fn saturating_pow(self, rhs: u32) -> Self
Saturating integer exponentiation. Computes self.pow(exp)
, saturating
at the numeric bounds instead of overflowing.
fn wrapping_add(self, rhs: Self) -> Self
fn wrapping_add(self, rhs: Self) -> Self
Wrapping (modular) addition. Computes self + rhs
, wrapping around at
the boundary of the type.
fn wrapping_sub(self, rhs: Self) -> Self
fn wrapping_sub(self, rhs: Self) -> Self
Wrapping (modular) subtraction. Computes self - rhs
, wrapping around
at the boundary of the type.
fn wrapping_mul(self, rhs: Self) -> Self
fn wrapping_mul(self, rhs: Self) -> Self
Wrapping (modular) multiplication. Computes self * rhs
, wrapping
around at the boundary of the type.
fn wrapping_div(self, rhs: Self) -> Self
fn wrapping_div(self, rhs: Self) -> Self
Wrapping (modular) division. Computes self / rhs
, wrapping around at
the boundary of the type.
Signed Integers
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.
Unsigned Integers
Wrapping (modular) division. Computes self / rhs
. Wrapped division on
unsigned types is just normal division. There’s no way wrapping could
ever happen. This function exists, so that all operations are accounted
for in the wrapping operations.
Panics
This function will panic if rhs
is 0.
fn wrapping_div_euclid(self, rhs: Self) -> Self
fn wrapping_div_euclid(self, rhs: Self) -> Self
Wrapping Eulidean division. Computes self.div_euclid(rhs)
, wrapping
around at the boundary of the type.
Signed Types
Wrapping will only occur in 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 this
case, this method returns MIN
itself.
Unsigned Types
Wrapped division on unsigned types is just normal division. There’s no
way wrapping could ever happen. This function exists, so that all
operations are accounted for in the wrapping operations. Since, for the
positive integers, all common definitions of division are equal, this is
exactly equal to self.wrapping_div(rhs)
.
Panics
This function will panic if rhs
is 0.
fn wrapping_rem(self, rhs: Self) -> Self
fn wrapping_rem(self, rhs: Self) -> Self
Wrapping (modular) remainder. Computes self % rhs
, wrapping around at
the boundary of the type.
Signed Integers
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
.
Unsigned Integers
Wrapped remainder calculation on unsigned types is just the regular remainder calculation. There’s no way wrapping could ever happen. This function exists, so that all operations are accounted for in the wrapping operations.
Panics
This function will panic if rhs
is 0.
fn wrapping_rem_euclid(self, rhs: Self) -> Self
fn wrapping_rem_euclid(self, rhs: Self) -> Self
Wrapping Euclidean remainder. Computes self.rem_euclid(rhs)
, wrapping
around at the boundary of the type.
Signed Integers
Wrapping will only occur in MIN % -1
on a signed type (where MIN
is
the negative minimal value for the type). In this case, this method
returns 0.
Unsigned Integers
Wrapped modulo calculation on unsigned types is just the regular
remainder calculation. There’s no way wrapping could ever happen. This
function exists, so that all operations are accounted for in the
wrapping operations. Since, for the positive integers, all common
definitions of division are equal, this is exactly equal to
self.wrapping_rem(rhs)
.
Panics
This function will panic if rhs
is 0.
fn wrapping_neg(self) -> Self
fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self
, wrapping around at the
boundary of the type.
Signed Integers
The only case where such wrapping can occur is when one negates MIN
on a signed type (where MIN
is the negative minimal value for the
type); this is a positive value that is too large to represent in the
type. In such a case, this function returns MIN
itself.
Unsigned Integers
Since unsigned types do not have negative equivalents all applications
of this function will wrap (except for -0
). For values smaller than
the corresponding signed type’s maximum the result is the same as
casting the corresponding signed value. Any larger values are equivalent
to MAX + 1 - (val - MAX - 1)
where MAX
is the corresponding signed
type’s maximum.
fn wrapping_shl(self, rhs: u32) -> Self
fn wrapping_shl(self, rhs: u32) -> Self
Panic-free bitwise shift-left; yields self << mask(rhs)
, where mask
removes any high-order bits of rhs
that would cause the shift to
exceed the bitwidth of the type.
Note that this is not the same as a rotate-left; the RHS of a wrapping
shift-left is restricted to the range of the type, rather than the bits
shifted out of the LHS being returned to the other end. The primitive
integer types all implement a rotate_left
function, which may be what
you want instead.
fn wrapping_shr(self, rhs: u32) -> Self
fn wrapping_shr(self, rhs: u32) -> Self
Panic-free bitwise shift-right; yields self >> mask(rhs)
, where mask
removes any high-order bits of rhs
that would cause the shift to
exceed the bitwidth of the type.
Note that this is not the same as a rotate-right; the RHS of a wrapping
shift-right is restricted to the range of the type, rather than the bits
shifted out of the LHS being returned to the other end. The primitive
integer types all implement a rotate_right
function, which may be what
you want instead.
fn wrapping_pow(self, rhs: u32) -> Self
fn wrapping_pow(self, rhs: u32) -> Self
Wrapping (modular) exponentiation. Computes self.pow(exp)
, wrapping
around at the boundary of the type.
fn overflowing_add(self, rhs: Self) -> (Self, bool)
fn overflowing_add(self, rhs: Self) -> (Self, bool)
Calculates self + rhs
Returns a tuple of the addition along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.
fn overflowing_sub(self, rhs: Self) -> (Self, bool)
fn overflowing_sub(self, rhs: Self) -> (Self, bool)
Calculates self - rhs
Returns a tuple of the subtraction along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.
fn overflowing_mul(self, rhs: Self) -> (Self, bool)
fn overflowing_mul(self, rhs: Self) -> (Self, bool)
Calculates the multiplication of self
and rhs
.
Returns a tuple of the multiplication along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped value is returned.
fn overflowing_div(self, rhs: Self) -> (Self, bool)
fn overflowing_div(self, rhs: Self) -> (Self, bool)
Calculates the divisor when self
is divided by rhs
.
Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would occur then self is returned.
Panics
This function will panic if rhs
is 0.
fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool)
fn overflowing_div_euclid(self, rhs: Self) -> (Self, bool)
Calculates the quotient of Euclidean division self.div_euclid(rhs)
.
Returns a tuple of the divisor along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would occur then self is returned.
Panics
This function will panic if rhs
is 0.
fn overflowing_rem(self, rhs: Self) -> (Self, bool)
fn overflowing_rem(self, rhs: Self) -> (Self, bool)
Calculates the remainder when self
is divided by rhs
.
Returns a tuple of the remainder after dividing along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would occur then 0 is returned.
Panics
This function will panic if rhs
is 0.
fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool)
fn overflowing_rem_euclid(self, rhs: Self) -> (Self, bool)
Overflowing Euclidean remainder. Calculates self.rem_euclid(rhs)
.
Returns a tuple of the remainder after dividing along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would occur then 0 is returned.
Panics
This function will panic if rhs is 0.
fn overflowing_neg(self) -> (Self, bool)
fn overflowing_neg(self) -> (Self, bool)
Negates self, overflowing if this is equal to the minimum value.
Returns a tuple of the negated version of self along with a boolean
indicating whether an overflow happened. If self
is the minimum value
(e.g., i32::MIN
for values of type i32
), then the minimum value will
be returned again and true
will be returned for an overflow happening.
Shifts self left by rhs
bits.
Returns a tuple of the shifted version of self along with a boolean indicating whether the shift value was larger than or equal to the number of bits. If the shift value is too large, then value is masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
Shifts self right by rhs
bits.
Returns a tuple of the shifted version of self along with a boolean indicating whether the shift value was larger than or equal to the number of bits. If the shift value is too large, then value is masked (N-1) where N is the number of bits, and this value is then used to perform the shift.
Raises self to the power of exp
, using exponentiation by squaring.
Returns a tuple of the exponentiation along with a bool indicating whether an overflow happened.
Raises self to the power of exp
, using exponentiation by squaring.
fn div_euclid(self, rhs: Self) -> Self
fn div_euclid(self, rhs: Self) -> Self
Calculates the quotient of Euclidean division of self by rhs.
This computes the integer n
such that
self = n * rhs + self.rem_euclid(rhs)
, with
0 <= self.rem_euclid(rhs) < rhs
.
In other words, the result is self / rhs
rounded to the integer n
such that self >= n * rhs
. If self > 0
, this is equal to round
towards zero (the default in Rust); if self < 0
, this is equal to
round towards +/- infinity.
Panics
This function will panic if rhs
is 0 or the division results in
overflow.
fn rem_euclid(self, rhs: Self) -> Self
fn rem_euclid(self, rhs: Self) -> Self
Calculates the least nonnegative remainder of self (mod rhs)
.
This is done as if by the Euclidean division algorithm – given
r = self.rem_euclid(rhs)
, self = rhs * self.div_euclid(rhs) + r
, and
0 <= r < abs(rhs)
.
Panics
This function will panic if rhs
is 0 or the division results in
overflow.