Struct NonZero

struct NonZero<T: ZeroablePrimitive>(_)

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

impl NonZero<i128>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i128>::new(-1i128)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i128>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i128>::new(0b_01100100)?;
let b = NonZero::<i128>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i128>::new(0b_01100100)?;
let b = NonZero::<i128>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i128>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<i128>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<i128>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i128>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<i128>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<i128>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i128>::new(0b100_0000)?;
let b = NonZero::<i128>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x13f40000000000000000000000004f76i128)?;
let m = NonZero::new(0x4f7613f4)?;

assert_eq!(n.rotate_left(16), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x4f7613f4i128)?;
let m = NonZero::new(0x13f40000000000000000000000004f76)?;

assert_eq!(n.rotate_right(16), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12345678901234567890123456789012i128)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x12907856341290785634129078563412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12345678901234567890123456789012i128)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x48091e6a2c48091e6a2c48091e6a2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI128;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai128)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroI128::from_be(n), n)
} else {
    assert_eq!(NonZeroI128::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI128;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai128)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroI128::from_le(n), n)
} else {
    assert_eq!(NonZeroI128::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai128)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai128)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn abs(self: Self) -> Self

Computes the absolute value of self. See i128::abs for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i128)?;
let neg = NonZero::new(-1i128)?;

assert_eq!(pos, pos.abs());
assert_eq!(pos, neg.abs());
# Some(())
# }
const fn checked_abs(self: Self) -> Option<Self>

Checked absolute value. Checks for overflow and returns None if self == NonZero::<i128>::MIN. The result cannot be zero.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i128)?;
let neg = NonZero::new(-1i128)?;
let min = NonZero::new(i128::MIN)?;

assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
# Some(())
# }
const fn overflowing_abs(self: Self) -> (Self, bool)

Computes the absolute value of self, with overflow information, see i128::overflowing_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i128)?;
let neg = NonZero::new(-1i128)?;
let min = NonZero::new(i128::MIN)?;

assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
# Some(())
# }
const fn saturating_abs(self: Self) -> Self

Saturating absolute value, see i128::saturating_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i128)?;
let neg = NonZero::new(-1i128)?;
let min = NonZero::new(i128::MIN)?;
let min_plus = NonZero::new(i128::MIN + 1)?;
let max = NonZero::new(i128::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());
# Some(())
# }
const fn wrapping_abs(self: Self) -> Self

Wrapping absolute value, see i128::wrapping_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i128)?;
let neg = NonZero::new(-1i128)?;
let min = NonZero::new(i128::MIN)?;
# let max = NonZero::new(i128::MAX)?;

assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
# Some(())
# }
const fn unsigned_abs(self: Self) -> NonZero<u128>

Computes the absolute value of self without any wrapping or panicking.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let u_pos = NonZero::new(1u128)?;
let i_pos = NonZero::new(1i128)?;
let i_neg = NonZero::new(-1i128)?;
let i_min = NonZero::new(i128::MIN)?;
let u_max = NonZero::new(u128::MAX / 2 + 1)?;

assert_eq!(u_pos, i_pos.unsigned_abs());
assert_eq!(u_pos, i_neg.unsigned_abs());
assert_eq!(u_max, i_min.unsigned_abs());
# Some(())
# }
const fn is_positive(self: Self) -> bool

Returns true if self is positive and false if the number is negative.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;

assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());
# Some(())
# }
const fn is_negative(self: Self) -> bool

Returns true if self is negative and false if the number is positive.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;

assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());
# Some(())
# }
const fn checked_neg(self: Self) -> Option<Self>

Checked negation. Computes -self, returning None if self == NonZero::<i128>::MIN.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
let min = NonZero::new(i128::MIN)?;

assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);
# Some(())
# }
const fn overflowing_neg(self: Self) -> (Self, bool)

Negates self, overflowing if this is equal to the minimum value.

See i128::overflowing_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
let min = NonZero::new(i128::MIN)?;

assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
# Some(())
# }
const fn saturating_neg(self: Self) -> Self

Saturating negation. Computes -self, returning [NonZero::<i128>::MAX] if self == NonZero::<i128>::MIN instead of overflowing.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
let min = NonZero::new(i128::MIN)?;
let min_plus_one = NonZero::new(i128::MIN + 1)?;
let max = NonZero::new(i128::MAX)?;

assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
# Some(())
# }
const fn wrapping_neg(self: Self) -> Self

Wrapping (modular) negation. Computes -self, wrapping around at the boundary of the type.

See i128::wrapping_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i128)?;
let neg_five = NonZero::new(-5i128)?;
let min = NonZero::new(i128::MIN)?;

assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
# Some(())
# }
const fn cast_unsigned(self: Self) -> NonZero<u128>

Returns the bit pattern of self reinterpreted as an unsigned integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::new(-1i128).unwrap();

assert_eq!(n.cast_unsigned(), NonZero::<u128>::MAX);
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i128)?;
let four = NonZero::new(4i128)?;
let max = NonZero::new(i128::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<i128>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i128)?;
let four = NonZero::new(4i128)?;
let max = NonZero::new(i128::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > i128::MAX, or self * rhs < i128::MIN.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i128)?;
let four = NonZero::new(4i128)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i128)?;
let twenty_seven = NonZero::new(27i128)?;
let half_max = NonZero::new(i128::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<i128>::MIN] or [NonZero::<i128>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i128)?;
let twenty_seven = NonZero::new(27i128)?;
let max = NonZero::new(i128::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i128>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i128>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i128>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i128>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i128>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<i128>::from_str_radix("1 ", 10).is_err());

impl NonZero<i16>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i16>::new(-1i16)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i16>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i16>::new(0b_01100100)?;
let b = NonZero::<i16>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i16>::new(0b_01100100)?;
let b = NonZero::<i16>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i16>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<i16>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<i16>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i16>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<i16>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<i16>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i16>::new(0b100_0000)?;
let b = NonZero::<i16>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(-0x5ffdi16)?;
let m = NonZero::new(0x3a)?;

assert_eq!(n.rotate_left(4), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x3ai16)?;
let m = NonZero::new(-0x5ffd)?;

assert_eq!(n.rotate_right(4), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234i16)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x3412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234i16)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI16;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai16)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroI16::from_be(n), n)
} else {
    assert_eq!(NonZeroI16::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI16;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai16)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroI16::from_le(n), n)
} else {
    assert_eq!(NonZeroI16::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai16)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai16)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn abs(self: Self) -> Self

Computes the absolute value of self. See i16::abs for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;

assert_eq!(pos, pos.abs());
assert_eq!(pos, neg.abs());
# Some(())
# }
const fn checked_abs(self: Self) -> Option<Self>

Checked absolute value. Checks for overflow and returns None if self == NonZero::<i16>::MIN. The result cannot be zero.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;
let min = NonZero::new(i16::MIN)?;

assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
# Some(())
# }
const fn overflowing_abs(self: Self) -> (Self, bool)

Computes the absolute value of self, with overflow information, see i16::overflowing_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;
let min = NonZero::new(i16::MIN)?;

assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
# Some(())
# }
const fn saturating_abs(self: Self) -> Self

Saturating absolute value, see i16::saturating_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;
let min = NonZero::new(i16::MIN)?;
let min_plus = NonZero::new(i16::MIN + 1)?;
let max = NonZero::new(i16::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());
# Some(())
# }
const fn wrapping_abs(self: Self) -> Self

Wrapping absolute value, see i16::wrapping_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i16)?;
let neg = NonZero::new(-1i16)?;
let min = NonZero::new(i16::MIN)?;
# let max = NonZero::new(i16::MAX)?;

assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
# Some(())
# }
const fn unsigned_abs(self: Self) -> NonZero<u16>

Computes the absolute value of self without any wrapping or panicking.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let u_pos = NonZero::new(1u16)?;
let i_pos = NonZero::new(1i16)?;
let i_neg = NonZero::new(-1i16)?;
let i_min = NonZero::new(i16::MIN)?;
let u_max = NonZero::new(u16::MAX / 2 + 1)?;

assert_eq!(u_pos, i_pos.unsigned_abs());
assert_eq!(u_pos, i_neg.unsigned_abs());
assert_eq!(u_max, i_min.unsigned_abs());
# Some(())
# }
const fn is_positive(self: Self) -> bool

Returns true if self is positive and false if the number is negative.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;

assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());
# Some(())
# }
const fn is_negative(self: Self) -> bool

Returns true if self is negative and false if the number is positive.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;

assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());
# Some(())
# }
const fn checked_neg(self: Self) -> Option<Self>

Checked negation. Computes -self, returning None if self == NonZero::<i16>::MIN.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
let min = NonZero::new(i16::MIN)?;

assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);
# Some(())
# }
const fn overflowing_neg(self: Self) -> (Self, bool)

Negates self, overflowing if this is equal to the minimum value.

See i16::overflowing_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
let min = NonZero::new(i16::MIN)?;

assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
# Some(())
# }
const fn saturating_neg(self: Self) -> Self

Saturating negation. Computes -self, returning [NonZero::<i16>::MAX] if self == NonZero::<i16>::MIN instead of overflowing.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
let min = NonZero::new(i16::MIN)?;
let min_plus_one = NonZero::new(i16::MIN + 1)?;
let max = NonZero::new(i16::MAX)?;

assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
# Some(())
# }
const fn wrapping_neg(self: Self) -> Self

Wrapping (modular) negation. Computes -self, wrapping around at the boundary of the type.

See i16::wrapping_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i16)?;
let neg_five = NonZero::new(-5i16)?;
let min = NonZero::new(i16::MIN)?;

assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
# Some(())
# }
const fn cast_unsigned(self: Self) -> NonZero<u16>

Returns the bit pattern of self reinterpreted as an unsigned integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::new(-1i16).unwrap();

assert_eq!(n.cast_unsigned(), NonZero::<u16>::MAX);
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i16)?;
let four = NonZero::new(4i16)?;
let max = NonZero::new(i16::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<i16>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i16)?;
let four = NonZero::new(4i16)?;
let max = NonZero::new(i16::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > i16::MAX, or self * rhs < i16::MIN.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i16)?;
let four = NonZero::new(4i16)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i16)?;
let twenty_seven = NonZero::new(27i16)?;
let half_max = NonZero::new(i16::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<i16>::MIN] or [NonZero::<i16>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i16)?;
let twenty_seven = NonZero::new(27i16)?;
let max = NonZero::new(i16::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i16>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i16>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i16>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i16>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i16>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<i16>::from_str_radix("1 ", 10).is_err());

impl NonZero<i32>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i32>::new(-1i32)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i32>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i32>::new(0b_01100100)?;
let b = NonZero::<i32>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i32>::new(0b_01100100)?;
let b = NonZero::<i32>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i32>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<i32>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<i32>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i32>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<i32>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<i32>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i32>::new(0b100_0000)?;
let b = NonZero::<i32>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x10000b3i32)?;
let m = NonZero::new(0xb301)?;

