pub type NonZeroI8 = NonZero<i8>;
Expand description
An i8
that is known not to equal zero.
This enables some memory layout optimization.
For example, Option<NonZeroI8>
is the same size as i8
:
§Layout
NonZeroI8
is guaranteed to have the same layout and bit validity as i8
with the exception that 0
is not a valid instance.
Option<NonZeroI8>
is guaranteed to be compatible with i8
,
including in FFI.
Thanks to the null pointer optimization,
NonZeroI8
and Option<NonZeroI8>
are guaranteed to have the same size and alignment:
Aliased Type§
struct NonZeroI8(/* 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 behavior 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<i8>
impl NonZero<i8>
1.70.0 · Sourcepub const MIN: NonZero<i8> = _
pub const MIN: NonZero<i8> = _
1.70.0 · Sourcepub const MAX: NonZero<i8> = _
pub const MAX: NonZero<i8> = _
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<i8>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn rotate_left(self, n: u32) -> NonZero<i8>
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<i8>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn rotate_right(self, n: u32) -> NonZero<i8>
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<i8>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn swap_bytes(self) -> NonZero<i8>
nonzero_bitwise
#128281)Sourcepub const fn reverse_bits(self) -> NonZero<i8>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn reverse_bits(self) -> NonZero<i8>
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<i8>) -> NonZero<i8>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn from_be(x: NonZero<i8>) -> NonZero<i8>
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<i8>) -> NonZero<i8>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn from_le(x: NonZero<i8>) -> NonZero<i8>
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<i8>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn to_be(self) -> NonZero<i8>
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<i8>
🔬This is a nightly-only experimental API. (nonzero_bitwise
#128281)
pub const fn to_le(self) -> NonZero<i8>
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<NonZero<i8>>
pub const fn checked_abs(self) -> Option<NonZero<i8>>
1.64.0 (const: 1.64.0) · Sourcepub const fn overflowing_abs(self) -> (NonZero<i8>, bool)
pub const fn overflowing_abs(self) -> (NonZero<i8>, bool)
Computes the absolute value of self,
with overflow information, see
i8::overflowing_abs
.
§Example
1.64.0 (const: 1.64.0) · Sourcepub const fn saturating_abs(self) -> NonZero<i8>
pub const fn saturating_abs(self) -> NonZero<i8>
Saturating absolute value, see
i8::saturating_abs
.
§Example
let pos = NonZero::new(1i8)?;
let neg = NonZero::new(-1i8)?;
let min = NonZero::new(i8::MIN)?;
let min_plus = NonZero::new(i8::MIN + 1)?;
let max = NonZero::new(i8::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) -> NonZero<i8>
pub const fn wrapping_abs(self) -> NonZero<i8>
Wrapping absolute value, see
i8::wrapping_abs
.
§Example
1.64.0 (const: 1.64.0) · Sourcepub const fn unsigned_abs(self) -> NonZero<u8>
pub const fn unsigned_abs(self) -> NonZero<u8>
Computes the absolute value of self without any wrapping or panicking.
§Example
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<NonZero<i8>>
pub const fn checked_neg(self) -> Option<NonZero<i8>>
Checked negation. Computes -self
,
returning None
if self == NonZero::<i8>::MIN
.
§Example
1.71.0 (const: 1.71.0) · Sourcepub const fn overflowing_neg(self) -> (NonZero<i8>, bool)
pub const fn overflowing_neg(self) -> (NonZero<i8>, bool)
Negates self, overflowing if this is equal to the minimum value.
See i8::overflowing_neg
for documentation on overflow behavior.
§Example
1.71.0 (const: 1.71.0) · Sourcepub const fn saturating_neg(self) -> NonZero<i8>
pub const fn saturating_neg(self) -> NonZero<i8>
Saturating negation. Computes -self
,
returning NonZero::<i8>::MAX
if self == NonZero::<i8>::MIN
instead of overflowing.
§Example
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
let min = NonZero::new(i8::MIN)?;
let min_plus_one = NonZero::new(i8::MIN + 1)?;
let max = NonZero::new(i8::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) -> NonZero<i8>
pub const fn wrapping_neg(self) -> NonZero<i8>
Wrapping (modular) negation. Computes -self
, wrapping around at the boundary
of the type.
See i8::wrapping_neg
for documentation on overflow behavior.
§Example
1.64.0 (const: 1.64.0) · Sourcepub const fn checked_mul(self, other: NonZero<i8>) -> Option<NonZero<i8>>
pub const fn checked_mul(self, other: NonZero<i8>) -> Option<NonZero<i8>>
1.64.0 (const: 1.64.0) · Sourcepub const fn saturating_mul(self, other: NonZero<i8>) -> NonZero<i8>
pub const fn saturating_mul(self, other: NonZero<i8>) -> NonZero<i8>
Multiplies two non-zero integers together.
Return NonZero::<i8>::MAX
on overflow.
§Examples
Sourcepub const unsafe fn unchecked_mul(self, other: NonZero<i8>) -> NonZero<i8>
🔬This is a nightly-only experimental API. (nonzero_ops
#84186)
pub const unsafe fn unchecked_mul(self, other: NonZero<i8>) -> NonZero<i8>
nonzero_ops
#84186)Multiplies two non-zero integers together,
assuming overflow cannot occur.
Overflow is unchecked, and it is undefined behavior to overflow
even if the result would wrap to a non-zero value.
The behavior is undefined as soon as
self * rhs > i8::MAX
, or self * rhs < i8::MIN
.
§Examples
1.64.0 (const: 1.64.0) · Sourcepub const fn checked_pow(self, other: u32) -> Option<NonZero<i8>>
pub const fn checked_pow(self, other: u32) -> Option<NonZero<i8>>
1.64.0 (const: 1.64.0) · Sourcepub const fn saturating_pow(self, other: u32) -> NonZero<i8>
pub const fn saturating_pow(self, other: u32) -> NonZero<i8>
Raise non-zero value to an integer power.
Return NonZero::<i8>::MIN
or NonZero::<i8>::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