pub struct NonNU8<const N: u8>(/* private fields */);Expand description
8-bit unsigned integer type that is known not to equal to any single value N.
This enables some memory layout optimization.
For example, Option<NonNU8<N>> is the same size as u8:
use std::mem::size_of;
assert_eq!(size_of::<Option<NonNU8<42>>>(), size_of::<u8>());§Layout
NonNU8\<N> is guaranteed to have the same layout and bit validity as u8
with the exception that N is not a valid instance.
Option<NonNU8> is guaranteed to be compatible with u8,
including in FFI.
Note that this does not mean you can transmute this type to get a u8 of the same value.
If you need an u8 of the same value, use NonNU8::get instead.
Implementations§
Source§impl<const N: u8> NonNU8<N>
impl<const N: u8> NonNU8<N>
Sourcepub const unsafe fn new_unchecked(n: u8) -> Self
pub const unsafe fn new_unchecked(n: u8) -> Self
Creates a NonNU8<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.
Sourcepub const fn cast<const M: u8>(self) -> Option<NonNU8<M>>
pub const fn cast<const M: u8>(self) -> Option<NonNU8<M>>
Returns a new NonNU8<M> with the current value if it is not M.
Sourcepub const unsafe fn cast_unchecked<const M: u8>(self) -> NonNU8<M>
pub const unsafe fn cast_unchecked<const M: u8>(self) -> NonNU8<M>
Returns a new NonNU8<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.
Sourcepub const fn leading_zeros(self) -> u32
pub const fn leading_zeros(self) -> u32
Returns the number of leading zeros in the binary representation of self.
§Examples
Basic usage:
let n = NonNU8::<42>::new(u8::MAX).unwrap();
assert_eq!(n.leading_zeros(), 0);Sourcepub const fn trailing_zeros(self) -> u32
pub const fn trailing_zeros(self) -> u32
Returns the number of trailing zeros in the binary representation
of self.
§Examples
Basic usage:
let n = NonNU8::<42>::new(0b0101000).unwrap();
assert_eq!(n.trailing_zeros(), 3);Sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
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 = NonNU8::<42>::new(2).unwrap();
let four = NonNU8::<42>::new(4).unwrap();
let max = NonNU8::<42>::new(u8::MAX).unwrap();
assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));Sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-N integers together.
Return NonNU8::<42>::MAX on overflow.
§Examples
let two = NonNU8::<42>::new(2)?;
let four = NonNU8::<42>::new(4)?;
let max = NonNU8::<42>::new(u8::MAX)?;
assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));Sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
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 > u8::MAX.
Raises non-N value to an integer power.
Checks for overflow and returns None on overflow.
§Examples
let three = NonNU8::<42>::new(3)?;
let twenty_seven = NonNU8::<42>::new(27)?;
let half_max = NonNU8::<42>::new(u8::MAX / 2)?;
assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));Sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-N value to an integer power.
Return NonNU8::<42>::MAX on overflow.
§Examples
let three = NonNU8::<42>::new(3)?;
let twenty_seven = NonNU8::<42>::new(27)?;
let max = NonNU8::<42>::new(u8::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));Sourcepub const fn is_power_of_two(self) -> bool
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 = NonNU8::<42>::new(8).unwrap();
assert!(eight.is_power_of_two());
let ten = NonNU8::<42>::new(10).unwrap();
assert!(!ten.is_power_of_two());Sourcepub const fn checked_add(self, other: u8) -> Option<Self>
pub const fn checked_add(self, other: u8) -> Option<Self>
Sourcepub const fn saturating_add(self, other: u8) -> Self
pub const fn saturating_add(self, other: u8) -> Self
Adds an unsigned integer to a non-N value.
Return NonNU8::MAX on overflow.
§Examples
let one = NonNU8::<42>::new(1)?;
let two = NonNU8::<42>::new(2)?;
let max = NonNU8::<42>::new(u8::MAX)?;
assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));Sourcepub const fn checked_next_power_of_two(self) -> Option<Self>
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 = NonNU8::<42>::new(2)?;
let three = NonNU8::<42>::new(3)?;
let four = NonNU8::<42>::new(4)?;
let max = NonNU8::<42>::new(u8::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() );Sourcepub const fn ilog10(self) -> u32
pub const fn ilog10(self) -> u32
Returns the base 10 logarithm of the number, rounded down.
u8::ilog10,
§Examples
assert_eq!(NonNU8::<42>::new(99).unwrap().ilog10(), 1);
assert_eq!(NonNU8::<42>::new(100).unwrap().ilog10(), 2);
assert_eq!(NonNU8::<42>::new(101).unwrap().ilog10(), 2);Trait Implementations§
Source§impl<const N: u8, const M: u8> AddAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> AddAssign<NonNU8<M>> for NonNU8<N>
Source§fn add_assign(&mut self, rhs: NonNU8<M>)
fn add_assign(&mut self, rhs: NonNU8<M>)
+= operation. Read moreSource§impl<const N: u8> AddAssign<u8> for NonNU8<N>
impl<const N: u8> AddAssign<u8> for NonNU8<N>
Source§fn add_assign(&mut self, rhs: u8)
fn add_assign(&mut self, rhs: u8)
+= operation. Read moreSource§impl<const N: u8, const M: u8> BitAndAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> BitAndAssign<NonNU8<M>> for NonNU8<N>
Source§fn bitand_assign(&mut self, rhs: NonNU8<M>)
fn bitand_assign(&mut self, rhs: NonNU8<M>)
&= operation. Read moreSource§impl<const N: u8> BitAndAssign<u8> for NonNU8<N>
impl<const N: u8> BitAndAssign<u8> for NonNU8<N>
Source§fn bitand_assign(&mut self, rhs: u8)
fn bitand_assign(&mut self, rhs: u8)
&= operation. Read moreSource§impl<const N: u8, const M: u8> BitOrAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> BitOrAssign<NonNU8<M>> for NonNU8<N>
Source§fn bitor_assign(&mut self, rhs: NonNU8<M>)
fn bitor_assign(&mut self, rhs: NonNU8<M>)
|= operation. Read moreSource§impl<const N: u8> BitOrAssign<u8> for NonNU8<N>
impl<const N: u8> BitOrAssign<u8> for NonNU8<N>
Source§fn bitor_assign(&mut self, rhs: u8)
fn bitor_assign(&mut self, rhs: u8)
|= operation. Read moreSource§impl<const N: u8, const M: u8> BitXorAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> BitXorAssign<NonNU8<M>> for NonNU8<N>
Source§fn bitxor_assign(&mut self, rhs: NonNU8<M>)
fn bitxor_assign(&mut self, rhs: NonNU8<M>)
^= operation. Read moreSource§impl<const N: u8> BitXorAssign<u8> for NonNU8<N>
impl<const N: u8> BitXorAssign<u8> for NonNU8<N>
Source§fn bitxor_assign(&mut self, rhs: u8)
fn bitxor_assign(&mut self, rhs: u8)
^= operation. Read moreSource§impl<const N: u8, const M: u8> DivAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> DivAssign<NonNU8<M>> for NonNU8<N>
Source§fn div_assign(&mut self, rhs: NonNU8<M>)
fn div_assign(&mut self, rhs: NonNU8<M>)
/= operation. Read moreSource§impl<const N: u8> DivAssign<u8> for NonNU8<N>
impl<const N: u8> DivAssign<u8> for NonNU8<N>
Source§fn div_assign(&mut self, rhs: u8)
fn div_assign(&mut self, rhs: u8)
/= operation. Read moreSource§impl<const N: u8, const M: u8> MulAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> MulAssign<NonNU8<M>> for NonNU8<N>
Source§fn mul_assign(&mut self, rhs: NonNU8<M>)
fn mul_assign(&mut self, rhs: NonNU8<M>)
*= operation. Read moreSource§impl<const N: u8> MulAssign<u8> for NonNU8<N>
impl<const N: u8> MulAssign<u8> for NonNU8<N>
Source§fn mul_assign(&mut self, rhs: u8)
fn mul_assign(&mut self, rhs: u8)
*= operation. Read moreSource§impl<const N: u8> Ord for NonNU8<N>
impl<const N: u8> Ord for NonNU8<N>
Source§impl<const N: u8> PartialOrd for NonNU8<N>
impl<const N: u8> PartialOrd for NonNU8<N>
Source§impl<const N: u8, const M: u8> RemAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> RemAssign<NonNU8<M>> for NonNU8<N>
Source§fn rem_assign(&mut self, rhs: NonNU8<M>)
fn rem_assign(&mut self, rhs: NonNU8<M>)
%= operation. Read moreSource§impl<const N: u8> RemAssign<u8> for NonNU8<N>
impl<const N: u8> RemAssign<u8> for NonNU8<N>
Source§fn rem_assign(&mut self, rhs: u8)
fn rem_assign(&mut self, rhs: u8)
%= operation. Read moreSource§impl<const N: u8, const M: u8> ShlAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> ShlAssign<NonNU8<M>> for NonNU8<N>
Source§fn shl_assign(&mut self, rhs: NonNU8<M>)
fn shl_assign(&mut self, rhs: NonNU8<M>)
<<= operation. Read moreSource§impl<const N: u8> ShlAssign<u8> for NonNU8<N>
impl<const N: u8> ShlAssign<u8> for NonNU8<N>
Source§fn shl_assign(&mut self, rhs: u8)
fn shl_assign(&mut self, rhs: u8)
<<= operation. Read moreSource§impl<const N: u8, const M: u8> ShrAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> ShrAssign<NonNU8<M>> for NonNU8<N>
Source§fn shr_assign(&mut self, rhs: NonNU8<M>)
fn shr_assign(&mut self, rhs: NonNU8<M>)
>>= operation. Read moreSource§impl<const N: u8> ShrAssign<u8> for NonNU8<N>
impl<const N: u8> ShrAssign<u8> for NonNU8<N>
Source§fn shr_assign(&mut self, rhs: u8)
fn shr_assign(&mut self, rhs: u8)
>>= operation. Read moreSource§impl<const N: u8, const M: u8> SubAssign<NonNU8<M>> for NonNU8<N>
impl<const N: u8, const M: u8> SubAssign<NonNU8<M>> for NonNU8<N>
Source§fn sub_assign(&mut self, rhs: NonNU8<M>)
fn sub_assign(&mut self, rhs: NonNU8<M>)
-= operation. Read moreSource§impl<const N: u8> SubAssign<u8> for NonNU8<N>
impl<const N: u8> SubAssign<u8> for NonNU8<N>
Source§fn sub_assign(&mut self, rhs: u8)
fn sub_assign(&mut self, rhs: u8)
-= operation. Read more