pub type NonZeroI16 = NonZero<i16>;
Expand description
An integer that is known not to equal zero.
This enables some memory layout optimization.
For example, Option<NonZeroI16>
is the same size as i16
:
§Layout
NonZeroI16
is guaranteed to have the same layout and bit validity as i16
with the exception that 0
is not a valid instance.
Option<NonZeroI16>
is guaranteed to be compatible with i16
,
including in FFI.
Thanks to the null pointer optimization,
NonZeroI16
and Option<NonZeroI16>
are guaranteed to have the same size and alignment:
Aliased Type§
struct NonZeroI16(/* private fields */);
Implementations
source§impl<T> NonZero<T>where
T: ZeroablePrimitive,
impl<T> NonZero<T>where
T: ZeroablePrimitive,
1.28.0 (const: 1.47.0) · sourcepub const fn new(n: T) -> Option<Self>
pub const fn new(n: T) -> Option<Self>
Creates a non-zero if the given value is not zero.
1.28.0 (const: 1.28.0) · sourcepub const unsafe fn new_unchecked(n: T) -> Self
pub const unsafe fn new_unchecked(n: T) -> Self
Creates a non-zero without checking whether the value is non-zero. This results in undefined behaviour if the value is zero.
§Safety
The value must not be zero.
sourcepub fn from_mut(n: &mut T) -> Option<&mut Self>
🔬This is a nightly-only experimental API. (nonzero_from_mut
#106290)
pub fn from_mut(n: &mut T) -> Option<&mut Self>
nonzero_from_mut
#106290)Converts a reference to a non-zero mutable reference if the referenced value is not zero.
sourcepub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self
🔬This is a nightly-only experimental API. (nonzero_from_mut
#106290)
pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self
nonzero_from_mut
#106290)Converts a mutable reference to a non-zero mutable reference without checking whether the referenced value is non-zero. This results in undefined behavior if the referenced value is zero.
§Safety
The referenced value must not be zero.
source§impl NonZero<i16>
impl NonZero<i16>
1.70.0 · sourcepub const MIN: Self = _
pub const MIN: Self = _
1.70.0 · sourcepub const MAX: Self = _
pub const MAX: Self = _
1.53.0 (const: 1.53.0) · 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
.
On many architectures, this function can perform better than leading_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
1.53.0 (const: 1.53.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
.
On many architectures, this function can perform better than trailing_zeros()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
sourcepub const fn count_ones(self) -> NonZero<u32>
🔬This is a nightly-only experimental API. (non_zero_count_ones
#120287)
pub const fn count_ones(self) -> NonZero<u32>
non_zero_count_ones
#120287)sourcepub const fn rotate_left(self, n: u32) -> Self
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn rotate_left(self, n: u32) -> Self
nonzero_bitwise
#128281)Shifts the bits to the left by a specified amount, n
,
wrapping the truncated bits to the end of the resulting integer.
Please note this isn’t the same operation as the <<
shifting operator!
§Examples
Basic usage:
sourcepub const fn rotate_right(self, n: u32) -> Self
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn rotate_right(self, n: u32) -> Self
nonzero_bitwise
#128281)Shifts the bits to the right by a specified amount, n
,
wrapping the truncated bits to the beginning of the resulting
integer.
Please note this isn’t the same operation as the >>
shifting operator!
§Examples
Basic usage:
sourcepub const fn swap_bytes(self) -> Self
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn swap_bytes(self) -> Self
nonzero_bitwise
#128281)sourcepub const fn reverse_bits(self) -> Self
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn reverse_bits(self) -> Self
nonzero_bitwise
#128281)Reverses the order of bits in the integer. The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.
§Examples
Basic usage:
sourcepub const fn from_be(x: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn from_be(x: Self) -> Self
nonzero_bitwise
#128281)Converts an integer from big endian to the target’s endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
§Examples
Basic usage:
sourcepub const fn from_le(x: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn from_le(x: Self) -> Self
nonzero_bitwise
#128281)Converts an integer from little endian to the target’s endianness.
On little endian this is a no-op. On big endian the bytes are swapped.
§Examples
Basic usage:
sourcepub const fn to_be(self) -> Self
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn to_be(self) -> Self
nonzero_bitwise
#128281)Converts self
to big endian from the target’s endianness.
On big endian this is a no-op. On little endian the bytes are swapped.
§Examples
Basic usage:
sourcepub const fn to_le(self) -> Self
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn to_le(self) -> Self
nonzero_bitwise
#128281)Converts self
to little endian from the target’s endianness.
On little endian this is a no-op. On big endian the bytes are swapped.
§Examples
Basic usage:
1.64.0 (const: 1.64.0) · sourcepub const fn checked_abs(self) -> Option<Self>
pub const fn checked_abs(self) -> Option<Self>
1.64.0 (const: 1.64.0) · 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
1.64.0 (const: 1.64.0) · sourcepub const fn saturating_abs(self) -> Self
pub const fn saturating_abs(self) -> Self
Saturating absolute value, see
i16::saturating_abs
.
§Example
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;
let min = NonZero::new(i16::MIN)?;
let min_plus = NonZero::new(i16::MIN + 1)?;
let max = NonZero::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());
1.64.0 (const: 1.64.0) · sourcepub const fn wrapping_abs(self) -> Self
pub const fn wrapping_abs(self) -> Self
Wrapping absolute value, see
i16::wrapping_abs
.
§Example
1.64.0 (const: 1.64.0) · sourcepub const fn unsigned_abs(self) -> NonZero<u16>
pub const fn unsigned_abs(self) -> NonZero<u16>
Computes the absolute value of self without any wrapping or panicking.
§Example
let u_pos = NonZero::new(1u16)?;
let i_pos = NonZero::new(1i16)?;
let i_neg = NonZero::new(-1i16)?;
let i_min = NonZero::new(i16::MIN)?;
let u_max = NonZero::new(u16::MAX / 2 + 1)?;
assert_eq!(u_pos, i_pos.unsigned_abs());
assert_eq!(u_pos, i_neg.unsigned_abs());
assert_eq!(u_max, i_min.unsigned_abs());
1.71.0 (const: 1.71.0) · 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
1.71.0 (const: 1.71.0) · 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
1.71.0 (const: 1.71.0) · sourcepub const fn checked_neg(self) -> Option<Self>
pub const fn checked_neg(self) -> Option<Self>
Checked negation. Computes -self
,
returning None
if self == NonZero::<i16>::MIN
.
§Example
1.71.0 (const: 1.71.0) · 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
1.71.0 (const: 1.71.0) · sourcepub const fn saturating_neg(self) -> Self
pub const fn saturating_neg(self) -> Self
Saturating negation. Computes -self
,
returning NonZero::<i16>::MAX
if self == NonZero::<i16>::MIN
instead of overflowing.
§Example
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
let min = NonZero::new(i16::MIN)?;
let min_plus_one = NonZero::new(i16::MIN + 1)?;
let max = NonZero::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);
1.71.0 (const: 1.71.0) · 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
1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: Self) -> Option<Self>
pub const fn checked_mul(self, other: Self) -> Option<Self>
1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: Self) -> Self
pub const fn saturating_mul(self, other: Self) -> Self
Multiplies two non-zero integers together.
Return NonZero::<i16>::MAX
on overflow.
§Examples
sourcepub const unsafe fn unchecked_mul(self, other: Self) -> Self
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: Self) -> Self
nonzero_ops
#84186)Multiplies two non-zero 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-zero value.
The behaviour is undefined as soon as
self * rhs > i16::MAX
, or self * rhs < i16::MIN
.
§Examples
1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<Self>
pub const fn checked_pow(self, other: u32) -> Option<Self>
1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> Self
pub const fn saturating_pow(self, other: u32) -> Self
Raise non-zero value to an integer power.
Return NonZero::<i16>::MIN
or NonZero::<i16>::MAX
on overflow.
§Examples
Trait Implementations
1.45.0 · source§impl<T> BitOrAssign<T> for NonZero<T>where
T: ZeroablePrimitive,
Self: BitOr<T, Output = Self>,
impl<T> BitOrAssign<T> for NonZero<T>where
T: ZeroablePrimitive,
Self: BitOr<T, Output = Self>,
source§fn bitor_assign(&mut self, rhs: T)
fn bitor_assign(&mut self, rhs: T)
|=
operation. Read more1.45.0 · source§impl<T> BitOrAssign for NonZero<T>where
T: ZeroablePrimitive,
Self: BitOr<Output = Self>,
impl<T> BitOrAssign for NonZero<T>where
T: ZeroablePrimitive,
Self: BitOr<Output = Self>,
source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
|=
operation. Read more