pub struct NonNI16<const N: i16>(/* private fields */);Expand description
16-bit signed integer type that is known not to equal to any single value N.
This enables some memory layout optimization.
For example, Option<NonNI16<N>> is the same size as i16:
use std::mem::size_of;
assert_eq!(size_of::<Option<NonNI16<42>>>(), size_of::<i16>());§Layout
NonNI16\<N> is guaranteed to have the same layout and bit validity as i16
with the exception that N is not a valid instance.
Option<NonNI16> is guaranteed to be compatible with i16,
including in FFI.
Note that this does not mean you can transmute this type to get a i16 of the same value.
If you need an i16 of the same value, use NonNI16::get instead.
Implementations§
Source§impl<const N: i16> NonNI16<N>
impl<const N: i16> NonNI16<N>
Sourcepub const unsafe fn new_unchecked(n: i16) -> Self
pub const unsafe fn new_unchecked(n: i16) -> Self
Creates a NonNI16<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: i16>(self) -> Option<NonNI16<M>>
pub const fn cast<const M: i16>(self) -> Option<NonNI16<M>>
Returns a new NonNI16<M> with the current value if it is not M.
Sourcepub const unsafe fn cast_unchecked<const M: i16>(self) -> NonNI16<M>
pub const unsafe fn cast_unchecked<const M: i16>(self) -> NonNI16<M>
Returns a new NonNI16<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 = NonNI16::<42>::new(-1i16).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 = NonNI16::<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 = NonNI16::<42>::new(2).unwrap();
let four = NonNI16::<42>::new(4).unwrap();
let max = NonNI16::<42>::new(i16::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 NonNI16::<42>::MAX on overflow.
§Examples
let two = NonNI16::<42>::new(2)?;
let four = NonNI16::<42>::new(4)?;
let max = NonNI16::<42>::new(i16::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 > i16::MAX, or self * rhs < i16::MIN.
Raises non-N value to an integer power.
Checks for overflow and returns None on overflow.
§Examples
let three = NonNI16::<42>::new(3)?;
let twenty_seven = NonNI16::<42>::new(27)?;
let half_max = NonNI16::<42>::new(i16::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 NonNI16::<42>::MIN or NonNI16::<42>::MAX on overflow.
§Examples
let three = NonNI16::<42>::new(3)?;
let twenty_seven = NonNI16::<42>::new(27)?;
let max = NonNI16::<42>::new(i16::MAX)?;
assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));Sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
Checked absolute value.
Checks for overflow and returns None if
self == NonNI16::MIN.
The result cannot be N.
§Example
let pos = NonNI16::<42>::new(1)?;
let neg = NonNI16::<42>::new(-1)?;
let min = NonNI16::<42>::new(i16::MIN)?;
assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());Sourcepub const fn overflowing_abs(self) -> (Self, bool)
pub const fn overflowing_abs(self) -> (Self, bool)
Computes the absolute value of self,
with overflow information, see
i16::overflowing_abs.
§Example
let pos = NonNI16::<42>::new(1)?;
let neg = NonNI16::<42>::new(-1)?;
let min = NonNI16::<42>::new(i16::MIN)?;
assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());Sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value, see
i16::saturating_abs.
§Example
let pos = NonNI16::<42>::new(1)?;
let neg = NonNI16::<42>::new(-1)?;
let min = NonNI16::<42>::new(i16::MIN)?;
let min_plus = NonNI16::<42>::new(i16::MIN + 1)?;
let max = NonNI16::<42>::new(i16::MAX)?;
assert_eq!(pos, pos.saturating_abs());
assert_eq!(pos, neg.saturating_abs());
assert_eq!(max, min.saturating_abs());
assert_eq!(max, min_plus.saturating_abs());Sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value, see
i16::wrapping_abs.
§Example
let pos = NonNI16::<42>::new(1)?;
let neg = NonNI16::<42>::new(-1)?;
let min = NonNI16::<42>::new(i16::MIN)?;
assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());Sourcepub const fn is_positive(self) -> bool
pub const fn is_positive(self) -> bool
Returns true if self is positive and false if the
number is negative.
§Example
let pos_five = NonNI16::<42>::new(5).unwrap();
let neg_five = NonNI16::<42>::new(-5).unwrap();
assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());Sourcepub const fn is_negative(self) -> bool
pub const fn is_negative(self) -> bool
Returns true if self is negative and false if the
number is positive.
§Example
let pos_five = NonNI16::<42>::new(5)?;
let neg_five = NonNI16::<42>::new(-5)?;
assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());Sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Checked negation. Computes -self,
returning None if self == NonNI16::MIN.
§Example
let pos_five = NonNI16::<42>::new(5)?;
let neg_five = NonNI16::<42>::new(-5)?;
let min = NonNI16::<42>::new(i16::MIN)?;
assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);Sourcepub const fn overflowing_neg(self) -> (Self, bool)
pub const fn overflowing_neg(self) -> (Self, bool)
Negates self, overflowing if this is equal to the minimum value.
See i16::overflowing_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonNI16::<42>::new(5)?;
let neg_five = NonNI16::<42>::new(-5)?;
let min = NonNI16::<42>::new(i16::MIN)?;
assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));Sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation. Computes -self,
returning NonNI16::<42>::MAX
if self == NonNI16::<42>::MIN
instead of overflowing.
§Example
let pos_five = NonNI16::<42>::new(5)?;
let neg_five = NonNI16::<42>::new(-5)?;
let min = NonNI16::<42>::new(i16::MIN)?;
let min_plus_one = NonNI16::<42>::new(i16::MIN + 1)?;
let max = NonNI16::<42>::new(i16::MAX)?;
assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);Sourcepub const fn wrapping_neg(self) -> Self
pub const fn wrapping_neg(self) -> Self
Wrapping (modular) negation. Computes -self, wrapping around at the boundary
of the type.
See i16::wrapping_neg
for documentation on overflow behaviour.
§Example
let pos_five = NonNI16::<42>::new(5)?;
let neg_five = NonNI16::<42>::new(-5)?;
let min = NonNI16::<42>::new(i16::MIN)?;
assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);Trait Implementations§
Source§impl<const N: i16, const M: i16> AddAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> AddAssign<NonNI16<M>> for NonNI16<N>
Source§fn add_assign(&mut self, rhs: NonNI16<M>)
fn add_assign(&mut self, rhs: NonNI16<M>)
+= operation. Read moreSource§impl<const N: i16> AddAssign<i16> for NonNI16<N>
impl<const N: i16> AddAssign<i16> for NonNI16<N>
Source§fn add_assign(&mut self, rhs: i16)
fn add_assign(&mut self, rhs: i16)
+= operation. Read moreSource§impl<const N: i16, const M: i16> BitAndAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> BitAndAssign<NonNI16<M>> for NonNI16<N>
Source§fn bitand_assign(&mut self, rhs: NonNI16<M>)
fn bitand_assign(&mut self, rhs: NonNI16<M>)
&= operation. Read moreSource§impl<const N: i16> BitAndAssign<i16> for NonNI16<N>
impl<const N: i16> BitAndAssign<i16> for NonNI16<N>
Source§fn bitand_assign(&mut self, rhs: i16)
fn bitand_assign(&mut self, rhs: i16)
&= operation. Read moreSource§impl<const N: i16, const M: i16> BitOrAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> BitOrAssign<NonNI16<M>> for NonNI16<N>
Source§fn bitor_assign(&mut self, rhs: NonNI16<M>)
fn bitor_assign(&mut self, rhs: NonNI16<M>)
|= operation. Read moreSource§impl<const N: i16> BitOrAssign<i16> for NonNI16<N>
impl<const N: i16> BitOrAssign<i16> for NonNI16<N>
Source§fn bitor_assign(&mut self, rhs: i16)
fn bitor_assign(&mut self, rhs: i16)
|= operation. Read moreSource§impl<const N: i16, const M: i16> BitXorAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> BitXorAssign<NonNI16<M>> for NonNI16<N>
Source§fn bitxor_assign(&mut self, rhs: NonNI16<M>)
fn bitxor_assign(&mut self, rhs: NonNI16<M>)
^= operation. Read moreSource§impl<const N: i16> BitXorAssign<i16> for NonNI16<N>
impl<const N: i16> BitXorAssign<i16> for NonNI16<N>
Source§fn bitxor_assign(&mut self, rhs: i16)
fn bitxor_assign(&mut self, rhs: i16)
^= operation. Read moreSource§impl<const N: i16, const M: i16> DivAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> DivAssign<NonNI16<M>> for NonNI16<N>
Source§fn div_assign(&mut self, rhs: NonNI16<M>)
fn div_assign(&mut self, rhs: NonNI16<M>)
/= operation. Read moreSource§impl<const N: i16> DivAssign<i16> for NonNI16<N>
impl<const N: i16> DivAssign<i16> for NonNI16<N>
Source§fn div_assign(&mut self, rhs: i16)
fn div_assign(&mut self, rhs: i16)
/= operation. Read moreSource§impl<const N: i16, const M: i16> MulAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> MulAssign<NonNI16<M>> for NonNI16<N>
Source§fn mul_assign(&mut self, rhs: NonNI16<M>)
fn mul_assign(&mut self, rhs: NonNI16<M>)
*= operation. Read moreSource§impl<const N: i16> MulAssign<i16> for NonNI16<N>
impl<const N: i16> MulAssign<i16> for NonNI16<N>
Source§fn mul_assign(&mut self, rhs: i16)
fn mul_assign(&mut self, rhs: i16)
*= operation. Read moreSource§impl<const N: i16> Ord for NonNI16<N>
impl<const N: i16> Ord for NonNI16<N>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const N: i16> PartialOrd for NonNI16<N>
impl<const N: i16> PartialOrd for NonNI16<N>
Source§impl<const N: i16, const M: i16> RemAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> RemAssign<NonNI16<M>> for NonNI16<N>
Source§fn rem_assign(&mut self, rhs: NonNI16<M>)
fn rem_assign(&mut self, rhs: NonNI16<M>)
%= operation. Read moreSource§impl<const N: i16> RemAssign<i16> for NonNI16<N>
impl<const N: i16> RemAssign<i16> for NonNI16<N>
Source§fn rem_assign(&mut self, rhs: i16)
fn rem_assign(&mut self, rhs: i16)
%= operation. Read moreSource§impl<const N: i16, const M: i16> ShlAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> ShlAssign<NonNI16<M>> for NonNI16<N>
Source§fn shl_assign(&mut self, rhs: NonNI16<M>)
fn shl_assign(&mut self, rhs: NonNI16<M>)
<<= operation. Read moreSource§impl<const N: i16> ShlAssign<i16> for NonNI16<N>
impl<const N: i16> ShlAssign<i16> for NonNI16<N>
Source§fn shl_assign(&mut self, rhs: i16)
fn shl_assign(&mut self, rhs: i16)
<<= operation. Read moreSource§impl<const N: i16, const M: i16> ShrAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> ShrAssign<NonNI16<M>> for NonNI16<N>
Source§fn shr_assign(&mut self, rhs: NonNI16<M>)
fn shr_assign(&mut self, rhs: NonNI16<M>)
>>= operation. Read moreSource§impl<const N: i16> ShrAssign<i16> for NonNI16<N>
impl<const N: i16> ShrAssign<i16> for NonNI16<N>
Source§fn shr_assign(&mut self, rhs: i16)
fn shr_assign(&mut self, rhs: i16)
>>= operation. Read moreSource§impl<const N: i16, const M: i16> SubAssign<NonNI16<M>> for NonNI16<N>
impl<const N: i16, const M: i16> SubAssign<NonNI16<M>> for NonNI16<N>
Source§fn sub_assign(&mut self, rhs: NonNI16<M>)
fn sub_assign(&mut self, rhs: NonNI16<M>)
-= operation. Read moreSource§impl<const N: i16> SubAssign<i16> for NonNI16<N>
impl<const N: i16> SubAssign<i16> for NonNI16<N>
Source§fn sub_assign(&mut self, rhs: i16)
fn sub_assign(&mut self, rhs: i16)
-= operation. Read more