assert_eq!(n.rotate_left(8), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0xb301i32)?;
let m = NonZero::new(0x10000b3)?;

assert_eq!(n.rotate_right(8), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12345678i32)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x78563412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12345678i32)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x1e6a2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI32;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai32)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroI32::from_be(n), n)
} else {
    assert_eq!(NonZeroI32::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI32;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai32)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroI32::from_le(n), n)
} else {
    assert_eq!(NonZeroI32::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai32)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai32)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn abs(self: Self) -> Self

Computes the absolute value of self. See i32::abs for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i32)?;
let neg = NonZero::new(-1i32)?;

assert_eq!(pos, pos.abs());
assert_eq!(pos, neg.abs());
# Some(())
# }
const fn checked_abs(self: Self) -> Option<Self>

Checked absolute value. Checks for overflow and returns None if self == NonZero::<i32>::MIN. The result cannot be zero.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i32)?;
let neg = NonZero::new(-1i32)?;
let min = NonZero::new(i32::MIN)?;

assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
# Some(())
# }
const fn overflowing_abs(self: Self) -> (Self, bool)

Computes the absolute value of self, with overflow information, see i32::overflowing_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i32)?;
let neg = NonZero::new(-1i32)?;
let min = NonZero::new(i32::MIN)?;

assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
# Some(())
# }
const fn saturating_abs(self: Self) -> Self

Saturating absolute value, see i32::saturating_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i32)?;
let neg = NonZero::new(-1i32)?;
let min = NonZero::new(i32::MIN)?;
let min_plus = NonZero::new(i32::MIN + 1)?;
let max = NonZero::new(i32::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());
# Some(())
# }
const fn wrapping_abs(self: Self) -> Self

Wrapping absolute value, see i32::wrapping_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i32)?;
let neg = NonZero::new(-1i32)?;
let min = NonZero::new(i32::MIN)?;
# let max = NonZero::new(i32::MAX)?;

assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
# Some(())
# }
const fn unsigned_abs(self: Self) -> NonZero<u32>

Computes the absolute value of self without any wrapping or panicking.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let u_pos = NonZero::new(1u32)?;
let i_pos = NonZero::new(1i32)?;
let i_neg = NonZero::new(-1i32)?;
let i_min = NonZero::new(i32::MIN)?;
let u_max = NonZero::new(u32::MAX / 2 + 1)?;

assert_eq!(u_pos, i_pos.unsigned_abs());
assert_eq!(u_pos, i_neg.unsigned_abs());
assert_eq!(u_max, i_min.unsigned_abs());
# Some(())
# }
const fn is_positive(self: Self) -> bool

Returns true if self is positive and false if the number is negative.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;

assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());
# Some(())
# }
const fn is_negative(self: Self) -> bool

Returns true if self is negative and false if the number is positive.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;

assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());
# Some(())
# }
const fn checked_neg(self: Self) -> Option<Self>

Checked negation. Computes -self, returning None if self == NonZero::<i32>::MIN.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
let min = NonZero::new(i32::MIN)?;

assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);
# Some(())
# }
const fn overflowing_neg(self: Self) -> (Self, bool)

Negates self, overflowing if this is equal to the minimum value.

See i32::overflowing_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
let min = NonZero::new(i32::MIN)?;

assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
# Some(())
# }
const fn saturating_neg(self: Self) -> Self

Saturating negation. Computes -self, returning [NonZero::<i32>::MAX] if self == NonZero::<i32>::MIN instead of overflowing.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
let min = NonZero::new(i32::MIN)?;
let min_plus_one = NonZero::new(i32::MIN + 1)?;
let max = NonZero::new(i32::MAX)?;

assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
# Some(())
# }
const fn wrapping_neg(self: Self) -> Self

Wrapping (modular) negation. Computes -self, wrapping around at the boundary of the type.

See i32::wrapping_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i32)?;
let neg_five = NonZero::new(-5i32)?;
let min = NonZero::new(i32::MIN)?;

assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
# Some(())
# }
const fn cast_unsigned(self: Self) -> NonZero<u32>

Returns the bit pattern of self reinterpreted as an unsigned integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::new(-1i32).unwrap();

assert_eq!(n.cast_unsigned(), NonZero::<u32>::MAX);
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i32)?;
let four = NonZero::new(4i32)?;
let max = NonZero::new(i32::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<i32>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i32)?;
let four = NonZero::new(4i32)?;
let max = NonZero::new(i32::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > i32::MAX, or self * rhs < i32::MIN.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i32)?;
let four = NonZero::new(4i32)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i32)?;
let twenty_seven = NonZero::new(27i32)?;
let half_max = NonZero::new(i32::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<i32>::MIN] or [NonZero::<i32>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i32)?;
let twenty_seven = NonZero::new(27i32)?;
let max = NonZero::new(i32::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i32>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i32>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i32>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i32>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i32>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<i32>::from_str_radix("1 ", 10).is_err());

impl NonZero<i64>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i64>::new(-1i64)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i64>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i64>::new(0b_01100100)?;
let b = NonZero::<i64>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i64>::new(0b_01100100)?;
let b = NonZero::<i64>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i64>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<i64>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<i64>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i64>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<i64>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<i64>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i64>::new(0b100_0000)?;
let b = NonZero::<i64>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0xaa00000000006e1i64)?;
let m = NonZero::new(0x6e10aa)?;

assert_eq!(n.rotate_left(12), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x6e10aai64)?;
let m = NonZero::new(0xaa00000000006e1)?;

assert_eq!(n.rotate_right(12), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234567890123456i64)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x5634129078563412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234567890123456i64)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x6a2c48091e6a2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI64;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai64)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroI64::from_be(n), n)
} else {
    assert_eq!(NonZeroI64::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI64;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai64)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroI64::from_le(n), n)
} else {
    assert_eq!(NonZeroI64::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai64)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai64)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn abs(self: Self) -> Self

Computes the absolute value of self. See i64::abs for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i64)?;
let neg = NonZero::new(-1i64)?;

assert_eq!(pos, pos.abs());
assert_eq!(pos, neg.abs());
# Some(())
# }
const fn checked_abs(self: Self) -> Option<Self>

Checked absolute value. Checks for overflow and returns None if self == NonZero::<i64>::MIN. The result cannot be zero.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i64)?;
let neg = NonZero::new(-1i64)?;
let min = NonZero::new(i64::MIN)?;

assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
# Some(())
# }
const fn overflowing_abs(self: Self) -> (Self, bool)

Computes the absolute value of self, with overflow information, see i64::overflowing_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i64)?;
let neg = NonZero::new(-1i64)?;
let min = NonZero::new(i64::MIN)?;

assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
# Some(())
# }
const fn saturating_abs(self: Self) -> Self

Saturating absolute value, see i64::saturating_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i64)?;
let neg = NonZero::new(-1i64)?;
let min = NonZero::new(i64::MIN)?;
let min_plus = NonZero::new(i64::MIN + 1)?;
let max = NonZero::new(i64::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());
# Some(())
# }
const fn wrapping_abs(self: Self) -> Self

Wrapping absolute value, see i64::wrapping_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i64)?;
let neg = NonZero::new(-1i64)?;
let min = NonZero::new(i64::MIN)?;
# let max = NonZero::new(i64::MAX)?;

assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
# Some(())
# }
const fn unsigned_abs(self: Self) -> NonZero<u64>

Computes the absolute value of self without any wrapping or panicking.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let u_pos = NonZero::new(1u64)?;
let i_pos = NonZero::new(1i64)?;
let i_neg = NonZero::new(-1i64)?;
let i_min = NonZero::new(i64::MIN)?;
let u_max = NonZero::new(u64::MAX / 2 + 1)?;

assert_eq!(u_pos, i_pos.unsigned_abs());
assert_eq!(u_pos, i_neg.unsigned_abs());
assert_eq!(u_max, i_min.unsigned_abs());
# Some(())
# }
const fn is_positive(self: Self) -> bool

Returns true if self is positive and false if the number is negative.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;

assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());
# Some(())
# }
const fn is_negative(self: Self) -> bool

Returns true if self is negative and false if the number is positive.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;

assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());
# Some(())
# }
const fn checked_neg(self: Self) -> Option<Self>

Checked negation. Computes -self, returning None if self == NonZero::<i64>::MIN.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
let min = NonZero::new(i64::MIN)?;

assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);
# Some(())
# }
const fn overflowing_neg(self: Self) -> (Self, bool)

Negates self, overflowing if this is equal to the minimum value.

See i64::overflowing_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
let min = NonZero::new(i64::MIN)?;

assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
# Some(())
# }
const fn saturating_neg(self: Self) -> Self

Saturating negation. Computes -self, returning [NonZero::<i64>::MAX] if self == NonZero::<i64>::MIN instead of overflowing.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
let min = NonZero::new(i64::MIN)?;
let min_plus_one = NonZero::new(i64::MIN + 1)?;
let max = NonZero::new(i64::MAX)?;

assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
# Some(())
# }
const fn wrapping_neg(self: Self) -> Self

Wrapping (modular) negation. Computes -self, wrapping around at the boundary of the type.

See i64::wrapping_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i64)?;
let neg_five = NonZero::new(-5i64)?;
let min = NonZero::new(i64::MIN)?;

assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
# Some(())
# }
const fn cast_unsigned(self: Self) -> NonZero<u64>

Returns the bit pattern of self reinterpreted as an unsigned integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::new(-1i64).unwrap();

assert_eq!(n.cast_unsigned(), NonZero::<u64>::MAX);
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i64)?;
let four = NonZero::new(4i64)?;
let max = NonZero::new(i64::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<i64>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i64)?;
let four = NonZero::new(4i64)?;
let max = NonZero::new(i64::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > i64::MAX, or self * rhs < i64::MIN.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i64)?;
let four = NonZero::new(4i64)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i64)?;
let twenty_seven = NonZero::new(27i64)?;
let half_max = NonZero::new(i64::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<i64>::MIN] or [NonZero::<i64>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i64)?;
let twenty_seven = NonZero::new(27i64)?;
let max = NonZero::new(i64::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i64>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i64>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i64>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i64>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i64>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<i64>::from_str_radix("1 ", 10).is_err());

impl NonZero<i8>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i8>::new(-1i8)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<i8>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i8>::new(0b_01100100)?;
let b = NonZero::<i8>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i8>::new(0b_01100100)?;
let b = NonZero::<i8>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i8>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<i8>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<i8>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i8>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<i8>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<i8>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<i8>::new(0b100_0000)?;
let b = NonZero::<i8>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(-0x7ei8)?;
let m = NonZero::new(0xa)?;

assert_eq!(n.rotate_left(2), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0xai8)?;
let m = NonZero::new(-0x7e)?;

assert_eq!(n.rotate_right(2), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12i8)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x12)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12i8)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI8;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai8)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroI8::from_be(n), n)
} else {
    assert_eq!(NonZeroI8::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroI8;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai8)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroI8::from_le(n), n)
} else {
    assert_eq!(NonZeroI8::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai8)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ai8)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn abs(self: Self) -> Self

