NonZero

Struct NonZero 

1.79.0 · Source
pub struct NonZero<T: ZeroablePrimitive>(/* private fields */);
Expand description

A value that is known not to equal zero.

This enables some memory layout optimization. For example, Option<NonZero<u32>> is the same size as u32:

use core::{num::NonZero};

assert_eq!(size_of::<Option<NonZero<u32>>>(), size_of::<u32>());

§Layout

NonZero<T> is guaranteed to have the same layout and bit validity as T with the exception that the all-zero bit pattern is invalid. Option<NonZero<T>> is guaranteed to be compatible with T, including in FFI.

Thanks to the null pointer optimization, NonZero<T> and Option<NonZero<T>> are guaranteed to have the same size and alignment:

use std::num::NonZero;

assert_eq!(size_of::<NonZero<u32>>(), size_of::<Option<NonZero<u32>>>());
assert_eq!(align_of::<NonZero<u32>>(), align_of::<Option<NonZero<u32>>>());

§Note on generic usage

NonZero<T> can only be used with some standard library primitive types (such as u8, i32, and etc.). The type parameter T must implement the internal trait [ZeroablePrimitive], which is currently permanently unstable and cannot be implemented by users. Therefore, you cannot use NonZero<T> with your own types, nor can you implement traits for all NonZero<T>, only for concrete types.

Implementations§

Source§

impl<T> NonZero<T>
where T: ZeroablePrimitive,

1.28.0 (const: 1.47.0) · Source

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) · Source

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 behavior if the value is zero.

§Safety

The value must not be zero.

1.28.0 (const: 1.34.0) · Source

pub const fn get(self) -> T

Returns the contained value as a primitive type.

Trait Implementations§

1.28.0 · Source§

impl<T> Clone for NonZero<T>
where T: ZeroablePrimitive,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)
where Self:,

Performs copy-assignment from source. Read more
1.51.0 (const: unstable) · Source§

impl Div<NonZero<usize>> for usize

Source§

fn div(self, other: NonZero<usize>) -> usize

Same as self / other.get(), but because other is a NonZero<_>, there’s never a runtime check for division-by-zero.

This operation rounds towards zero, truncating any fractional part of the exact result, and cannot panic.

Source§

type Output = usize

The resulting type after applying the / operator.
1.28.0 · Source§

impl<T> Copy for NonZero<T>
where T: ZeroablePrimitive,

Auto Trait Implementations§

§

impl<T> Freeze for NonZero<T>
where <T as ZeroablePrimitive>::NonZeroInner: Freeze,

§

impl<T> Send for NonZero<T>
where <T as ZeroablePrimitive>::NonZeroInner: Send,

§

impl<T> Sync for NonZero<T>
where <T as ZeroablePrimitive>::NonZeroInner: Sync,

§

impl<T> Unpin for NonZero<T>
where <T as ZeroablePrimitive>::NonZeroInner: Unpin,

Blanket Implementations§

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.