pub struct NonMax<T: NonMaxItem>(/* private fields */);Expand description
A wrapper type for an integer that cannot be its maximum value.
This type leverages Rust’s NonZero optimization by mapping the maximum value to zero internally.
As a result, Option<NonMax<T>> has the same size as the underlying primitive type T.
§Examples
assert_eq!(size_of::<NonMaxU32>(), 4);
assert_eq!(size_of::<Option<NonMaxU32>>(), 4);Implementations§
Source§impl<T: NonMaxItem + Copy> NonMax<T>
impl<T: NonMaxItem + Copy> NonMax<T>
Source§impl<T: NonMaxItem + Copy> NonMax<T>
impl<T: NonMaxItem + Copy> NonMax<T>
Sourcepub fn checked_add(self, rhs: Self) -> Option<Self>
pub fn checked_add(self, rhs: Self) -> Option<Self>
Checked integer addition. Computes self + rhs, returning None if overflow occurred
or if the result is the maximum value.
Sourcepub fn checked_sub(self, rhs: Self) -> Option<Self>
pub fn checked_sub(self, rhs: Self) -> Option<Self>
Checked integer subtraction. Computes self - rhs, returning None if overflow occurred
or if the result is the maximum value.
Sourcepub fn checked_mul(self, rhs: Self) -> Option<Self>
pub fn checked_mul(self, rhs: Self) -> Option<Self>
Checked integer multiplication. Computes self * rhs, returning None if overflow occurred
or if the result is the maximum value.
Sourcepub fn checked_div(self, rhs: Self) -> Option<Self>
pub fn checked_div(self, rhs: Self) -> Option<Self>
Checked integer division. Computes self / rhs, returning None if the divisor is zero
or if the result is the maximum value.
Sourcepub fn checked_rem(self, rhs: Self) -> Option<Self>
pub fn checked_rem(self, rhs: Self) -> Option<Self>
Checked integer remainder. Computes self % rhs, returning None if the divisor is zero
or if the result is the maximum value.
Sourcepub fn checked_add_val(self, rhs: T) -> Option<Self>
pub fn checked_add_val(self, rhs: T) -> Option<Self>
Checked addition with a primitive value.
§Examples
let x = NonMaxU8::new(100).unwrap();
assert_eq!(x.checked_add_val(50).unwrap().get(), 150);
assert!(x.checked_add_val(155).is_none()); // 255 is MAXSourcepub fn checked_sub_val(self, rhs: T) -> Option<Self>
pub fn checked_sub_val(self, rhs: T) -> Option<Self>
Checked subtraction with a primitive value.
Sourcepub fn checked_mul_val(self, rhs: T) -> Option<Self>
pub fn checked_mul_val(self, rhs: T) -> Option<Self>
Checked multiplication with a primitive value.
Sourcepub fn checked_div_val(self, rhs: T) -> Option<Self>
pub fn checked_div_val(self, rhs: T) -> Option<Self>
Checked division with a primitive value.
Sourcepub fn checked_rem_val(self, rhs: T) -> Option<Self>
pub fn checked_rem_val(self, rhs: T) -> Option<Self>
Checked remainder with a primitive value.
Source§impl NonMax<u8>
impl NonMax<u8>
Sourcepub const unsafe fn new_unchecked(value: u8) -> Self
pub const unsafe fn new_unchecked(value: u8) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<u16>
impl NonMax<u16>
Sourcepub const unsafe fn new_unchecked(value: u16) -> Self
pub const unsafe fn new_unchecked(value: u16) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<u32>
impl NonMax<u32>
Sourcepub const unsafe fn new_unchecked(value: u32) -> Self
pub const unsafe fn new_unchecked(value: u32) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<u64>
impl NonMax<u64>
Sourcepub const unsafe fn new_unchecked(value: u64) -> Self
pub const unsafe fn new_unchecked(value: u64) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<u128>
impl NonMax<u128>
Sourcepub const unsafe fn new_unchecked(value: u128) -> Self
pub const unsafe fn new_unchecked(value: u128) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<usize>
impl NonMax<usize>
Sourcepub const unsafe fn new_unchecked(value: usize) -> Self
pub const unsafe fn new_unchecked(value: usize) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<i8>
impl NonMax<i8>
Sourcepub const unsafe fn new_unchecked(value: i8) -> Self
pub const unsafe fn new_unchecked(value: i8) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<i16>
impl NonMax<i16>
Sourcepub const unsafe fn new_unchecked(value: i16) -> Self
pub const unsafe fn new_unchecked(value: i16) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<i32>
impl NonMax<i32>
Sourcepub const unsafe fn new_unchecked(value: i32) -> Self
pub const unsafe fn new_unchecked(value: i32) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<i64>
impl NonMax<i64>
Sourcepub const unsafe fn new_unchecked(value: i64) -> Self
pub const unsafe fn new_unchecked(value: i64) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Source§impl NonMax<i128>
impl NonMax<i128>
Sourcepub const unsafe fn new_unchecked(value: i128) -> Self
pub const unsafe fn new_unchecked(value: i128) -> Self
Creates a new NonMax without checking the value.
§Safety
The value must not be the maximum value of the underlying type.
Trait Implementations§
Source§impl<T: NonMaxItem + Copy + Add<Output = T>> AddAssign<T> for NonMax<T>
impl<T: NonMaxItem + Copy + Add<Output = T>> AddAssign<T> for NonMax<T>
Source§fn add_assign(&mut self, rhs: T)
fn add_assign(&mut self, rhs: T)
+= operation. Read moreSource§impl<T: NonMaxItem + Copy + Add<Output = T>> AddAssign for NonMax<T>
impl<T: NonMaxItem + Copy + Add<Output = T>> AddAssign for NonMax<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl<T: NonMaxItem + Copy + Div<T, Output = T>> DivAssign<T> for NonMax<T>
impl<T: NonMaxItem + Copy + Div<T, Output = T>> DivAssign<T> for NonMax<T>
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
/= operation. Read moreSource§impl<T: NonMaxItem + Copy + Div<Output = T>> DivAssign for NonMax<T>
impl<T: NonMaxItem + Copy + Div<Output = T>> DivAssign for NonMax<T>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreSource§impl<T: NonMaxItem + Copy + Mul<Output = T>> MulAssign<T> for NonMax<T>
impl<T: NonMaxItem + Copy + Mul<Output = T>> MulAssign<T> for NonMax<T>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
*= operation. Read moreSource§impl<T: NonMaxItem + Copy + Mul<Output = T>> MulAssign for NonMax<T>
impl<T: NonMaxItem + Copy + Mul<Output = T>> MulAssign for NonMax<T>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moreSource§impl<T: NonMaxItem + Copy + Ord> Ord for NonMax<T>
impl<T: NonMaxItem + Copy + Ord> Ord for NonMax<T>
Source§impl<T: NonMaxItem + Copy + PartialOrd> PartialOrd for NonMax<T>
impl<T: NonMaxItem + Copy + PartialOrd> PartialOrd for NonMax<T>
Source§impl<T: NonMaxItem + Copy + Rem<T, Output = T>> RemAssign<T> for NonMax<T>
impl<T: NonMaxItem + Copy + Rem<T, Output = T>> RemAssign<T> for NonMax<T>
Source§fn rem_assign(&mut self, rhs: T)
fn rem_assign(&mut self, rhs: T)
%= operation. Read moreSource§impl<T: NonMaxItem + Copy + Rem<Output = T>> RemAssign for NonMax<T>
impl<T: NonMaxItem + Copy + Rem<Output = T>> RemAssign for NonMax<T>
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
%= operation. Read moreSource§impl<T: NonMaxItem + Copy + Sub<Output = T>> SubAssign<T> for NonMax<T>
impl<T: NonMaxItem + Copy + Sub<Output = T>> SubAssign<T> for NonMax<T>
Source§fn sub_assign(&mut self, rhs: T)
fn sub_assign(&mut self, rhs: T)
-= operation. Read moreSource§impl<T: NonMaxItem + Copy + Sub<Output = T>> SubAssign for NonMax<T>
impl<T: NonMaxItem + Copy + Sub<Output = T>> SubAssign for NonMax<T>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more