Computes the absolute value of self. See i8::abs for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i8)?;
let neg = NonZero::new(-1i8)?;

assert_eq!(pos, pos.abs());
assert_eq!(pos, neg.abs());
# Some(())
# }
const fn checked_abs(self: Self) -> Option<Self>

Checked absolute value. Checks for overflow and returns None if self == NonZero::<i8>::MIN. The result cannot be zero.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i8)?;
let neg = NonZero::new(-1i8)?;
let min = NonZero::new(i8::MIN)?;

assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
# Some(())
# }
const fn overflowing_abs(self: Self) -> (Self, bool)

Computes the absolute value of self, with overflow information, see i8::overflowing_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i8)?;
let neg = NonZero::new(-1i8)?;
let min = NonZero::new(i8::MIN)?;

assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
# Some(())
# }
const fn saturating_abs(self: Self) -> Self

Saturating absolute value, see i8::saturating_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
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());
# Some(())
# }
const fn wrapping_abs(self: Self) -> Self

Wrapping absolute value, see i8::wrapping_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1i8)?;
let neg = NonZero::new(-1i8)?;
let min = NonZero::new(i8::MIN)?;
# let max = NonZero::new(i8::MAX)?;

assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
# Some(())
# }
const fn unsigned_abs(self: Self) -> NonZero<u8>

Computes the absolute value of self without any wrapping or panicking.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let u_pos = NonZero::new(1u8)?;
let i_pos = NonZero::new(1i8)?;
let i_neg = NonZero::new(-1i8)?;
let i_min = NonZero::new(i8::MIN)?;
let u_max = NonZero::new(u8::MAX / 2 + 1)?;

assert_eq!(u_pos, i_pos.unsigned_abs());
assert_eq!(u_pos, i_neg.unsigned_abs());
assert_eq!(u_max, i_min.unsigned_abs());
# Some(())
# }
const fn is_positive(self: Self) -> bool

Returns true if self is positive and false if the number is negative.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;

assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());
# Some(())
# }
const fn is_negative(self: Self) -> bool

Returns true if self is negative and false if the number is positive.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;

assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());
# Some(())
# }
const fn checked_neg(self: Self) -> Option<Self>

Checked negation. Computes -self, returning None if self == NonZero::<i8>::MIN.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
let min = NonZero::new(i8::MIN)?;

assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);
# Some(())
# }
const fn overflowing_neg(self: Self) -> (Self, bool)

Negates self, overflowing if this is equal to the minimum value.

See i8::overflowing_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
let min = NonZero::new(i8::MIN)?;

assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
# Some(())
# }
const fn saturating_neg(self: Self) -> Self

Saturating negation. Computes -self, returning [NonZero::<i8>::MAX] if self == NonZero::<i8>::MIN instead of overflowing.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
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);
# Some(())
# }
const fn wrapping_neg(self: Self) -> Self

Wrapping (modular) negation. Computes -self, wrapping around at the boundary of the type.

See i8::wrapping_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5i8)?;
let neg_five = NonZero::new(-5i8)?;
let min = NonZero::new(i8::MIN)?;

assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
# Some(())
# }
const fn cast_unsigned(self: Self) -> NonZero<u8>

Returns the bit pattern of self reinterpreted as an unsigned integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::new(-1i8).unwrap();

assert_eq!(n.cast_unsigned(), NonZero::<u8>::MAX);
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i8)?;
let four = NonZero::new(4i8)?;
let max = NonZero::new(i8::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<i8>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i8)?;
let four = NonZero::new(4i8)?;
let max = NonZero::new(i8::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2i8)?;
let four = NonZero::new(4i8)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i8)?;
let twenty_seven = NonZero::new(27i8)?;
let half_max = NonZero::new(i8::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<i8>::MIN] or [NonZero::<i8>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3i8)?;
let twenty_seven = NonZero::new(27i8)?;
let max = NonZero::new(i8::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i8>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i8>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i8>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<i8>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<i8>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<i8>::from_str_radix("1 ", 10).is_err());

impl NonZero<isize>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<isize>::new(-1isize)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<isize>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<isize>::new(0b_01100100)?;
let b = NonZero::<isize>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<isize>::new(0b_01100100)?;
let b = NonZero::<isize>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<isize>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<isize>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<isize>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<isize>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<isize>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<isize>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<isize>::new(0b100_0000)?;
let b = NonZero::<isize>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0xaa00000000006e1isize)?;
let m = NonZero::new(0x6e10aa)?;

assert_eq!(n.rotate_left(12), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x6e10aaisize)?;
let m = NonZero::new(0xaa00000000006e1)?;

assert_eq!(n.rotate_right(12), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234567890123456isize)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x5634129078563412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234567890123456isize)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x6a2c48091e6a2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroIsize;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Aisize)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroIsize::from_be(n), n)
} else {
    assert_eq!(NonZeroIsize::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroIsize;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Aisize)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroIsize::from_le(n), n)
} else {
    assert_eq!(NonZeroIsize::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Aisize)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Aisize)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn abs(self: Self) -> Self

Computes the absolute value of self. See isize::abs for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;

assert_eq!(pos, pos.abs());
assert_eq!(pos, neg.abs());
# Some(())
# }
const fn checked_abs(self: Self) -> Option<Self>

Checked absolute value. Checks for overflow and returns None if self == NonZero::<isize>::MIN. The result cannot be zero.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;
let min = NonZero::new(isize::MIN)?;

assert_eq!(Some(pos), neg.checked_abs());
assert_eq!(None, min.checked_abs());
# Some(())
# }
const fn overflowing_abs(self: Self) -> (Self, bool)

Computes the absolute value of self, with overflow information, see isize::overflowing_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;
let min = NonZero::new(isize::MIN)?;

assert_eq!((pos, false), pos.overflowing_abs());
assert_eq!((pos, false), neg.overflowing_abs());
assert_eq!((min, true), min.overflowing_abs());
# Some(())
# }
const fn saturating_abs(self: Self) -> Self

Saturating absolute value, see isize::saturating_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;
let min = NonZero::new(isize::MIN)?;
let min_plus = NonZero::new(isize::MIN + 1)?;
let max = NonZero::new(isize::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());
# Some(())
# }
const fn wrapping_abs(self: Self) -> Self

Wrapping absolute value, see isize::wrapping_abs.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos = NonZero::new(1isize)?;
let neg = NonZero::new(-1isize)?;
let min = NonZero::new(isize::MIN)?;
# let max = NonZero::new(isize::MAX)?;

assert_eq!(pos, pos.wrapping_abs());
assert_eq!(pos, neg.wrapping_abs());
assert_eq!(min, min.wrapping_abs());
assert_eq!(max, (-max).wrapping_abs());
# Some(())
# }
const fn unsigned_abs(self: Self) -> NonZero<usize>

Computes the absolute value of self without any wrapping or panicking.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let u_pos = NonZero::new(1usize)?;
let i_pos = NonZero::new(1isize)?;
let i_neg = NonZero::new(-1isize)?;
let i_min = NonZero::new(isize::MIN)?;
let u_max = NonZero::new(usize::MAX / 2 + 1)?;

assert_eq!(u_pos, i_pos.unsigned_abs());
assert_eq!(u_pos, i_neg.unsigned_abs());
assert_eq!(u_max, i_min.unsigned_abs());
# Some(())
# }
const fn is_positive(self: Self) -> bool

Returns true if self is positive and false if the number is negative.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;

assert!(pos_five.is_positive());
assert!(!neg_five.is_positive());
# Some(())
# }
const fn is_negative(self: Self) -> bool

Returns true if self is negative and false if the number is positive.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;

assert!(neg_five.is_negative());
assert!(!pos_five.is_negative());
# Some(())
# }
const fn checked_neg(self: Self) -> Option<Self>

Checked negation. Computes -self, returning None if self == NonZero::<isize>::MIN.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
let min = NonZero::new(isize::MIN)?;

assert_eq!(pos_five.checked_neg(), Some(neg_five));
assert_eq!(min.checked_neg(), None);
# Some(())
# }
const fn overflowing_neg(self: Self) -> (Self, bool)

Negates self, overflowing if this is equal to the minimum value.

See isize::overflowing_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
let min = NonZero::new(isize::MIN)?;

assert_eq!(pos_five.overflowing_neg(), (neg_five, false));
assert_eq!(min.overflowing_neg(), (min, true));
# Some(())
# }
const fn saturating_neg(self: Self) -> Self

Saturating negation. Computes -self, returning [NonZero::<isize>::MAX] if self == NonZero::<isize>::MIN instead of overflowing.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
let min = NonZero::new(isize::MIN)?;
let min_plus_one = NonZero::new(isize::MIN + 1)?;
let max = NonZero::new(isize::MAX)?;

assert_eq!(pos_five.saturating_neg(), neg_five);
assert_eq!(min.saturating_neg(), max);
assert_eq!(max.saturating_neg(), min_plus_one);
# Some(())
# }
const fn wrapping_neg(self: Self) -> Self

Wrapping (modular) negation. Computes -self, wrapping around at the boundary of the type.

See isize::wrapping_neg for documentation on overflow behavior.

Example

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let pos_five = NonZero::new(5isize)?;
let neg_five = NonZero::new(-5isize)?;
let min = NonZero::new(isize::MIN)?;

assert_eq!(pos_five.wrapping_neg(), neg_five);
assert_eq!(min.wrapping_neg(), min);
# Some(())
# }
const fn cast_unsigned(self: Self) -> NonZero<usize>

Returns the bit pattern of self reinterpreted as an unsigned integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::new(-1isize).unwrap();

assert_eq!(n.cast_unsigned(), NonZero::<usize>::MAX);
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2isize)?;
let four = NonZero::new(4isize)?;
let max = NonZero::new(isize::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<isize>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2isize)?;
let four = NonZero::new(4isize)?;
let max = NonZero::new(isize::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > isize::MAX, or self * rhs < isize::MIN.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2isize)?;
let four = NonZero::new(4isize)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3isize)?;
let twenty_seven = NonZero::new(27isize)?;
let half_max = NonZero::new(isize::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<isize>::MIN] or [NonZero::<isize>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3isize)?;
let twenty_seven = NonZero::new(27isize)?;
let max = NonZero::new(isize::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<isize>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<isize>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<isize>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<isize>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + or - sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<isize>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<isize>::from_str_radix("1 ", 10).is_err());

impl NonZero<u128>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u128>::new(u128::MAX)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u128>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u128>::new(0b_01100100)?;
let b = NonZero::<u128>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u128>::new(0b_01100100)?;
let b = NonZero::<u128>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u128>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<u128>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<u128>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u128>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<u128>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<u128>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u128>::new(0b100_0000)?;
let b = NonZero::<u128>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x13f40000000000000000000000004f76u128)?;
let m = NonZero::new(0x4f7613f4)?;

