Skip to main content

NonZeroPrimitiveUnsigned

Trait NonZeroPrimitiveUnsigned 

Source
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§

Source

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§

Source

fn bit_width(self) -> NonZero<u32>

Returns the minimum number of bits required to represent self.

Source

fn cast_signed(self) -> Self::NonZeroSigned

Returns the bit pattern of self reinterpreted as a signed integer of the same size.

Source

fn checked_add(self, other: Self::Integer) -> Option<Self>

Adds an unsigned integer to a non-zero value. Returns None on overflow.

Source

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.

Source

fn div_ceil(self, rhs: Self) -> Self

Calculates the quotient of self and rhs, rounding the result towards positive infinity.

Source

fn ilog10(self) -> u32

Returns the base 10 logarithm of the number, rounded down.

Source

fn ilog2(self) -> u32

Returns the base 2 logarithm of the number, rounded down.

Source

fn is_power_of_two(self) -> bool

Returns true if and only if self == (1 << k) for some k.

Source

fn isqrt(self) -> Self

Returns the square root of the number, rounded down.

Source

fn midpoint(self, rhs: Self) -> Self

Calculates the midpoint (average) between self and rhs.

Source

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>

Source§

fn bit_width(self) -> NonZero<u32>

See the inherent bit_width method.

Source§

fn cast_signed(self) -> Self::NonZeroSigned

See the inherent cast_signed method.

Source§

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>

See the inherent checked_next_power_of_two method.

Source§

fn div_ceil(self, rhs: Self) -> Self

See the inherent div_ceil method.

Source§

fn ilog10(self) -> u32

See the inherent ilog10 method.

Source§

fn ilog2(self) -> u32

See the inherent ilog2 method.

Source§

fn is_power_of_two(self) -> bool

See the inherent is_power_of_two method.

Source§

fn isqrt(self) -> Self

See the inherent isqrt method.

Source§

fn midpoint(self, rhs: Self) -> Self

See the inherent midpoint method.

Source§

fn saturating_add(self, other: Self::Integer) -> Self

See the inherent saturating_add method.

Source§

type NonZeroSigned = NonZero<i8>

Source§

impl NonZeroPrimitiveUnsigned for NonZero<u16>

Source§

fn bit_width(self) -> NonZero<u32>

See the inherent bit_width method.

Source§

fn cast_signed(self) -> Self::NonZeroSigned

See the inherent cast_signed method.

Source§

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>

See the inherent checked_next_power_of_two method.

Source§

fn div_ceil(self, rhs: Self) -> Self

See the inherent div_ceil method.

Source§

fn ilog10(self) -> u32

See the inherent ilog10 method.

Source§

fn ilog2(self) -> u32

See the inherent ilog2 method.

Source§

fn is_power_of_two(self) -> bool

See the inherent is_power_of_two method.

Source§

fn isqrt(self) -> Self

See the inherent isqrt method.

Source§

fn midpoint(self, rhs: Self) -> Self

See the inherent midpoint method.

Source§

fn saturating_add(self, other: Self::Integer) -> Self

See the inherent saturating_add method.

Source§

type NonZeroSigned = NonZero<i16>

Source§

impl NonZeroPrimitiveUnsigned for NonZero<u32>

Source§

fn bit_width(self) -> NonZero<u32>

See the inherent bit_width method.

Source§

fn cast_signed(self) -> Self::NonZeroSigned

See the inherent cast_signed method.

Source§

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>

See the inherent checked_next_power_of_two method.

Source§

fn div_ceil(self, rhs: Self) -> Self

See the inherent div_ceil method.

Source§

fn ilog10(self) -> u32

See the inherent ilog10 method.

Source§

fn ilog2(self) -> u32

See the inherent ilog2 method.

Source§

fn is_power_of_two(self) -> bool

See the inherent is_power_of_two method.

Source§

fn isqrt(self) -> Self

See the inherent isqrt method.

Source§

fn midpoint(self, rhs: Self) -> Self

See the inherent midpoint method.

Source§

fn saturating_add(self, other: Self::Integer) -> Self

See the inherent saturating_add method.

Source§

type NonZeroSigned = NonZero<i32>

Source§

impl NonZeroPrimitiveUnsigned for NonZero<u64>

Source§

fn bit_width(self) -> NonZero<u32>

See the inherent bit_width method.

Source§

fn cast_signed(self) -> Self::NonZeroSigned

See the inherent cast_signed method.

Source§

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>

See the inherent checked_next_power_of_two method.

Source§

fn div_ceil(self, rhs: Self) -> Self

See the inherent div_ceil method.

Source§

fn ilog10(self) -> u32

See the inherent ilog10 method.

Source§

fn ilog2(self) -> u32

See the inherent ilog2 method.

Source§

fn is_power_of_two(self) -> bool

See the inherent is_power_of_two method.

Source§

fn isqrt(self) -> Self

See the inherent isqrt method.

Source§

fn midpoint(self, rhs: Self) -> Self

See the inherent midpoint method.

Source§

fn saturating_add(self, other: Self::Integer) -> Self

See the inherent saturating_add method.

Source§

type NonZeroSigned = NonZero<i64>

Source§

impl NonZeroPrimitiveUnsigned for NonZero<u128>

Source§

fn bit_width(self) -> NonZero<u32>

See the inherent bit_width method.

Source§

fn cast_signed(self) -> Self::NonZeroSigned

See the inherent cast_signed method.

Source§

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>

See the inherent checked_next_power_of_two method.

Source§

fn div_ceil(self, rhs: Self) -> Self

See the inherent div_ceil method.

Source§

fn ilog10(self) -> u32

See the inherent ilog10 method.

Source§

fn ilog2(self) -> u32

See the inherent ilog2 method.

Source§

fn is_power_of_two(self) -> bool

See the inherent is_power_of_two method.

Source§

fn isqrt(self) -> Self

See the inherent isqrt method.

Source§

fn midpoint(self, rhs: Self) -> Self

See the inherent midpoint method.

Source§

fn saturating_add(self, other: Self::Integer) -> Self

See the inherent saturating_add method.

Source§

type NonZeroSigned = NonZero<i128>

Source§

impl NonZeroPrimitiveUnsigned for NonZero<usize>

Source§

fn bit_width(self) -> NonZero<u32>

See the inherent bit_width method.

Source§

fn cast_signed(self) -> Self::NonZeroSigned

See the inherent cast_signed method.

Source§

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>

See the inherent checked_next_power_of_two method.

Source§

fn div_ceil(self, rhs: Self) -> Self

See the inherent div_ceil method.

Source§

fn ilog10(self) -> u32

See the inherent ilog10 method.

Source§

fn ilog2(self) -> u32

See the inherent ilog2 method.

Source§

fn is_power_of_two(self) -> bool

See the inherent is_power_of_two method.

Source§

fn isqrt(self) -> Self

See the inherent isqrt method.

Source§

fn midpoint(self, rhs: Self) -> Self

See the inherent midpoint method.

Source§

fn saturating_add(self, other: Self::Integer) -> Self

See the inherent saturating_add method.

Source§

type NonZeroSigned = NonZero<isize>

Implementors§