NonNU64

Struct NonNU64 

Source
pub struct NonNU64<const N: u64>(/* private fields */);
Expand description

64-bit unsigned integer type that is known not to equal to any single value N.

This enables some memory layout optimization. For example, Option<NonNU64<N>> is the same size as u64:

use std::mem::size_of;
assert_eq!(size_of::<Option<NonNU64<42>>>(), size_of::<u64>());

§Layout

NonNU64\<N> is guaranteed to have the same layout and bit validity as u64 with the exception that N is not a valid instance. Option<NonNU64> is guaranteed to be compatible with u64, including in FFI.

Note that this does not mean you can transmute this type to get a u64 of the same value. If you need an u64 of the same value, use NonNU64::get instead.

Implementations§

Source§

impl<const N: u64> NonNU64<N>

Source

pub const MIN: Self

The smallest value that can be represented by this non-N integer type.

Source

pub const MAX: Self

The largest value that can be represented by this non-N integer type.

Source

pub const BITS: u32 = 64u32

The size of this non-N integer type in bits.

This value is equal to u64::BITS.

§Examples

assert_eq!(NonNU64::<42>::BITS, u64::BITS);
Source

pub const fn new(n: u64) -> Option<Self>

Creates a non-N if the given value is not N.

Source

pub const unsafe fn new_unchecked(n: u64) -> Self

Creates a NonNU64<N> without checking whether the value is non-N. This results in undefined behaviour if the value is N.

§Safety

The value must not be N.

Source

pub const fn get(self) -> u64

Returns the value as a primitive type.

Source

pub const fn cast<const M: u64>(self) -> Option<NonNU64<M>>

Returns a new NonNU64<M> with the current value if it is not M.

Source

pub const unsafe fn cast_unchecked<const M: u64>(self) -> NonNU64<M>

Returns a new NonNU64<M> with the current value without checking whether it is not M. This results in undefined behaviour if the current value is M.

§Safety

The current value must not be M.

Source

pub const fn leading_zeros(self) -> u32

Returns the number of leading zeros in the binary representation of self.

§Examples

Basic usage:

let n = NonNU64::<42>::new(u64::MAX).unwrap();

assert_eq!(n.leading_zeros(), 0);
Source

pub const fn trailing_zeros(self) -> u32

Returns the number of trailing zeros in the binary representation of self.

§Examples

Basic usage:

let n = NonNU64::<42>::new(0b0101000).unwrap();

assert_eq!(n.trailing_zeros(), 3);
Source

pub const fn checked_mul(self, other: Self) -> Option<Self>

Multiplies two non-N integers together. Checks for overflow and returns None on overflow.

§Examples
let two = NonNU64::<42>::new(2).unwrap();
let four = NonNU64::<42>::new(4).unwrap();
let max = NonNU64::<42>::new(u64::MAX).unwrap();

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
Source

pub const fn saturating_mul(self, other: Self) -> Self

Multiplies two non-N integers together. Return NonNU64::<42>::MAX on overflow.