assert_eq!(n.rotate_left(16), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x4f7613f4u128)?;
let m = NonZero::new(0x13f40000000000000000000000004f76)?;

assert_eq!(n.rotate_right(16), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12345678901234567890123456789012u128)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x12907856341290785634129078563412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12345678901234567890123456789012u128)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x48091e6a2c48091e6a2c48091e6a2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU128;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au128)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroU128::from_be(n), n)
} else {
    assert_eq!(NonZeroU128::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU128;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au128)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroU128::from_le(n), n)
} else {
    assert_eq!(NonZeroU128::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au128)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au128)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn checked_add(self: Self, other: u128) -> Option<Self>

Adds an unsigned integer to a non-zero value. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u128)?;
let two = NonZero::new(2u128)?;
let max = NonZero::new(u128::MAX)?;

assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
# Some(())
# }
const fn saturating_add(self: Self, other: u128) -> Self

Adds an unsigned integer to a non-zero value. Return [NonZero::<u128>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u128)?;
let two = NonZero::new(2u128)?;
let max = NonZero::new(u128::MAX)?;

assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
# Some(())
# }
unsafe const fn unchecked_add(self: Self, other: u128) -> Self

Adds an unsigned integer to a non-zero value, 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 > u128::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u128)?;
let two = NonZero::new(2u128)?;

assert_eq!(two, unsafe { one.unchecked_add(1) });
# Some(())
# }
const fn checked_next_power_of_two(self: Self) -> Option<Self>

Returns the smallest power of two greater than or equal to self. Checks for overflow and returns None if the next power of two is greater than the type’s maximum value. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u128)?;
let three = NonZero::new(3u128)?;
let four = NonZero::new(4u128)?;
let max = NonZero::new(u128::MAX)?;

assert_eq!(Some(two), two.checked_next_power_of_two() );
assert_eq!(Some(four), three.checked_next_power_of_two() );
assert_eq!(None, max.checked_next_power_of_two() );
# Some(())
# }
const fn ilog2(self: Self) -> u32

Returns the base 2 logarithm of the number, rounded down.

This is the same operation as u128::ilog2, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(7u128)?.ilog2(), 2);
assert_eq!(NonZero::new(8u128)?.ilog2(), 3);
assert_eq!(NonZero::new(9u128)?.ilog2(), 3);
# Some(())
# }
const fn ilog10(self: Self) -> u32

Returns the base 10 logarithm of the number, rounded down.

This is the same operation as u128::ilog10, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(99u128)?.ilog10(), 1);
assert_eq!(NonZero::new(100u128)?.ilog10(), 2);
assert_eq!(NonZero::new(101u128)?.ilog10(), 2);
# Some(())
# }
const fn midpoint(self: Self, rhs: Self) -> Self

Calculates the midpoint (average) between 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u128)?;
let two = NonZero::new(2u128)?;
let four = NonZero::new(4u128)?;

assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
# Some(())
# }
const fn is_power_of_two(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let eight = NonZero::new(8u128)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u128)?;
assert!(!ten.is_power_of_two());
# Some(())
# }
const fn isqrt(self: Self) -> Self

Returns the square root of the number, rounded down.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let ten = NonZero::new(10u128)?;
let three = NonZero::new(3u128)?;

assert_eq!(ten.isqrt(), three);
# Some(())
# }
const fn cast_signed(self: Self) -> NonZero<i128>

Returns the bit pattern of self reinterpreted as a signed integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::<u128>::MAX;

assert_eq!(n.cast_signed(), NonZero::new(-1i128).unwrap());
const fn bit_width(self: Self) -> NonZero<u32>

Returns the minimum number of bits required to represent self.

Examples

#![feature(uint_bit_width)]

# use core::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u128>::MIN.bit_width(), NonZero::new(1)?);
assert_eq!(NonZero::<u128>::new(0b111)?.bit_width(), NonZero::new(3)?);
assert_eq!(NonZero::<u128>::new(0b1110)?.bit_width(), NonZero::new(4)?);
# Some(())
# }
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u128)?;
let four = NonZero::new(4u128)?;
let max = NonZero::new(u128::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<u128>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u128)?;
let four = NonZero::new(4u128)?;
let max = NonZero::new(u128::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > u128::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u128)?;
let four = NonZero::new(4u128)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u128)?;
let twenty_seven = NonZero::new(27u128)?;
let half_max = NonZero::new(u128::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<u128>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u128)?;
let twenty_seven = NonZero::new(27u128)?;
let max = NonZero::new(u128::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u128>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u128>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u128>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u128>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u128>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<u128>::from_str_radix("1 ", 10).is_err());

impl NonZero<u128>

const fn div_ceil(self: Self, rhs: Self) -> Self

Calculates the quotient of self and rhs, rounding the result towards positive infinity.

The result is guaranteed to be non-zero.

Examples

# use std::num::NonZero;
let one = NonZero::new(1u128).unwrap();
let max = NonZero::new(u128::MAX).unwrap();
assert_eq!(one.div_ceil(max), one);

let two = NonZero::new(2u128).unwrap();
let three = NonZero::new(3u128).unwrap();
assert_eq!(three.div_ceil(two), two);

impl NonZero<u16>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u16>::new(u16::MAX)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u16>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u16>::new(0b_01100100)?;
let b = NonZero::<u16>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u16>::new(0b_01100100)?;
let b = NonZero::<u16>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u16>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<u16>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<u16>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u16>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<u16>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<u16>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u16>::new(0b100_0000)?;
let b = NonZero::<u16>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0xa003u16)?;
let m = NonZero::new(0x3a)?;

assert_eq!(n.rotate_left(4), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x3au16)?;
let m = NonZero::new(0xa003)?;

assert_eq!(n.rotate_right(4), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234u16)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x3412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234u16)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU16;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au16)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroU16::from_be(n), n)
} else {
    assert_eq!(NonZeroU16::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU16;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au16)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroU16::from_le(n), n)
} else {
    assert_eq!(NonZeroU16::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au16)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au16)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn checked_add(self: Self, other: u16) -> Option<Self>

Adds an unsigned integer to a non-zero value. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u16)?;
let two = NonZero::new(2u16)?;
let max = NonZero::new(u16::MAX)?;

assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
# Some(())
# }
const fn saturating_add(self: Self, other: u16) -> Self

Adds an unsigned integer to a non-zero value. Return [NonZero::<u16>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u16)?;
let two = NonZero::new(2u16)?;
let max = NonZero::new(u16::MAX)?;

assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
# Some(())
# }
unsafe const fn unchecked_add(self: Self, other: u16) -> Self

Adds an unsigned integer to a non-zero value, 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 > u16::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u16)?;
let two = NonZero::new(2u16)?;

assert_eq!(two, unsafe { one.unchecked_add(1) });
# Some(())
# }
const fn checked_next_power_of_two(self: Self) -> Option<Self>

Returns the smallest power of two greater than or equal to self. Checks for overflow and returns None if the next power of two is greater than the type’s maximum value. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u16)?;
let three = NonZero::new(3u16)?;
let four = NonZero::new(4u16)?;
let max = NonZero::new(u16::MAX)?;

assert_eq!(Some(two), two.checked_next_power_of_two() );
assert_eq!(Some(four), three.checked_next_power_of_two() );
assert_eq!(None, max.checked_next_power_of_two() );
# Some(())
# }
const fn ilog2(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(7u16)?.ilog2(), 2);
assert_eq!(NonZero::new(8u16)?.ilog2(), 3);
assert_eq!(NonZero::new(9u16)?.ilog2(), 3);
# Some(())
# }
const fn ilog10(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(99u16)?.ilog10(), 1);
assert_eq!(NonZero::new(100u16)?.ilog10(), 2);
assert_eq!(NonZero::new(101u16)?.ilog10(), 2);
# Some(())
# }
const fn midpoint(self: Self, rhs: Self) -> Self

Calculates the midpoint (average) between 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u16)?;
let two = NonZero::new(2u16)?;
let four = NonZero::new(4u16)?;

assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
# Some(())
# }
const fn is_power_of_two(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let eight = NonZero::new(8u16)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u16)?;
assert!(!ten.is_power_of_two());
# Some(())
# }
const fn isqrt(self: Self) -> Self

Returns the square root of the number, rounded down.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let ten = NonZero::new(10u16)?;
let three = NonZero::new(3u16)?;

assert_eq!(ten.isqrt(), three);
# Some(())
# }
const fn cast_signed(self: Self) -> NonZero<i16>

Returns the bit pattern of self reinterpreted as a signed integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::<u16>::MAX;

assert_eq!(n.cast_signed(), NonZero::new(-1i16).unwrap());
const fn bit_width(self: Self) -> NonZero<u32>

Returns the minimum number of bits required to represent self.

Examples

#![feature(uint_bit_width)]

# use core::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u16>::MIN.bit_width(), NonZero::new(1)?);
assert_eq!(NonZero::<u16>::new(0b111)?.bit_width(), NonZero::new(3)?);
assert_eq!(NonZero::<u16>::new(0b1110)?.bit_width(), NonZero::new(4)?);
# Some(())
# }
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u16)?;
let four = NonZero::new(4u16)?;
let max = NonZero::new(u16::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<u16>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u16)?;
let four = NonZero::new(4u16)?;
let max = NonZero::new(u16::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > u16::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u16)?;
let four = NonZero::new(4u16)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u16)?;
let twenty_seven = NonZero::new(27u16)?;
let half_max = NonZero::new(u16::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<u16>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u16)?;
let twenty_seven = NonZero::new(27u16)?;
let max = NonZero::new(u16::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u16>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u16>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u16>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u16>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u16>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<u16>::from_str_radix("1 ", 10).is_err());

impl NonZero<u16>

const fn div_ceil(self: Self, rhs: Self) -> Self

Calculates the quotient of self and rhs, rounding the result towards positive infinity.

The result is guaranteed to be non-zero.

Examples

# use std::num::NonZero;
let one = NonZero::new(1u16).unwrap();
let max = NonZero::new(u16::MAX).unwrap();
assert_eq!(one.div_ceil(max), one);

let two = NonZero::new(2u16).unwrap();
let three = NonZero::new(3u16).unwrap();
assert_eq!(three.div_ceil(two), two);

impl NonZero<u32>

const fn div_ceil(self: Self, rhs: Self) -> Self

Calculates the quotient of self and rhs, rounding the result towards positive infinity.

The result is guaranteed to be non-zero.

Examples

# use std::num::NonZero;
let one = NonZero::new(1u32).unwrap();
let max = NonZero::new(u32::MAX).unwrap();
assert_eq!(one.div_ceil(max), one);

let two = NonZero::new(2u32).unwrap();
let three = NonZero::new(3u32).unwrap();
assert_eq!(three.div_ceil(two), two);

impl NonZero<u32>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u32>::new(u32::MAX)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u32>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u32>::new(0b_01100100)?;
let b = NonZero::<u32>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u32>::new(0b_01100100)?;
let b = NonZero::<u32>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u32>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<u32>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<u32>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u32>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<u32>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<u32>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u32>::new(0b100_0000)?;
let b = NonZero::<u32>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x10000b3u32)?;
let m = NonZero::new(0xb301)?;

