pub type NonZeroU32 = NonZero<u32>;Expand description
⅀ ⚗️
core
An unsigned integer that is known not to equal zero.
📍num/grain/niche re-exported from core::num
📜
A u32 that is known not to equal zero.
This enables some memory layout optimization.
For example, Option<NonZeroU32> is the same size as u32:
assert_eq!(size_of::<Option<core::num::NonZeroU32>>(), size_of::<u32>());§Layout
NonZeroU32 is guaranteed to have the same layout and bit validity as u32
with the exception that 0 is not a valid instance.
Option<NonZeroU32> is guaranteed to be compatible with u32,
including in FFI.
Thanks to the null pointer optimization,
NonZeroU32 and Option<NonZeroU32>
are guaranteed to have the same size and alignment:
use std::num::NonZeroU32;
assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
assert_eq!(align_of::<NonZeroU32>(), align_of::<Option<NonZeroU32>>());§Compile-time creation
Since both Option::unwrap() and Option::expect() are const, it is possible to
define a new
NonZeroU32
at compile time via:
use std::num::NonZeroU32;
const TEN: NonZeroU32 = NonZeroU32::new(10).expect("ten is non-zero");Aliased Type§
pub struct NonZeroU32(/* private fields */);Trait Implementations§
Source§impl BitSized<32> for NonZeroU32
impl BitSized<32> for NonZeroU32
Source§const BIT_SIZE: usize = _
const BIT_SIZE: usize = _
Source§const MIN_BYTE_SIZE: usize = _
const MIN_BYTE_SIZE: usize = _
Source§fn bit_size(&self) -> usize
fn bit_size(&self) -> usize
Source§fn min_byte_size(&self) -> usize
fn min_byte_size(&self) -> usize
Source§impl ConstInit for NonZeroU32
This implementation returns the same as Self::MIN.
impl ConstInit for NonZeroU32
This implementation returns the same as Self::MIN.
Source§impl Num for NonZeroU32
impl Num for NonZeroU32
Source§fn num_set_one(&mut self) -> Result<()>
fn num_set_one(&mut self) -> Result<()>
§Features
Makes use of the unsafe_niche feature if enabled.
Source§fn num_from_ref(from: &Self::Inner) -> Result<Self>
fn num_from_ref(from: &Self::Inner) -> Result<Self>
Self if given a valid &value.Source§fn num_set_ref(&mut self, value: &Self::Inner) -> Result<()>
fn num_set_ref(&mut self, value: &Self::Inner) -> Result<()>
self to the given valid &value.Source§fn num_is_zero(&self) -> Result<bool>
fn num_is_zero(&self) -> Result<bool>
true if self is zero.Source§fn num_is_one(&self) -> Result<bool>
fn num_is_one(&self) -> Result<bool>
true if self is one.Source§fn num_get_zero() -> Result<Self>
fn num_get_zero() -> Result<Self>
Source§fn num_get_one() -> Result<Self>
fn num_get_one() -> Result<Self>
Source§fn num_set_zero(&mut self) -> Result<()>
fn num_set_zero(&mut self) -> Result<()>
self to 0.Source§fn num_ref_mul(&self, other: &Self) -> Result<Self::Out>
fn num_ref_mul(&self, other: &Self) -> Result<Self::Out>
num_mul but takes the arguments by reference.Source§fn num_ref_mul_assign(&mut self, other: &Self) -> Result<()>
fn num_ref_mul_assign(&mut self, other: &Self) -> Result<()>
&mut self *= rhs; (multiplication).Source§fn num_ref_add(&self, other: &Self) -> Result<Self::Out>
fn num_ref_add(&self, other: &Self) -> Result<Self::Out>
num_add but takes the arguments by reference.Source§fn num_ref_add_assign(&mut self, other: &Self) -> Result<()>
fn num_ref_add_assign(&mut self, other: &Self) -> Result<()>
&mut self += rhs; (addition).Source§fn num_ref_sub(&self, other: &Self) -> Result<Self::Out>
fn num_ref_sub(&self, other: &Self) -> Result<Self::Out>
num_sub but takes the arguments by reference.Source§fn num_ref_sub_assign(&mut self, other: &Self) -> Result<()>
fn num_ref_sub_assign(&mut self, other: &Self) -> Result<()>
&mut self -= rhs; (subtraction).Source§fn num_ref_div(&self, other: &Self) -> Result<Self::Out>
fn num_ref_div(&self, other: &Self) -> Result<Self::Out>
num_div but takes the arguments by reference.Source§fn num_ref_div_assign(&mut self, other: &Self) -> Result<()>
fn num_ref_div_assign(&mut self, other: &Self) -> Result<()>
&mut self /= rhs; (division).Source§fn num_ref_rem(&self, other: &Self) -> Result<Self::Out>
fn num_ref_rem(&self, other: &Self) -> Result<Self::Out>
num_rem but takes the arguments by reference.Source§fn num_ref_rem_assign(&mut self, other: &Self) -> Result<()>
fn num_ref_rem_assign(&mut self, other: &Self) -> Result<()>
&mut self %= rhs; (remainder).Source§fn num_ref_abs(&self) -> Result<Self>
fn num_ref_abs(&self) -> Result<Self>
num_abs but takes the arguments by reference.Source§impl NumConst for NonZeroU32
impl NumConst for NonZeroU32
Source§const NUM_IS_BIG: bool = false
const NUM_IS_BIG: bool = false
Source§const NUM_IS_INT: bool = true
const NUM_IS_INT: bool = true
Source§const NUM_IS_FLOAT: bool = false
const NUM_IS_FLOAT: bool = false
Source§const NUM_IS_FIXED: bool = false
const NUM_IS_FIXED: bool = false
Source§const NUM_IS_SIGNED: bool = false
const NUM_IS_SIGNED: bool = false
Source§const NUM_IS_NICHE: bool = true
const NUM_IS_NICHE: bool = true
Source§const NUM_ZERO: Option<NonZeroU32>
const NUM_ZERO: Option<NonZeroU32>
0), if applicable.Source§const NUM_ONE: Option<NonZeroU32>
const NUM_ONE: Option<NonZeroU32>
1).Source§const NUM_TWO: Option<NonZeroU32>
const NUM_TWO: Option<NonZeroU32>
2).Source§const NUM_THREE: Option<NonZeroU32>
const NUM_THREE: Option<NonZeroU32>
3).Source§const NUM_NEG_ONE: Option<NonZeroU32> = None
const NUM_NEG_ONE: Option<NonZeroU32> = None
ONE (-1), if applicable.Source§const NUM_MIN: Option<NonZeroU32>
const NUM_MIN: Option<NonZeroU32>
Source§const NUM_MAX: Option<NonZeroU32>
const NUM_MAX: Option<NonZeroU32>
Source§const NUM_MIN_POSITIVE: Option<NonZeroU32>
const NUM_MIN_POSITIVE: Option<NonZeroU32>
Source§const NUM_MAX_NEGATIVE: Option<NonZeroU32> = None
const NUM_MAX_NEGATIVE: Option<NonZeroU32> = None
Source§const NUM_MIN_NORM: Option<NonZeroU32>
const NUM_MIN_NORM: Option<NonZeroU32>
MIN for integers).Source§const NUM_MAX_NORM: Option<NonZeroU32>
const NUM_MAX_NORM: Option<NonZeroU32>
MAX for integers).Source§const NUM_MAX_POWER_OF_TWO: Option<NonZeroU32>
const NUM_MAX_POWER_OF_TWO: Option<NonZeroU32>
Source§fn is_num_zero(&self) -> bool
fn is_num_zero(&self) -> bool
self is equal to NUM_ZERO.Source§fn is_num_one(&self) -> bool
fn is_num_one(&self) -> bool
self is equal to NUM_ONE.Source§fn is_num_two(&self) -> bool
fn is_num_two(&self) -> bool
self is equal to NUM_TWO.Source§fn is_num_three(&self) -> bool
fn is_num_three(&self) -> bool
self is equal to NUM_THREE.Source§fn is_num_neg_one(&self) -> bool
fn is_num_neg_one(&self) -> bool
self is equal to NUM_NEG_ONE.Source§fn is_num_min(&self) -> bool
fn is_num_min(&self) -> bool
self is equal to NUM_MIN.Source§fn is_num_max(&self) -> bool
fn is_num_max(&self) -> bool
self is equal to NUM_MAX.Source§fn is_num_min_positive(&self) -> bool
fn is_num_min_positive(&self) -> bool
Source§fn is_num_max_negative(&self) -> bool
fn is_num_max_negative(&self) -> bool
self is equal to NUM_MAX_NEGATIVE.Source§fn is_num_min_norm(&self) -> bool
fn is_num_min_norm(&self) -> bool
self is equal to NUM_MIN_NORM.Source§fn is_num_max_norm(&self) -> bool
fn is_num_max_norm(&self) -> bool
self is equal to NUM_MAX_NORM.Source§fn is_num_max_power_of_two(&self) -> bool
fn is_num_max_power_of_two(&self) -> bool
self is equal to NUM_MAX_POWER_OF_TWO.