§Examples
let two = NonNU64::<42>::new(2)?;
let four = NonNU64::<42>::new(4)?;
let max = NonNU64::<42>::new(u64::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
Source

pub const fn checked_pow(self, other: u32) -> Option<Self>

Multiplies two non-N integers together, assuming overflow cannot occur. Overflow is unchecked, and it is undefined behaviour to overflow even if the result would wrap to a non-N value. The behaviour is undefined as soon as self * rhs > u64::MAX. Raises non-N value to an integer power. Checks for overflow and returns None on overflow.

§Examples
let three = NonNU64::<42>::new(3)?;
let twenty_seven = NonNU64::<42>::new(27)?;
let half_max = NonNU64::<42>::new(u64::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
Source

pub const fn saturating_pow(self, other: u32) -> Self

Raise non-N value to an integer power. Return NonNU64::<42>::MAX on overflow.

§Examples
let three = NonNU64::<42>::new(3)?;
let twenty_seven = NonNU64::<42>::new(27)?;
let max = NonNU64::<42>::new(u64::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
Source

pub const fn is_power_of_two(self) -> bool

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

§Examples

Basic usage:

let eight = NonNU64::<42>::new(8).unwrap();
assert!(eight.is_power_of_two());
let ten = NonNU64::<42>::new(10).unwrap();
assert!(!ten.is_power_of_two());
Source

pub const fn checked_add(self, other: u64) -> Option<Self>

Adds an unsigned integer to a non-N value. Checks for overflow and returns None on overflow.

§Examples
let one = NonNU64::<42>::new(1)?;
let two = NonNU64::<42>::new(2)?;
let max = NonNU64::<42>::new(u64::MAX)?;

assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
Source

pub const fn saturating_add(self, other: u64) -> Self

Adds an unsigned integer to a non-N value. Return NonNU64::MAX on overflow.

§Examples
let one = NonNU64::<42>::new(1)?;
let two = NonNU64::<42>::new(2)?;
let max = NonNU64::<42>::new(u64::MAX)?;

assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
Source

pub const fn checked_next_power_of_two(self) -> Option<Self>

Returns the smallest power of two greater than or equal to n. Checks for overflow and returns None if the next power of two is greater than the type’s maximum value.

§Examples
let two = NonNU64::<42>::new(2)?;
let three = NonNU64::<42>::new(3)?;
let four = NonNU64::<42>::new(4)?;
let max = NonNU64::<42>::new(u64::MAX)?;

assert_eq!(Some(two), two.checked_next_power_of_two() );
assert_eq!(Some(four), three.checked_next_power_of_two() );
assert_eq!(None, max.checked_next_power_of_two() );
Source

pub const fn ilog2(self) -> u32

Returns the base 2 logarithm of the number, rounded down. u64::ilog2,

§Examples
assert_eq!(NonNU64::<42>::new(7).unwrap().ilog2(), 2);
assert_eq!(NonNU64::<42>::new(8).unwrap().ilog2(), 3);
assert_eq!(NonNU64::<42>::new(9).unwrap().ilog2(), 3);
Source

pub const fn ilog10(self) -> u32

Returns the base 10 logarithm of the number, rounded down. u64::ilog10,

§Examples
assert_eq!(NonNU64::<42>::new(99).unwrap().ilog10(), 1);
assert_eq!(NonNU64::<42>::new(100).unwrap().ilog10(), 2);
assert_eq!(NonNU64::<42>::new(101).unwrap().ilog10(), 2);

Trait Implementations§

Source§

impl<const N: u64, const M: u64> Add<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: NonNU64<M>) -> Self::Output

Performs the + operation. Read more
Source§

impl<const N: u64> Add<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: u64) -> Self::Output

Performs the + operation. Read more
Source§

impl<const N: u64, const M: u64> AddAssign<NonNU64<M>> for NonNU64<N>

Source§

fn add_assign(&mut self, rhs: NonNU64<M>)

Performs the += operation. Read more
Source§

impl<const N: u64> AddAssign<u64> for NonNU64<N>

Source§

fn add_assign(&mut self, rhs: u64)

Performs the += operation. Read more
Source§

impl<const N: u64> Binary for NonNU64<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: u64, const M: u64> BitAnd<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: NonNU64<M>) -> Self::Output

Performs the & operation. Read more
Source§

impl<const N: u64> BitAnd<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: u64) -> Self::Output

Performs the & operation. Read more
Source§

impl<const N: u64, const M: u64> BitAndAssign<NonNU64<M>> for NonNU64<N>

Source§

fn bitand_assign(&mut self, rhs: NonNU64<M>)

Performs the &= operation. Read more
Source§

impl<const N: u64> BitAndAssign<u64> for NonNU64<N>

Source§

fn bitand_assign(&mut self, rhs: u64)

Performs the &= operation. Read more
Source§

impl<const N: u64, const M: u64> BitOr<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: NonNU64<M>) -> Self::Output

Performs the | operation. Read more
Source§

impl<const N: u64> BitOr<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: u64) -> Self::Output

Performs the | operation. Read more
Source§

impl<const N: u64, const M: u64> BitOrAssign<NonNU64<M>> for NonNU64<N>

Source§

fn bitor_assign(&mut self, rhs: NonNU64<M>)

Performs the |= operation. Read more
Source§

impl<const N: u64> BitOrAssign<u64> for NonNU64<N>

Source§

fn bitor_assign(&mut self, rhs: u64)

Performs the |= operation. Read more
Source§

impl<const N: u64, const M: u64> BitXor<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: NonNU64<M>) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<const N: u64> BitXor<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: u64) -> Self::Output

Performs the ^ operation. Read more
Source§

impl<const N: u64, const M: u64> BitXorAssign<NonNU64<M>> for NonNU64<N>

Source§

fn bitxor_assign(&mut self, rhs: NonNU64<M>)

Performs the ^= operation. Read more
Source§

impl<const N: u64> BitXorAssign<u64> for NonNU64<N>

Source§

fn bitxor_assign(&mut self, rhs: u64)

Performs the ^= operation. Read more
Source§

impl<const N: u64> Clone for NonNU64<N>

Source§

fn clone(&self) -> NonNU64<N>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: u64> Debug for NonNU64<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: u64> Display for NonNU64<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: u64, const M: u64> Div<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: NonNU64<M>) -> Self::Output

Performs the / operation. Read more
Source§

impl<const N: u64> Div<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u64) -> Self::Output

Performs the / operation. Read more
Source§

impl<const N: u64, const M: u64> DivAssign<NonNU64<M>> for NonNU64<N>

Source§

fn div_assign(&mut self, rhs: NonNU64<M>)

Performs the /= operation. Read more
Source§

impl<const N: u64> DivAssign<u64> for NonNU64<N>

Source§

fn div_assign(&mut self, rhs: u64)

Performs the /= operation. Read more
Source§