assert_eq!(n.rotate_left(8), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0xb301u32)?;
let m = NonZero::new(0x10000b3)?;

assert_eq!(n.rotate_right(8), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12345678u32)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x78563412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12345678u32)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x1e6a2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU32;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au32)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroU32::from_be(n), n)
} else {
    assert_eq!(NonZeroU32::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU32;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au32)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroU32::from_le(n), n)
} else {
    assert_eq!(NonZeroU32::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au32)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au32)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn checked_add(self: Self, other: u32) -> Option<Self>

Adds an unsigned integer to a non-zero value. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u32)?;
let two = NonZero::new(2u32)?;
let max = NonZero::new(u32::MAX)?;

assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
# Some(())
# }
const fn saturating_add(self: Self, other: u32) -> Self

Adds an unsigned integer to a non-zero value. Return [NonZero::<u32>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u32)?;
let two = NonZero::new(2u32)?;
let max = NonZero::new(u32::MAX)?;

assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
# Some(())
# }
unsafe const fn unchecked_add(self: Self, other: u32) -> Self

Adds an unsigned integer to a non-zero value, 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 > u32::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u32)?;
let two = NonZero::new(2u32)?;

assert_eq!(two, unsafe { one.unchecked_add(1) });
# Some(())
# }
const fn checked_next_power_of_two(self: Self) -> Option<Self>

Returns the smallest power of two greater than or equal to self. Checks for overflow and returns None if the next power of two is greater than the type’s maximum value. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u32)?;
let three = NonZero::new(3u32)?;
let four = NonZero::new(4u32)?;
let max = NonZero::new(u32::MAX)?;

assert_eq!(Some(two), two.checked_next_power_of_two() );
assert_eq!(Some(four), three.checked_next_power_of_two() );
assert_eq!(None, max.checked_next_power_of_two() );
# Some(())
# }
const fn ilog2(self: Self) -> u32

Returns the base 2 logarithm of the number, rounded down.

This is the same operation as u32::ilog2, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(7u32)?.ilog2(), 2);
assert_eq!(NonZero::new(8u32)?.ilog2(), 3);
assert_eq!(NonZero::new(9u32)?.ilog2(), 3);
# Some(())
# }
const fn ilog10(self: Self) -> u32

Returns the base 10 logarithm of the number, rounded down.

This is the same operation as u32::ilog10, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(99u32)?.ilog10(), 1);
assert_eq!(NonZero::new(100u32)?.ilog10(), 2);
assert_eq!(NonZero::new(101u32)?.ilog10(), 2);
# Some(())
# }
const fn midpoint(self: Self, rhs: Self) -> Self

Calculates the midpoint (average) between 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u32)?;
let two = NonZero::new(2u32)?;
let four = NonZero::new(4u32)?;

assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
# Some(())
# }
const fn is_power_of_two(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let eight = NonZero::new(8u32)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u32)?;
assert!(!ten.is_power_of_two());
# Some(())
# }
const fn isqrt(self: Self) -> Self

Returns the square root of the number, rounded down.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let ten = NonZero::new(10u32)?;
let three = NonZero::new(3u32)?;

assert_eq!(ten.isqrt(), three);
# Some(())
# }
const fn cast_signed(self: Self) -> NonZero<i32>

Returns the bit pattern of self reinterpreted as a signed integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::<u32>::MAX;

assert_eq!(n.cast_signed(), NonZero::new(-1i32).unwrap());
const fn bit_width(self: Self) -> NonZero<u32>

Returns the minimum number of bits required to represent self.

Examples

#![feature(uint_bit_width)]

# use core::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u32>::MIN.bit_width(), NonZero::new(1)?);
assert_eq!(NonZero::<u32>::new(0b111)?.bit_width(), NonZero::new(3)?);
assert_eq!(NonZero::<u32>::new(0b1110)?.bit_width(), NonZero::new(4)?);
# Some(())
# }
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u32)?;
let four = NonZero::new(4u32)?;
let max = NonZero::new(u32::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<u32>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u32)?;
let four = NonZero::new(4u32)?;
let max = NonZero::new(u32::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > u32::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u32)?;
let four = NonZero::new(4u32)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u32)?;
let twenty_seven = NonZero::new(27u32)?;
let half_max = NonZero::new(u32::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<u32>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u32)?;
let twenty_seven = NonZero::new(27u32)?;
let max = NonZero::new(u32::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u32>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u32>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u32>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u32>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u32>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<u32>::from_str_radix("1 ", 10).is_err());

impl NonZero<u64>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u64>::new(u64::MAX)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u64>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u64>::new(0b_01100100)?;
let b = NonZero::<u64>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u64>::new(0b_01100100)?;
let b = NonZero::<u64>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u64>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<u64>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<u64>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u64>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<u64>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<u64>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u64>::new(0b100_0000)?;
let b = NonZero::<u64>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0xaa00000000006e1u64)?;
let m = NonZero::new(0x6e10aa)?;

assert_eq!(n.rotate_left(12), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x6e10aau64)?;
let m = NonZero::new(0xaa00000000006e1)?;

assert_eq!(n.rotate_right(12), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234567890123456u64)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x5634129078563412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234567890123456u64)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x6a2c48091e6a2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU64;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au64)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroU64::from_be(n), n)
} else {
    assert_eq!(NonZeroU64::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU64;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au64)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroU64::from_le(n), n)
} else {
    assert_eq!(NonZeroU64::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au64)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au64)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn checked_add(self: Self, other: u64) -> Option<Self>

Adds an unsigned integer to a non-zero value. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u64)?;
let two = NonZero::new(2u64)?;
let max = NonZero::new(u64::MAX)?;

assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
# Some(())
# }
const fn saturating_add(self: Self, other: u64) -> Self

Adds an unsigned integer to a non-zero value. Return [NonZero::<u64>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u64)?;
let two = NonZero::new(2u64)?;
let max = NonZero::new(u64::MAX)?;

assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
# Some(())
# }
unsafe const fn unchecked_add(self: Self, other: u64) -> Self

Adds an unsigned integer to a non-zero value, 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 > u64::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u64)?;
let two = NonZero::new(2u64)?;

assert_eq!(two, unsafe { one.unchecked_add(1) });
# Some(())
# }
const fn checked_next_power_of_two(self: Self) -> Option<Self>

Returns the smallest power of two greater than or equal to self. Checks for overflow and returns None if the next power of two is greater than the type’s maximum value. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u64)?;
let three = NonZero::new(3u64)?;
let four = NonZero::new(4u64)?;
let max = NonZero::new(u64::MAX)?;

assert_eq!(Some(two), two.checked_next_power_of_two() );
assert_eq!(Some(four), three.checked_next_power_of_two() );
assert_eq!(None, max.checked_next_power_of_two() );
# Some(())
# }
const fn ilog2(self: Self) -> u32

Returns the base 2 logarithm of the number, rounded down.

This is the same operation as u64::ilog2, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(7u64)?.ilog2(), 2);
assert_eq!(NonZero::new(8u64)?.ilog2(), 3);
assert_eq!(NonZero::new(9u64)?.ilog2(), 3);
# Some(())
# }
const fn ilog10(self: Self) -> u32

Returns the base 10 logarithm of the number, rounded down.

This is the same operation as u64::ilog10, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(99u64)?.ilog10(), 1);
assert_eq!(NonZero::new(100u64)?.ilog10(), 2);
assert_eq!(NonZero::new(101u64)?.ilog10(), 2);
# Some(())
# }
const fn midpoint(self: Self, rhs: Self) -> Self

Calculates the midpoint (average) between 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u64)?;
let two = NonZero::new(2u64)?;
let four = NonZero::new(4u64)?;

assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
# Some(())
# }
const fn is_power_of_two(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let eight = NonZero::new(8u64)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u64)?;
assert!(!ten.is_power_of_two());
# Some(())
# }
const fn isqrt(self: Self) -> Self

Returns the square root of the number, rounded down.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let ten = NonZero::new(10u64)?;
let three = NonZero::new(3u64)?;

assert_eq!(ten.isqrt(), three);
# Some(())
# }
const fn cast_signed(self: Self) -> NonZero<i64>

Returns the bit pattern of self reinterpreted as a signed integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::<u64>::MAX;

assert_eq!(n.cast_signed(), NonZero::new(-1i64).unwrap());
const fn bit_width(self: Self) -> NonZero<u32>

Returns the minimum number of bits required to represent self.

Examples

#![feature(uint_bit_width)]

# use core::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u64>::MIN.bit_width(), NonZero::new(1)?);
assert_eq!(NonZero::<u64>::new(0b111)?.bit_width(), NonZero::new(3)?);
assert_eq!(NonZero::<u64>::new(0b1110)?.bit_width(), NonZero::new(4)?);
# Some(())
# }
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u64)?;
let four = NonZero::new(4u64)?;
let max = NonZero::new(u64::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<u64>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u64)?;
let four = NonZero::new(4u64)?;
let max = NonZero::new(u64::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > u64::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u64)?;
let four = NonZero::new(4u64)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u64)?;
let twenty_seven = NonZero::new(27u64)?;
let half_max = NonZero::new(u64::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<u64>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u64)?;
let twenty_seven = NonZero::new(27u64)?;
let max = NonZero::new(u64::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u64>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u64>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u64>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u64>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u64>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<u64>::from_str_radix("1 ", 10).is_err());

impl NonZero<u64>

const fn div_ceil(self: Self, rhs: Self) -> Self

Calculates the quotient of self and rhs, rounding the result towards positive infinity.

The result is guaranteed to be non-zero.

Examples

# use std::num::NonZero;
let one = NonZero::new(1u64).unwrap();
let max = NonZero::new(u64::MAX).unwrap();
assert_eq!(one.div_ceil(max), one);

let two = NonZero::new(2u64).unwrap();
let three = NonZero::new(3u64).unwrap();
assert_eq!(three.div_ceil(two), two);

impl NonZero<u8>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u8>::new(u8::MAX)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<u8>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u8>::new(0b_01100100)?;
let b = NonZero::<u8>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u8>::new(0b_01100100)?;
let b = NonZero::<u8>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u8>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<u8>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<u8>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u8>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<u8>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<u8>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<u8>::new(0b100_0000)?;
let b = NonZero::<u8>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x82u8)?;
let m = NonZero::new(0xa)?;

assert_eq!(n.rotate_left(2), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0xau8)?;
let m = NonZero::new(0x82)?;

assert_eq!(n.rotate_right(2), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12u8)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x12)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x12u8)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU8;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au8)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroU8::from_be(n), n)
} else {
    assert_eq!(NonZeroU8::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroU8;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au8)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroU8::from_le(n), n)
} else {
    assert_eq!(NonZeroU8::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au8)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Au8)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn checked_add(self: Self, other: u8) -> Option<Self>

