NonZero

Struct NonZero 

Source
pub struct NonZero<T> { /* private fields */ }
Expand description

An integer that is known to not equal zero.

Implementations§

Source§

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

Source

pub fn new(value: T) -> Option<Self>

Returns a new NonZero<T> if value is nonzero

Source

pub const unsafe fn new_unchecked(value: T) -> Self

Returns a new NonZero without checking that the provided value is nonzero.

§Safety

value must be known to be nonzero

Source

pub fn replace(&mut self, new_value: T) -> Option<T>

Tries replacing the nonzero value with a new one. If the new value is nonzero this returns the old value, otherwise this returns None.

Source

pub fn set(&mut self, value: T) -> bool

Sets self.value using the provided value. Only succeeds if the value provided was nonzero. Returns whether the operation succeeded.

Source

pub unsafe fn set_unchecked(&mut self, value: T)

Sets the internal value of the nonzero integer. If the value equals zero, this panics.

§Safety

value must be known to be nonzero

Source

pub fn map(self, f: impl Fn(T) -> T) -> Option<Self>

Applies a function to the inner value and returns a NonZero if the result was nonzero.

Source

pub unsafe fn map_unchecked(self, f: impl Fn(T) -> T) -> Self

Applies a function to the inner value and returns a NonZero if the result was nonzero.

§Safety

f must return a nonzero integer

Source

pub const fn get(&self) -> &T

A reference to the nonzero value

Source

pub const unsafe fn get_mut(&mut self) -> &mut T

👎Deprecated since 0.3.14: use swap instead

A mutable reference to the nonzero value

§Safety

The caller must guarantee that the value is nonzero when the mutable reference is dropped

Source

pub fn swap(&mut self, other: &mut Self)

Swap the nonzero value of two NonZeros

Source

pub fn into_inner(self) -> T

Destructures the NonZero, returning the contained value. You should probably use get unless you are trying to avoid cloning.

Trait Implementations§

Source§

impl<T: Clone> Clone for NonZero<T>

Source§

fn clone(&self) -> NonZero<T>

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

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for NonZero<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Display for NonZero<T>
where T: Display,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<NonZero<u128>> for NonZero<u128>

Source§

fn from(value: NonZeroU128) -> Self

Converts to this type from the input type.
Source§

impl From<NonZero<u16>> for NonZero<u16>

Source§

fn from(value: NonZeroU16) -> Self

Converts to this type from the input type.
Source§

impl From<NonZero<u32>> for NonZero<u32>

Source§

fn from(value: NonZeroU32) -> Self

Converts to this type from the input type.
Source§

impl From<NonZero<u64>> for NonZero<u64>

Source§

fn from(value: NonZeroU64) -> Self

Converts to this type from the input type.
Source§

impl From<NonZero<u8>> for NonZero<u8>

Source§

fn from(value: NonZeroU8) -> Self

Converts to this type from the input type.
Source§

impl From<NonZero<usize>> for NonZero<usize>

Source§

fn from(value: NonZeroUsize) -> Self

Converts to this type from the input type.
Source§

impl<T: Ord> Ord for NonZero<T>

Source§

fn cmp(&self, other: &NonZero<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for NonZero<T>

Source§

fn eq(&self, other: &NonZero<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd for NonZero<T>

Source§

fn partial_cmp(&self, other: &NonZero<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Copy> Copy for NonZero<T>

Source§

impl<T: Eq> Eq for NonZero<T>

Source§

impl<T> StructuralPartialEq for NonZero<T>

Auto Trait Implementations§

§

impl<T> Freeze for NonZero<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for NonZero<T>
where T: RefUnwindSafe,

§

impl<T> Send for NonZero<T>
where T: Send,

§

impl<T> Sync for NonZero<T>
where T: Sync,

§

impl<T> Unpin for NonZero<T>
where T: Unpin,

§

impl<T> UnwindSafe for NonZero<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.