pub trait NonZeroPrimitiveUnsigned: NonZeroPrimitiveInteger<Integer: PrimitiveUnsigned> + From<NonZero<u8>> {
type NonZeroSigned: NonZeroPrimitiveSigned;
// Required methods
fn bit_width(self) -> NonZero<u32>;
fn cast_signed(self) -> Self::NonZeroSigned;
fn checked_add(self, other: Self::Integer) -> Option<Self>;
fn checked_next_power_of_two(self) -> Option<Self>;
fn div_ceil(self, rhs: Self) -> Self;
fn ilog10(self) -> u32;
fn ilog2(self) -> u32;
fn is_power_of_two(self) -> bool;
fn isqrt(self) -> Self;
fn midpoint(self, rhs: Self) -> Self;
fn saturating_add(self, other: Self::Integer) -> Self;
}Expand description
Trait for NonZero primitive unsigned integers, including the supertrait
NonZeroPrimitiveInteger.
This encapsulates trait implementations and inherent methods that are common among all of the
implementations of NonZero<T>, where T is a PrimitiveUnsigned.
See the corresponding items on the individual types for more documentation and examples.
This trait is sealed with a private trait to prevent downstream implementations, so we may continue to expand along with the standard library without worrying about breaking changes for implementors.
§Examples
use num_primitive::NonZeroPrimitiveUnsigned;
use core::num::NonZero;
fn gcd<T: NonZeroPrimitiveUnsigned>(mut a: T, mut b: T) -> T {
while let Some(r) = T::new(b.get() % a) {
(a, b) = (r, a);
}
a
}
let a = NonZero::new(48u32).unwrap();
let b = NonZero::new(18u32).unwrap();
assert_eq!(gcd(a, b).get(), 6);Required Associated Types§
Sourcetype NonZeroSigned: NonZeroPrimitiveSigned
type NonZeroSigned: NonZeroPrimitiveSigned
The signed non-zero integer type used by methods like
cast_signed.
For core::num::NonZero<T>, this is NonZero<T::Signed>.
Required Methods§
Sourcefn bit_width(self) -> NonZero<u32>
fn bit_width(self) -> NonZero<u32>
Returns the minimum number of bits required to represent self.
Sourcefn cast_signed(self) -> Self::NonZeroSigned
fn cast_signed(self) -> Self::NonZeroSigned
Returns the bit pattern of self reinterpreted as a signed integer of the same size.
Sourcefn checked_add(self, other: Self::Integer) -> Option<Self>
fn checked_add(self, other: Self::Integer) -> Option<Self>
Adds an unsigned integer to a non-zero value. Returns None on overflow.
Sourcefn checked_next_power_of_two(self) -> Option<Self>
fn checked_next_power_of_two(self) -> Option<Self>
Returns the smallest power of two greater than or equal to self. Checks for overflow and
returns None if the next power of two is greater than the type’s maximum value.
Sourcefn div_ceil(self, rhs: Self) -> Self
fn div_ceil(self, rhs: Self) -> Self
Calculates the quotient of self and rhs, rounding the result towards positive infinity.
Sourcefn is_power_of_two(self) -> bool
fn is_power_of_two(self) -> bool
Returns true if and only if self == (1 << k) for some k.
Sourcefn saturating_add(self, other: Self::Integer) -> Self
fn saturating_add(self, other: Self::Integer) -> Self
Adds an unsigned integer to a non-zero value. Returns Self::MAX on overflow.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl NonZeroPrimitiveUnsigned for NonZero<u8>
impl NonZeroPrimitiveUnsigned for NonZero<u8>
Source§fn cast_signed(self) -> Self::NonZeroSigned
fn cast_signed(self) -> Self::NonZeroSigned
See the inherent cast_signed method.
Source§fn checked_add(self, other: Self::Integer) -> Option<Self>
fn checked_add(self, other: Self::Integer) -> Option<Self>
See the inherent checked_add method.
Source§fn checked_next_power_of_two(self) -> Option<Self>
fn checked_next_power_of_two(self) -> Option<Self>
See the inherent checked_next_power_of_two method.
Source§fn is_power_of_two(self) -> bool
fn is_power_of_two(self) -> bool
See the inherent is_power_of_two method.
Source§fn saturating_add(self, other: Self::Integer) -> Self
fn saturating_add(self, other: Self::Integer) -> Self
See the inherent saturating_add method.
type NonZeroSigned = NonZero<i8>
Source§impl NonZeroPrimitiveUnsigned for NonZero<u16>
impl NonZeroPrimitiveUnsigned for NonZero<u16>
Source§fn cast_signed(self) -> Self::NonZeroSigned
fn cast_signed(self) -> Self::NonZeroSigned
See the inherent cast_signed method.
Source§fn checked_add(self, other: Self::Integer) -> Option<Self>
fn checked_add(self, other: Self::Integer) -> Option<Self>
See the inherent checked_add method.
Source§fn checked_next_power_of_two(self) -> Option<Self>
fn checked_next_power_of_two(self) -> Option<Self>
See the inherent checked_next_power_of_two method.
Source§fn is_power_of_two(self) -> bool
fn is_power_of_two(self) -> bool
See the inherent is_power_of_two method.
Source§fn saturating_add(self, other: Self::Integer) -> Self
fn saturating_add(self, other: Self::Integer) -> Self
See the inherent saturating_add method.
type NonZeroSigned = NonZero<i16>
Source§impl NonZeroPrimitiveUnsigned for NonZero<u32>
impl NonZeroPrimitiveUnsigned for NonZero<u32>
Source§fn cast_signed(self) -> Self::NonZeroSigned
fn cast_signed(self) -> Self::NonZeroSigned
See the inherent cast_signed method.
Source§fn checked_add(self, other: Self::Integer) -> Option<Self>
fn checked_add(self, other: Self::Integer) -> Option<Self>
See the inherent checked_add method.
Source§fn checked_next_power_of_two(self) -> Option<Self>
fn checked_next_power_of_two(self) -> Option<Self>
See the inherent checked_next_power_of_two method.
Source§fn is_power_of_two(self) -> bool
fn is_power_of_two(self) -> bool
See the inherent is_power_of_two method.
Source§fn saturating_add(self, other: Self::Integer) -> Self
fn saturating_add(self, other: Self::Integer) -> Self
See the inherent saturating_add method.
type NonZeroSigned = NonZero<i32>
Source§impl NonZeroPrimitiveUnsigned for NonZero<u64>
impl NonZeroPrimitiveUnsigned for NonZero<u64>
Source§fn cast_signed(self) -> Self::NonZeroSigned
fn cast_signed(self) -> Self::NonZeroSigned
See the inherent cast_signed method.
Source§fn checked_add(self, other: Self::Integer) -> Option<Self>
fn checked_add(self, other: Self::Integer) -> Option<Self>
See the inherent checked_add method.
Source§fn checked_next_power_of_two(self) -> Option<Self>
fn checked_next_power_of_two(self) -> Option<Self>
See the inherent checked_next_power_of_two method.
Source§fn is_power_of_two(self) -> bool
fn is_power_of_two(self) -> bool
See the inherent is_power_of_two method.
Source§fn saturating_add(self, other: Self::Integer) -> Self
fn saturating_add(self, other: Self::Integer) -> Self
See the inherent saturating_add method.
type NonZeroSigned = NonZero<i64>
Source§impl NonZeroPrimitiveUnsigned for NonZero<u128>
impl NonZeroPrimitiveUnsigned for NonZero<u128>
Source§fn cast_signed(self) -> Self::NonZeroSigned
fn cast_signed(self) -> Self::NonZeroSigned
See the inherent cast_signed method.
Source§fn checked_add(self, other: Self::Integer) -> Option<Self>
fn checked_add(self, other: Self::Integer) -> Option<Self>
See the inherent checked_add method.
Source§fn checked_next_power_of_two(self) -> Option<Self>
fn checked_next_power_of_two(self) -> Option<Self>
See the inherent checked_next_power_of_two method.
Source§fn is_power_of_two(self) -> bool
fn is_power_of_two(self) -> bool
See the inherent is_power_of_two method.
Source§fn saturating_add(self, other: Self::Integer) -> Self
fn saturating_add(self, other: Self::Integer) -> Self
See the inherent saturating_add method.
type NonZeroSigned = NonZero<i128>
Source§impl NonZeroPrimitiveUnsigned for NonZero<usize>
impl NonZeroPrimitiveUnsigned for NonZero<usize>
Source§fn cast_signed(self) -> Self::NonZeroSigned
fn cast_signed(self) -> Self::NonZeroSigned
See the inherent cast_signed method.
Source§fn checked_add(self, other: Self::Integer) -> Option<Self>
fn checked_add(self, other: Self::Integer) -> Option<Self>
See the inherent checked_add method.
Source§fn checked_next_power_of_two(self) -> Option<Self>
fn checked_next_power_of_two(self) -> Option<Self>
See the inherent checked_next_power_of_two method.
Source§fn is_power_of_two(self) -> bool
fn is_power_of_two(self) -> bool
See the inherent is_power_of_two method.
Source§fn saturating_add(self, other: Self::Integer) -> Self
fn saturating_add(self, other: Self::Integer) -> Self
See the inherent saturating_add method.