Adds an unsigned integer to a non-zero value. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u8)?;
let two = NonZero::new(2u8)?;
let max = NonZero::new(u8::MAX)?;

assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
# Some(())
# }
const fn saturating_add(self: Self, other: u8) -> Self

Adds an unsigned integer to a non-zero value. Return [NonZero::<u8>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u8)?;
let two = NonZero::new(2u8)?;
let max = NonZero::new(u8::MAX)?;

assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
# Some(())
# }
unsafe const fn unchecked_add(self: Self, other: u8) -> Self

Adds an unsigned integer to a non-zero value, 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 > u8::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u8)?;
let two = NonZero::new(2u8)?;

assert_eq!(two, unsafe { one.unchecked_add(1) });
# Some(())
# }
const fn checked_next_power_of_two(self: Self) -> Option<Self>

Returns the smallest power of two greater than or equal to self. Checks for overflow and returns None if the next power of two is greater than the type’s maximum value. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u8)?;
let three = NonZero::new(3u8)?;
let four = NonZero::new(4u8)?;
let max = NonZero::new(u8::MAX)?;

assert_eq!(Some(two), two.checked_next_power_of_two() );
assert_eq!(Some(four), three.checked_next_power_of_two() );
assert_eq!(None, max.checked_next_power_of_two() );
# Some(())
# }
const fn ilog2(self: Self) -> u32

Returns the base 2 logarithm of the number, rounded down.

This is the same operation as u8::ilog2, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(7u8)?.ilog2(), 2);
assert_eq!(NonZero::new(8u8)?.ilog2(), 3);
assert_eq!(NonZero::new(9u8)?.ilog2(), 3);
# Some(())
# }
const fn ilog10(self: Self) -> u32

Returns the base 10 logarithm of the number, rounded down.

This is the same operation as u8::ilog10, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(99u8)?.ilog10(), 1);
assert_eq!(NonZero::new(100u8)?.ilog10(), 2);
assert_eq!(NonZero::new(101u8)?.ilog10(), 2);
# Some(())
# }
const fn midpoint(self: Self, rhs: Self) -> Self

Calculates the midpoint (average) between 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1u8)?;
let two = NonZero::new(2u8)?;
let four = NonZero::new(4u8)?;

assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
# Some(())
# }
const fn is_power_of_two(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let eight = NonZero::new(8u8)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10u8)?;
assert!(!ten.is_power_of_two());
# Some(())
# }
const fn isqrt(self: Self) -> Self

Returns the square root of the number, rounded down.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let ten = NonZero::new(10u8)?;
let three = NonZero::new(3u8)?;

assert_eq!(ten.isqrt(), three);
# Some(())
# }
const fn cast_signed(self: Self) -> NonZero<i8>

Returns the bit pattern of self reinterpreted as a signed integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::<u8>::MAX;

assert_eq!(n.cast_signed(), NonZero::new(-1i8).unwrap());
const fn bit_width(self: Self) -> NonZero<u32>

Returns the minimum number of bits required to represent self.

Examples

#![feature(uint_bit_width)]

# use core::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u8>::MIN.bit_width(), NonZero::new(1)?);
assert_eq!(NonZero::<u8>::new(0b111)?.bit_width(), NonZero::new(3)?);
assert_eq!(NonZero::<u8>::new(0b1110)?.bit_width(), NonZero::new(4)?);
# Some(())
# }
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u8)?;
let four = NonZero::new(4u8)?;
let max = NonZero::new(u8::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<u8>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u8)?;
let four = NonZero::new(4u8)?;
let max = NonZero::new(u8::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > u8::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2u8)?;
let four = NonZero::new(4u8)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u8)?;
let twenty_seven = NonZero::new(27u8)?;
let half_max = NonZero::new(u8::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<u8>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3u8)?;
let twenty_seven = NonZero::new(27u8)?;
let max = NonZero::new(u8::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u8>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u8>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u8>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<u8>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<u8>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<u8>::from_str_radix("1 ", 10).is_err());

impl NonZero<u8>

const fn div_ceil(self: Self, rhs: Self) -> Self

Calculates the quotient of self and rhs, rounding the result towards positive infinity.

The result is guaranteed to be non-zero.

Examples

# use std::num::NonZero;
let one = NonZero::new(1u8).unwrap();
let max = NonZero::new(u8::MAX).unwrap();
assert_eq!(one.div_ceil(max), one);

let two = NonZero::new(2u8).unwrap();
let three = NonZero::new(3u8).unwrap();
assert_eq!(three.div_ceil(two), two);

impl NonZero<usize>

const fn leading_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<usize>::new(usize::MAX)?;

assert_eq!(n.leading_zeros(), 0);
# Some(())
# }
const fn trailing_zeros(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::<usize>::new(0b0101000)?;

assert_eq!(n.trailing_zeros(), 3);
# Some(())
# }
const fn isolate_highest_one(self: Self) -> Self

Returns self with only the most significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<usize>::new(0b_01100100)?;
let b = NonZero::<usize>::new(0b_01000000)?;

assert_eq!(a.isolate_highest_one(), b);
# Some(())
# }
const fn isolate_lowest_one(self: Self) -> Self

Returns self with only the least significant bit set.

Example

#![feature(isolate_most_least_significant_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<usize>::new(0b_01100100)?;
let b = NonZero::<usize>::new(0b_00000100)?;

assert_eq!(a.isolate_lowest_one(), b);
# Some(())
# }
const fn highest_one(self: Self) -> u32

Returns the index of the highest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<usize>::new(0b1)?.highest_one(), 0);
assert_eq!(NonZero::<usize>::new(0b1_0000)?.highest_one(), 4);
assert_eq!(NonZero::<usize>::new(0b1_1111)?.highest_one(), 4);
# Some(())
# }
const fn lowest_one(self: Self) -> u32

Returns the index of the lowest bit set to one in self.

Examples

#![feature(int_lowest_highest_one)]

# use core::num::NonZero;
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<usize>::new(0b1)?.lowest_one(), 0);
assert_eq!(NonZero::<usize>::new(0b1_0000)?.lowest_one(), 4);
assert_eq!(NonZero::<usize>::new(0b1_1111)?.lowest_one(), 0);
# Some(())
# }
const fn count_ones(self: Self) -> NonZero<u32>

Returns the number of ones in the binary representation of self.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let a = NonZero::<usize>::new(0b100_0000)?;
let b = NonZero::<usize>::new(0b100_0011)?;

assert_eq!(a.count_ones(), NonZero::new(1)?);
assert_eq!(b.count_ones(), NonZero::new(3)?);
# Some(())
# }
const fn rotate_left(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0xaa00000000006e1usize)?;
let m = NonZero::new(0x6e10aa)?;

assert_eq!(n.rotate_left(12), m);
# Some(())
# }
const fn rotate_right(self: Self, n: u32) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x6e10aausize)?;
let m = NonZero::new(0xaa00000000006e1)?;

assert_eq!(n.rotate_right(12), m);
# Some(())
# }
const fn swap_bytes(self: Self) -> Self

Reverses the byte order of the integer.

Examples

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234567890123456usize)?;
let m = n.swap_bytes();

assert_eq!(m, NonZero::new(0x5634129078563412)?);
# Some(())
# }
const fn reverse_bits(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1234567890123456usize)?;
let m = n.reverse_bits();

assert_eq!(m, NonZero::new(0x6a2c48091e6a2c48)?);
# Some(())
# }
const fn from_be(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroUsize;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ausize)?;

if cfg!(target_endian = "big") {
    assert_eq!(NonZeroUsize::from_be(n), n)
} else {
    assert_eq!(NonZeroUsize::from_be(n), n.swap_bytes())
}
# Some(())
# }
const fn from_le(x: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
use std::num::NonZeroUsize;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ausize)?;

if cfg!(target_endian = "little") {
    assert_eq!(NonZeroUsize::from_le(n), n)
} else {
    assert_eq!(NonZeroUsize::from_le(n), n.swap_bytes())
}
# Some(())
# }
const fn to_be(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ausize)?;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
# Some(())
# }
const fn to_le(self: Self) -> Self

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

#![feature(nonzero_bitwise)]
# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let n = NonZero::new(0x1Ausize)?;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
# Some(())
# }
const fn checked_add(self: Self, other: usize) -> Option<Self>

Adds an unsigned integer to a non-zero value. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1usize)?;
let two = NonZero::new(2usize)?;
let max = NonZero::new(usize::MAX)?;

assert_eq!(Some(two), one.checked_add(1));
assert_eq!(None, max.checked_add(1));
# Some(())
# }
const fn saturating_add(self: Self, other: usize) -> Self

Adds an unsigned integer to a non-zero value. Return [NonZero::<usize>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1usize)?;
let two = NonZero::new(2usize)?;
let max = NonZero::new(usize::MAX)?;

assert_eq!(two, one.saturating_add(1));
assert_eq!(max, max.saturating_add(1));
# Some(())
# }
unsafe const fn unchecked_add(self: Self, other: usize) -> Self

Adds an unsigned integer to a non-zero value, 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 > usize::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1usize)?;
let two = NonZero::new(2usize)?;

assert_eq!(two, unsafe { one.unchecked_add(1) });
# Some(())
# }
const fn checked_next_power_of_two(self: Self) -> Option<Self>

Returns the smallest power of two greater than or equal to self. Checks for overflow and returns None if the next power of two is greater than the type’s maximum value. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2usize)?;
let three = NonZero::new(3usize)?;
let four = NonZero::new(4usize)?;
let max = NonZero::new(usize::MAX)?;

assert_eq!(Some(two), two.checked_next_power_of_two() );
assert_eq!(Some(four), three.checked_next_power_of_two() );
assert_eq!(None, max.checked_next_power_of_two() );
# Some(())
# }
const fn ilog2(self: Self) -> u32

Returns the base 2 logarithm of the number, rounded down.

This is the same operation as usize::ilog2, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(7usize)?.ilog2(), 2);
assert_eq!(NonZero::new(8usize)?.ilog2(), 3);
assert_eq!(NonZero::new(9usize)?.ilog2(), 3);
# Some(())
# }
const fn ilog10(self: Self) -> u32

Returns the base 10 logarithm of the number, rounded down.

This is the same operation as usize::ilog10, except that it has no failure cases to worry about since this value can never be zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::new(99usize)?.ilog10(), 1);
assert_eq!(NonZero::new(100usize)?.ilog10(), 2);
assert_eq!(NonZero::new(101usize)?.ilog10(), 2);
# Some(())
# }
const fn midpoint(self: Self, rhs: Self) -> Self

Calculates the midpoint (average) between 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let one = NonZero::new(1usize)?;
let two = NonZero::new(2usize)?;
let four = NonZero::new(4usize)?;