impl<const N: u64> From<NonNU64<N>> for u64

Source§

fn from(nonn: NonNU64<N>) -> Self

Converts a NonNU64 into an u64

Source§

impl<const N: u64> FromStr for NonNU64<N>

Source§

type Err = ParseIntError

The associated error which can be returned from parsing.
Source§

fn from_str(src: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<const N: u64> Hash for NonNU64<N>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const N: u64> LowerHex for NonNU64<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: u64, const M: u64> Mul<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: NonNU64<M>) -> Self::Output

Performs the * operation. Read more
Source§

impl<const N: u64> Mul<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u64) -> Self::Output

Performs the * operation. Read more
Source§

impl<const N: u64, const M: u64> MulAssign<NonNU64<M>> for NonNU64<N>

Source§

fn mul_assign(&mut self, rhs: NonNU64<M>)

Performs the *= operation. Read more
Source§

impl<const N: u64> MulAssign<u64> for NonNU64<N>

Source§

fn mul_assign(&mut self, rhs: u64)

Performs the *= operation. Read more
Source§

impl<const N: u64> Not for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the ! operator.
Source§

fn not(self) -> Self::Output

Performs the unary ! operation. Read more
Source§

impl<const N: u64> Octal for NonNU64<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: u64> Ord for NonNU64<N>

Source§

fn cmp(&self, other: &NonNU64<N>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<const N: u64> PartialEq for NonNU64<N>

Source§

fn eq(&self, other: &NonNU64<N>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<const N: u64> PartialOrd for NonNU64<N>

Source§

fn partial_cmp(&self, other: &NonNU64<N>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const N: u64, const M: u64> Rem<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: NonNU64<M>) -> Self::Output

Performs the % operation. Read more
Source§

impl<const N: u64> Rem<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: u64) -> Self::Output

Performs the % operation. Read more
Source§

impl<const N: u64, const M: u64> RemAssign<NonNU64<M>> for NonNU64<N>

Source§

fn rem_assign(&mut self, rhs: NonNU64<M>)

Performs the %= operation. Read more
Source§

impl<const N: u64> RemAssign<u64> for NonNU64<N>

Source§

fn rem_assign(&mut self, rhs: u64)

Performs the %= operation. Read more
Source§

impl<const N: u64, const M: u64> Shl<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: NonNU64<M>) -> Self::Output

Performs the << operation. Read more
Source§

impl<const N: u64> Shl<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: u64) -> Self::Output

Performs the << operation. Read more
Source§

impl<const N: u64, const M: u64> ShlAssign<NonNU64<M>> for NonNU64<N>

Source§

fn shl_assign(&mut self, rhs: NonNU64<M>)

Performs the <<= operation. Read more
Source§

impl<const N: u64> ShlAssign<u64> for NonNU64<N>

Source§

fn shl_assign(&mut self, rhs: u64)

Performs the <<= operation. Read more
Source§

impl<const N: u64, const M: u64> Shr<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: NonNU64<M>) -> Self::Output

Performs the >> operation. Read more
Source§

impl<const N: u64> Shr<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: u64) -> Self::Output

Performs the >> operation. Read more
Source§

impl<const N: u64, const M: u64> ShrAssign<NonNU64<M>> for NonNU64<N>

Source§

fn shr_assign(&mut self, rhs: NonNU64<M>)

Performs the >>= operation. Read more
Source§

impl<const N: u64> ShrAssign<u64> for NonNU64<N>

Source§

fn shr_assign(&mut self, rhs: u64)

Performs the >>= operation. Read more
Source§

impl<const N: u64, const M: u64> Sub<NonNU64<M>> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: NonNU64<M>) -> Self::Output

Performs the - operation. Read more
Source§

impl<const N: u64> Sub<u64> for NonNU64<N>

Source§

type Output = NonNU64<N>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: u64) -> Self::Output

Performs the - operation. Read more
Source§

impl<const N: u64, const M: u64> SubAssign<NonNU64<M>> for NonNU64<N>

Source§

fn sub_assign(&mut self, rhs: NonNU64<M>)

Performs the -= operation. Read more
Source§

impl<const N: u64> SubAssign<u64> for NonNU64<N>

Source§

fn sub_assign(&mut self, rhs: u64)

Performs the -= operation. Read more
Source§

impl<const N: u64> UpperHex for NonNU64<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: u64> Copy for NonNU64<N>

Source§

impl<const N: u64> Eq for NonNU64<N>

Source§

impl<const N: u64> StructuralPartialEq for NonNU64<N>

Auto Trait Implementations§

§

impl<const N: u64> Freeze for NonNU64<N>

§

impl<const N: u64> RefUnwindSafe for NonNU64<N>

§

impl<const N: u64> Send for NonNU64<N>

§

impl<const N: u64> Sync for NonNU64<N>

§

impl<const N: u64> Unpin for NonNU64<N>

§

impl<const N: u64> UnwindSafe for NonNU64<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.