pub type NonZeroU16 = NonZero<u16>;
Expand description
An integer that is known not to equal zero.
This enables some memory layout optimization.
For example, Option<NonZeroU16>
is the same size as u16
:
§Layout
NonZeroU16
is guaranteed to have the same layout and bit validity as u16
with the exception that 0
is not a valid instance.
Option<NonZeroU16>
is guaranteed to be compatible with u16
,
including in FFI.
Thanks to the null pointer optimization,
NonZeroU16
and Option<NonZeroU16>
are guaranteed to have the same size and alignment:
Aliased Type§
struct NonZeroU16(/* 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<NonZero<T>>
pub const fn new(n: T) -> Option<NonZero<T>>
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) -> NonZero<T>
pub const unsafe fn new_unchecked(n: T) -> NonZero<T>
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 NonZero<T>>
🔬This is a nightly-only experimental API. (nonzero_from_mut
#106290)
pub fn from_mut(n: &mut T) -> Option<&mut NonZero<T>>
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 NonZero<T>
🔬This is a nightly-only experimental API. (nonzero_from_mut
#106290)
pub unsafe fn from_mut_unchecked(n: &mut T) -> &mut NonZero<T>
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<u16>
impl NonZero<u16>
1.70.0 · sourcepub const MIN: NonZero<u16> = _
pub const MIN: NonZero<u16> = _
The smallest value that can be represented by this non-zero integer type, 1.
§Examples
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) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn rotate_left(self, n: u32) -> NonZero<u16>
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) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn rotate_right(self, n: u32) -> NonZero<u16>
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) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn swap_bytes(self) -> NonZero<u16>
nonzero_bitwise
#128281)sourcepub const fn reverse_bits(self) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn reverse_bits(self) -> NonZero<u16>
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: NonZero<u16>) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn from_be(x: NonZero<u16>) -> NonZero<u16>
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: NonZero<u16>) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn from_le(x: NonZero<u16>) -> NonZero<u16>
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) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn to_be(self) -> NonZero<u16>
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) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn to_le(self) -> NonZero<u16>
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_add(self, other: u16) -> Option<NonZero<u16>>
pub const fn checked_add(self, other: u16) -> Option<NonZero<u16>>
1.64.0 (const: 1.64.0) · sourcepub const fn saturating_add(self, other: u16) -> NonZero<u16>
pub const fn saturating_add(self, other: u16) -> NonZero<u16>
Adds an unsigned integer to a non-zero value.
Return NonZero::<u16>::MAX
on overflow.
§Examples
sourcepub const unsafe fn unchecked_add(self, other: u16) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_add(self, other: u16) -> NonZero<u16>
nonzero_ops
#84186)Adds an unsigned integer to a non-zero value,
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 > u16::MAX
.
§Examples
1.64.0 (const: 1.64.0) · sourcepub const fn checked_next_power_of_two(self) -> Option<NonZero<u16>>
pub const fn checked_next_power_of_two(self) -> Option<NonZero<u16>>
1.67.0 (const: 1.67.0) · sourcepub const fn ilog2(self) -> u32
pub const fn ilog2(self) -> u32
Returns the base 2 logarithm of the number, rounded down.
This is the same operation as
u16::ilog2
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
1.67.0 (const: 1.67.0) · sourcepub const fn ilog10(self) -> u32
pub const fn ilog10(self) -> u32
Returns the base 10 logarithm of the number, rounded down.
This is the same operation as
u16::ilog10
,
except that it has no failure cases to worry about
since this value can never be zero.
§Examples
sourcepub const fn midpoint(self, rhs: NonZero<u16>) -> NonZero<u16>
🔬This is a nightly-only experimental API. (num_midpoint
#110840)
pub const fn midpoint(self, rhs: NonZero<u16>) -> NonZero<u16>
num_midpoint
#110840)Calculates the middle point of self
and rhs
.
midpoint(a, b)
is (a + b) >> 1
as if it were performed in a
sufficiently-large signed integral type. This implies that the result is
always rounded towards negative infinity and that no overflow will ever occur.
§Examples
1.59.0 (const: 1.59.0) · 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
.
On many architectures, this function can perform better than is_power_of_two()
on the underlying integer type, as special handling of zero can be avoided.
§Examples
Basic usage:
sourcepub const fn isqrt(self) -> NonZero<u16>
🔬This is a nightly-only experimental API. (isqrt
#116226)
pub const fn isqrt(self) -> NonZero<u16>
isqrt
#116226)1.64.0 (const: 1.64.0) · sourcepub const fn checked_mul(self, other: NonZero<u16>) -> Option<NonZero<u16>>
pub const fn checked_mul(self, other: NonZero<u16>) -> Option<NonZero<u16>>
1.64.0 (const: 1.64.0) · sourcepub const fn saturating_mul(self, other: NonZero<u16>) -> NonZero<u16>
pub const fn saturating_mul(self, other: NonZero<u16>) -> NonZero<u16>
Multiplies two non-zero integers together.
Return NonZero::<u16>::MAX
on overflow.
§Examples
sourcepub const unsafe fn unchecked_mul(self, other: NonZero<u16>) -> NonZero<u16>
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: NonZero<u16>) -> NonZero<u16>
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 > u16::MAX
.
§Examples
1.64.0 (const: 1.64.0) · sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<u16>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<u16>>
1.64.0 (const: 1.64.0) · sourcepub const fn saturating_pow(self, other: u32) -> NonZero<u16>
pub const fn saturating_pow(self, other: u32) -> NonZero<u16>
Raise non-zero value to an integer power.
Return NonZero::<u16>::MAX
on overflow.
§Examples
Trait Implementations
1.45.0 · source§impl<T> BitOrAssign<T> for NonZero<T>
impl<T> BitOrAssign<T> for NonZero<T>
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>
impl<T> BitOrAssign for NonZero<T>
source§fn bitor_assign(&mut self, rhs: NonZero<T>)
fn bitor_assign(&mut self, rhs: NonZero<T>)
|=
operation. Read more