assert_eq!(one.midpoint(four), two);
assert_eq!(four.midpoint(one), two);
# Some(())
# }
const fn is_power_of_two(self: 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

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let eight = NonZero::new(8usize)?;
assert!(eight.is_power_of_two());
let ten = NonZero::new(10usize)?;
assert!(!ten.is_power_of_two());
# Some(())
# }
const fn isqrt(self: Self) -> Self

Returns the square root of the number, rounded down.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let ten = NonZero::new(10usize)?;
let three = NonZero::new(3usize)?;

assert_eq!(ten.isqrt(), three);
# Some(())
# }
const fn cast_signed(self: Self) -> NonZero<isize>

Returns the bit pattern of self reinterpreted as a signed integer of the same size.

Examples

# use std::num::NonZero;

let n = NonZero::<usize>::MAX;

assert_eq!(n.cast_signed(), NonZero::new(-1isize).unwrap());
const fn bit_width(self: Self) -> NonZero<u32>

Returns the minimum number of bits required to represent self.

Examples

#![feature(uint_bit_width)]

# use core::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<usize>::MIN.bit_width(), NonZero::new(1)?);
assert_eq!(NonZero::<usize>::new(0b111)?.bit_width(), NonZero::new(3)?);
assert_eq!(NonZero::<usize>::new(0b1110)?.bit_width(), NonZero::new(4)?);
# Some(())
# }
const fn checked_mul(self: Self, other: Self) -> Option<Self>

Multiplies two non-zero integers together. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2usize)?;
let four = NonZero::new(4usize)?;
let max = NonZero::new(usize::MAX)?;

assert_eq!(Some(four), two.checked_mul(two));
assert_eq!(None, max.checked_mul(two));
# Some(())
# }
const fn saturating_mul(self: Self, other: Self) -> Self

Multiplies two non-zero integers together. Return [NonZero::<usize>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2usize)?;
let four = NonZero::new(4usize)?;
let max = NonZero::new(usize::MAX)?;

assert_eq!(four, two.saturating_mul(two));
assert_eq!(max, four.saturating_mul(max));
# Some(())
# }
unsafe const fn unchecked_mul(self: Self, other: Self) -> Self

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 > usize::MAX.

Examples

#![feature(nonzero_ops)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let two = NonZero::new(2usize)?;
let four = NonZero::new(4usize)?;

assert_eq!(four, unsafe { two.unchecked_mul(two) });
# Some(())
# }
const fn checked_pow(self: Self, other: u32) -> Option<Self>

Raises non-zero value to an integer power. Checks for overflow and returns None on overflow. As a consequence, the result cannot wrap to zero.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3usize)?;
let twenty_seven = NonZero::new(27usize)?;
let half_max = NonZero::new(usize::MAX / 2)?;

assert_eq!(Some(twenty_seven), three.checked_pow(3));
assert_eq!(None, half_max.checked_pow(3));
# Some(())
# }
const fn saturating_pow(self: Self, other: u32) -> Self

Raise non-zero value to an integer power. Return [NonZero::<usize>::MAX] on overflow.

Examples

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
let three = NonZero::new(3usize)?;
let twenty_seven = NonZero::new(27usize)?;
let max = NonZero::new(usize::MAX)?;

assert_eq!(twenty_seven, three.saturating_pow(3));
assert_eq!(max, max.saturating_pow(3));
# Some(())
# }
const fn from_ascii(src: &[u8]) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with decimal digits.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<usize>::from_ascii(b"+10"), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<usize>::from_ascii(b"1 ").is_err());
const fn from_ascii_radix(src: &[u8], radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from an ASCII-byte slice with digits in a given base.

The characters are expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<usize>::from_ascii_radix(b"A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(int_from_ascii)]

# use std::num::NonZero;
#
assert!(NonZero::<usize>::from_ascii_radix(b"1 ", 10).is_err());
const fn from_str_radix(src: &str, radix: u32) -> Result<Self, ParseIntError>

Parses a non-zero integer from a string slice with digits in a given base.

The string is expected to be an optional + sign followed by only digits. Leading and trailing non-digit characters (including whitespace) represent an error. Underscores (which are accepted in Rust literals) also represent an error.

Digits are a subset of these characters, depending on radix:

  • 0-9
  • a-z
  • A-Z

Panics

This method panics if radix is not in the range from 2 to 36.

Examples

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
# fn main() { test().unwrap(); }
# fn test() -> Option<()> {
assert_eq!(NonZero::<usize>::from_str_radix("A", 16), Ok(NonZero::new(10)?));
# Some(())
# }

Trailing space returns error:

#![feature(nonzero_from_str_radix)]

# use std::num::NonZero;
#
assert!(NonZero::<usize>::from_str_radix("1 ", 10).is_err());

impl NonZero<usize>

const fn div_ceil(self: Self, rhs: Self) -> Self

Calculates the quotient of self and rhs, rounding the result towards positive infinity.

The result is guaranteed to be non-zero.

Examples

# use std::num::NonZero;
let one = NonZero::new(1usize).unwrap();
let max = NonZero::new(usize::MAX).unwrap();
assert_eq!(one.div_ceil(max), one);

let two = NonZero::new(2usize).unwrap();
let three = NonZero::new(3usize).unwrap();
assert_eq!(three.div_ceil(two), two);

impl<T> NonZero<T>

const fn new(n: T) -> Option<Self>

Creates a non-zero if the given value is not zero.

unsafe const 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.

fn from_mut(n: &mut T) -> Option<&mut Self>

Converts a reference to a non-zero mutable reference if the referenced value is not zero.

unsafe fn from_mut_unchecked(n: &mut T) -> &mut Self

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.

const fn get(self: Self) -> T

Returns the contained value as a primitive type.

impl From for NonZero<i128>

fn from(small: NonZero<u32>) -> Self

Converts [NonZero]<[u32]> to [NonZero]<[i128]> losslessly.

impl From for NonZero<i128>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[i128]> losslessly.

impl From for NonZero<i128>

fn from(small: NonZero<u64>) -> Self

Converts [NonZero]<[u64]> to [NonZero]<[i128]> losslessly.

impl From for NonZero<i128>

fn from(small: NonZero<i8>) -> Self

Converts [NonZero]<[i8]> to [NonZero]<[i128]> losslessly.

impl From for NonZero<i128>

fn from(small: NonZero<i32>) -> Self

Converts [NonZero]<[i32]> to [NonZero]<[i128]> losslessly.

impl From for NonZero<i128>

fn from(small: NonZero<i64>) -> Self

Converts [NonZero]<[i64]> to [NonZero]<[i128]> losslessly.

impl From for NonZero<i128>

fn from(small: NonZero<u16>) -> Self

Converts [NonZero]<[u16]> to [NonZero]<[i128]> losslessly.

impl From for NonZero<i128>

fn from(small: NonZero<i16>) -> Self

Converts [NonZero]<[i16]> to [NonZero]<[i128]> losslessly.

impl From for NonZero<i16>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[i16]> losslessly.

impl From for NonZero<i16>

fn from(small: NonZero<i8>) -> Self

Converts [NonZero]<[i8]> to [NonZero]<[i16]> losslessly.

impl From for NonZero<i32>

fn from(small: NonZero<u16>) -> Self

Converts [NonZero]<[u16]> to [NonZero]<[i32]> losslessly.

impl From for NonZero<i32>

fn from(small: NonZero<i16>) -> Self

Converts [NonZero]<[i16]> to [NonZero]<[i32]> losslessly.

impl From for NonZero<i32>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[i32]> losslessly.

impl From for NonZero<i32>

fn from(small: NonZero<i8>) -> Self

Converts [NonZero]<[i8]> to [NonZero]<[i32]> losslessly.

impl From for NonZero<i64>

fn from(small: NonZero<i8>) -> Self

Converts [NonZero]<[i8]> to [NonZero]<[i64]> losslessly.

impl From for NonZero<i64>

fn from(small: NonZero<i32>) -> Self

Converts [NonZero]<[i32]> to [NonZero]<[i64]> losslessly.

impl From for NonZero<i64>

fn from(small: NonZero<u16>) -> Self

Converts [NonZero]<[u16]> to [NonZero]<[i64]> losslessly.

impl From for NonZero<i64>

fn from(small: NonZero<i16>) -> Self

Converts [NonZero]<[i16]> to [NonZero]<[i64]> losslessly.

impl From for NonZero<i64>

fn from(small: NonZero<u32>) -> Self

Converts [NonZero]<[u32]> to [NonZero]<[i64]> losslessly.

impl From for NonZero<i64>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[i64]> losslessly.

impl From for NonZero<isize>

fn from(small: NonZero<i16>) -> Self

Converts [NonZero]<[i16]> to [NonZero]<[isize]> losslessly.

impl From for NonZero<isize>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[isize]> losslessly.

impl From for NonZero<isize>

fn from(small: NonZero<i8>) -> Self

Converts [NonZero]<[i8]> to [NonZero]<[isize]> losslessly.

impl From for NonZero<u128>

fn from(small: NonZero<u16>) -> Self

Converts [NonZero]<[u16]> to [NonZero]<[u128]> losslessly.

impl From for NonZero<u128>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[u128]> losslessly.

impl From for NonZero<u128>

fn from(small: NonZero<u32>) -> Self

Converts [NonZero]<[u32]> to [NonZero]<[u128]> losslessly.

impl From for NonZero<u128>

fn from(small: NonZero<u64>) -> Self

Converts [NonZero]<[u64]> to [NonZero]<[u128]> losslessly.

impl From for NonZero<u16>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[u16]> losslessly.

impl From for NonZero<u32>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[u32]> losslessly.

impl From for NonZero<u32>

fn from(small: NonZero<u16>) -> Self

Converts [NonZero]<[u16]> to [NonZero]<[u32]> losslessly.

impl From for NonZero<u64>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[u64]> losslessly.

impl From for NonZero<u64>

fn from(small: NonZero<u32>) -> Self

Converts [NonZero]<[u32]> to [NonZero]<[u64]> losslessly.

impl From for NonZero<u64>

fn from(small: NonZero<u16>) -> Self

Converts [NonZero]<[u16]> to [NonZero]<[u64]> losslessly.

impl From for NonZero<usize>

fn from(small: NonZero<u16>) -> Self

Converts [NonZero]<[u16]> to [NonZero]<[usize]> losslessly.

impl From for NonZero<usize>

fn from(small: NonZero<u8>) -> Self

Converts [NonZero]<[u8]> to [NonZero]<[usize]> losslessly.

impl From for NonZero<usize>

fn from(align: Alignment) -> NonZero<usize>

impl FromStr for NonZero<i128>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<i16>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<i32>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<i64>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<i8>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<isize>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<u128>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<u16>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<u32>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<u64>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<u8>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl FromStr for NonZero<usize>

fn from_str(src: &str) -> Result<Self, <Self as >::Err>

impl Neg for NonZero<i128>

fn neg(self: Self) -> Self

impl Neg for NonZero<i16>

fn neg(self: Self) -> Self

impl Neg for NonZero<i32>

fn neg(self: Self) -> Self

impl Neg for NonZero<i64>

fn neg(self: Self) -> Self

impl Neg for NonZero<i8>

fn neg(self: Self) -> Self

impl Neg for NonZero<isize>

fn neg(self: Self) -> Self

impl TryFrom for NonZero<i128>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[i128]>.

impl TryFrom for NonZero<i128>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[i128]>.

impl TryFrom for NonZero<i128>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[i128]>.

impl TryFrom for NonZero<i128>

fn try_from(value: i128) -> Result<Self, <Self as >::Error>

Attempts to convert i128 to [NonZero]<[i128]>.

impl TryFrom for NonZero<i16>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[i16]>.

impl TryFrom for NonZero<i16>

fn try_from(value: NonZero<u64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u64]> to [NonZero]<[i16]>.

impl TryFrom for NonZero<i16>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[i16]>.

impl TryFrom for NonZero<i16>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[i16]>.

impl TryFrom for NonZero<i16>

fn try_from(value: NonZero<u32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u32]> to [NonZero]<[i16]>.

impl TryFrom for NonZero<i16>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[i16]>.

impl TryFrom for NonZero<i16>

fn try_from(value: NonZero<i32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i32]> to [NonZero]<[i16]>.

impl TryFrom for NonZero<i16>

fn try_from(value: NonZero<u16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u16]> to [NonZero]<[i16]>.

impl TryFrom for NonZero<i16>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[i16]>.

impl TryFrom for NonZero<i16>

fn try_from(value: i16) -> Result<Self, <Self as >::Error>

Attempts to convert i16 to [NonZero]<[i16]>.

impl TryFrom for NonZero<i32>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[i32]>.

impl TryFrom for NonZero<i32>

fn try_from(value: i32) -> Result<Self, <Self as >::Error>

Attempts to convert i32 to [NonZero]<[i32]>.

impl TryFrom for NonZero<i32>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[i32]>.

impl TryFrom for NonZero<i32>

fn try_from(value: NonZero<u64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u64]> to [NonZero]<[i32]>.

impl TryFrom for NonZero<i32>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[i32]>.

impl TryFrom for NonZero<i32>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[i32]>.

impl TryFrom for NonZero<i32>

fn try_from(value: NonZero<u32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u32]> to [NonZero]<[i32]>.

impl TryFrom for NonZero<i32>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[i32]>.

impl TryFrom for NonZero<i64>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[i64]>.

impl TryFrom for NonZero<i64>

fn try_from(value: i64) -> Result<Self, <Self as >::Error>

Attempts to convert i64 to [NonZero]<[i64]>.

impl TryFrom for NonZero<i64>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[i64]>.

impl TryFrom for NonZero<i64>

fn try_from(value: NonZero<u64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u64]> to [NonZero]<[i64]>.

impl TryFrom for NonZero<i64>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[i64]>.

impl TryFrom for NonZero<i64>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[i64]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<u64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u64]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<u32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u32]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<i16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i16]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<u8>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u8]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<i32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i32]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<u16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u16]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[i8]>.

