Macro nz

Source
macro_rules! nz {
    ($int:expr) => { ... };
}
Expand description

Constructs a std::num::NonZero* from an integer constant.

§Type Inference

This macro can infer the argument’s type from the return type and vice-versa, so you can do either of:

  • let _ = nz!(1u8);
  • let _: NonZeroU8 = nz!(1);

§Compile-time errors

This macro checks that the $int argument is non-zero at compile-time, producing a compile-time error if it is 0.

§Example

§Usage

use notzero::nz;
use std::num::NonZeroU64;
 
let two = nz!(2u16); // returns a `NonZeroU16`
assert_eq!(two.get(), 2u16);
 
// infers the argument's type from the returned `NonZero`
let THREE: NonZeroU64 = nz!(3); 
assert_eq!(THREE.get(), 3u64);

§Zero argument

let _ = notzero::nz!(0);

the above code produces this compile-time error:

error[E0080]: evaluation of `main::_doctest_main_src_lib_rs_77_0::{constant#0}` failed
 --> src/lib.rs:81:9
  |
7 | let _ = notzero::nz!(0);
  |         ^^^^^^^^^^^^^^^ the evaluated program panicked at 'passed in a `0` argument', src/lib.rs:7:9
  |
  = note: this error originates in the macro `notzero::nz` (in Nightly builds, run with -Z macro-backtrace for more info)