impl TryFrom for NonZero<i8>

fn try_from(value: i8) -> Result<Self, <Self as >::Error>

Attempts to convert i8 to [NonZero]<[i8]>.

impl TryFrom for NonZero<isize>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[isize]>.

impl TryFrom for NonZero<isize>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[isize]>.

impl TryFrom for NonZero<isize>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[isize]>.

impl TryFrom for NonZero<isize>

fn try_from(value: NonZero<u32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u32]> to [NonZero]<[isize]>.

impl TryFrom for NonZero<isize>

fn try_from(value: NonZero<i32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i32]> to [NonZero]<[isize]>.

impl TryFrom for NonZero<isize>

fn try_from(value: isize) -> Result<Self, <Self as >::Error>

Attempts to convert isize to [NonZero]<[isize]>.

impl TryFrom for NonZero<isize>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[isize]>.

impl TryFrom for NonZero<isize>

fn try_from(value: NonZero<u16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u16]> to [NonZero]<[isize]>.

impl TryFrom for NonZero<isize>

fn try_from(value: NonZero<u64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u64]> to [NonZero]<[isize]>.

impl TryFrom for NonZero<u128>

fn try_from(value: NonZero<i16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i16]> to [NonZero]<[u128]>.

impl TryFrom for NonZero<u128>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[u128]>.

impl TryFrom for NonZero<u128>

fn try_from(value: u128) -> Result<Self, <Self as >::Error>

Attempts to convert u128 to [NonZero]<[u128]>.

impl TryFrom for NonZero<u128>

fn try_from(value: NonZero<i32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i32]> to [NonZero]<[u128]>.

impl TryFrom for NonZero<u128>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[u128]>.

impl TryFrom for NonZero<u128>

fn try_from(value: NonZero<i8>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i8]> to [NonZero]<[u128]>.

impl TryFrom for NonZero<u128>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[u128]>.

impl TryFrom for NonZero<u128>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[u128]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<i8>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i8]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<u64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u64]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<i16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i16]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<u32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u32]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: u16) -> Result<Self, <Self as >::Error>

Attempts to convert u16 to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<i32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i32]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u16>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[u16]>.

impl TryFrom for NonZero<u32>

fn try_from(value: NonZero<i32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i32]> to [NonZero]<[u32]>.

impl TryFrom for NonZero<u32>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[u32]>.

impl TryFrom for NonZero<u32>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[u32]>.

impl TryFrom for NonZero<u32>

fn try_from(value: NonZero<i8>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i8]> to [NonZero]<[u32]>.

impl TryFrom for NonZero<u32>

fn try_from(value: NonZero<u64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u64]> to [NonZero]<[u32]>.

impl TryFrom for NonZero<u32>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[u32]>.

impl TryFrom for NonZero<u32>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[u32]>.

impl TryFrom for NonZero<u32>

fn try_from(value: NonZero<i16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i16]> to [NonZero]<[u32]>.

impl TryFrom for NonZero<u32>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[u32]>.

impl TryFrom for NonZero<u32>

fn try_from(value: u32) -> Result<Self, <Self as >::Error>

Attempts to convert u32 to [NonZero]<[u32]>.

impl TryFrom for NonZero<u64>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[u64]>.

impl TryFrom for NonZero<u64>

fn try_from(value: u64) -> Result<Self, <Self as >::Error>

Attempts to convert u64 to [NonZero]<[u64]>.

impl TryFrom for NonZero<u64>

fn try_from(value: NonZero<i32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i32]> to [NonZero]<[u64]>.

impl TryFrom for NonZero<u64>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[u64]>.

impl TryFrom for NonZero<u64>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[u64]>.

impl TryFrom for NonZero<u64>

fn try_from(value: NonZero<i8>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i8]> to [NonZero]<[u64]>.

impl TryFrom for NonZero<u64>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[u64]>.

impl TryFrom for NonZero<u64>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[u64]>.

impl TryFrom for NonZero<u64>

fn try_from(value: NonZero<i16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i16]> to [NonZero]<[u64]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<u64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u64]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<usize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[usize]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<u16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u16]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<i16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i16]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<u32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u32]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: u8) -> Result<Self, <Self as >::Error>

Attempts to convert u8 to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<i32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i32]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<u8>

fn try_from(value: NonZero<i8>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i8]> to [NonZero]<[u8]>.

impl TryFrom for NonZero<usize>

fn try_from(value: NonZero<i8>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i8]> to [NonZero]<[usize]>.

impl TryFrom for NonZero<usize>

fn try_from(value: NonZero<i64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i64]> to [NonZero]<[usize]>.

impl TryFrom for NonZero<usize>

fn try_from(value: NonZero<i16>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i16]> to [NonZero]<[usize]>.

impl TryFrom for NonZero<usize>

fn try_from(value: NonZero<i128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i128]> to [NonZero]<[usize]>.

impl TryFrom for NonZero<usize>

fn try_from(value: usize) -> Result<Self, <Self as >::Error>

Attempts to convert usize to [NonZero]<[usize]>.

impl TryFrom for NonZero<usize>

fn try_from(value: NonZero<u64>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u64]> to [NonZero]<[usize]>.

impl TryFrom for NonZero<usize>

fn try_from(value: NonZero<u32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u32]> to [NonZero]<[usize]>.

impl TryFrom for NonZero<usize>

fn try_from(value: NonZero<i32>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[i32]> to [NonZero]<[usize]>.

impl TryFrom for NonZero<usize>

fn try_from(value: NonZero<u128>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[u128]> to [NonZero]<[usize]>.

impl TryFrom for NonZero<usize>

fn try_from(value: NonZero<isize>) -> Result<Self, <Self as >::Error>

Attempts to convert [NonZero]<[isize]> to [NonZero]<[usize]>.

impl<T> Any for NonZero<T>

fn type_id(self: &Self) -> TypeId

impl<T> Binary for NonZero<T>

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

impl<T> BitOr for NonZero<T>

fn bitor(self: Self, rhs: Self) -> <Self as >::Output

impl<T> BitOr for NonZero<T>

fn bitor(self: Self, rhs: T) -> <Self as >::Output

impl<T> BitOrAssign for NonZero<T>

fn bitor_assign(self: &mut Self, rhs: T)

impl<T> BitOrAssign for NonZero<T>

fn bitor_assign(self: &mut Self, rhs: Self)

impl<T> Borrow for NonZero<T>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for NonZero<T>

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

impl<T> Clone for NonZero<T>

fn clone(self: &Self) -> Self

impl<T> CloneToUninit for NonZero<T>

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

impl<T> Copy for NonZero<T>

impl<T> Debug for NonZero<T>

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

impl<T> Display for NonZero<T>

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

impl<T> Eq for NonZero<T>

impl<T> Freeze for NonZero<T>

impl<T> From for NonZero<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Hash for NonZero<T>

fn hash<H>(self: &Self, state: &mut H)
where
    H: Hasher

impl<T> LowerExp for NonZero<T>

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

impl<T> LowerHex for NonZero<T>

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

impl<T> Octal for NonZero<T>

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

impl<T> Ord for NonZero<T>

fn cmp(self: &Self, other: &Self) -> Ordering
fn max(self: Self, other: Self) -> Self
fn min(self: Self, other: Self) -> Self
fn clamp(self: Self, min: Self, max: Self) -> Self

impl<T> PartialEq for NonZero<T>

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

impl<T> PartialOrd for NonZero<T>

fn partial_cmp(self: &Self, other: &Self) -> Option<Ordering>
fn lt(self: &Self, other: &Self) -> bool
fn le(self: &Self, other: &Self) -> bool
fn gt(self: &Self, other: &Self) -> bool
fn ge(self: &Self, other: &Self) -> bool

impl<T> RefUnwindSafe for NonZero<T>

impl<T> Send for NonZero<T>

impl<T> StructuralPartialEq for NonZero<T>

impl<T> Sync for NonZero<T>

impl<T> Unpin for NonZero<T>

impl<T> UnsafeUnpin for NonZero<T>

impl<T> UnwindSafe for NonZero<T>

impl<T> UpperExp for NonZero<T>

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

impl<T> UpperHex for NonZero<T>

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

impl<T> UseCloned for NonZero<T>

impl<T, U> Into for NonZero<T>

fn into(self: Self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom for NonZero<T>

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

impl<T, U> TryInto for NonZero<T>

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