nz 0.1.2

Collection of 100% safe macros for creating numeric non-zero types more easily.
Documentation

nz

crates.io docs License:Zlib Minimum Rust Version Build status Unsafe-Zero-Percent

The nz crate provides a collection of user-friendly macros that simplify the creation of new instances of non-zero numeric types found in the core::num. With these macros, you can effortlessly generate instances using numeric literals, constant values and constant expressions, all at compile time.

Features

  • No unsafe code
  • No dependencies
  • no_std compatible
  • Supports all numeric non-zero types from the core::num module
  • Compile time evaluation
  • Zero detection at compile time

NonZero macros

Type Macro
NonZeroI8 nz::i8!
NonZeroI16 nz::i16!
NonZeroI32 nz::i32!
NonZeroI64 nz::i64!
NonZeroI128 nz::i128!
NonZeroIsize nz::isize!
NonZeroU8 nz::u8!
NonZeroU16 nz::u16!
NonZeroU32 nz::u32!
NonZeroU64 nz::u64!
NonZeroU128 nz::u128!
NonZeroUsize nz::usize!

Basic usage

use core::num::NonZeroU8;

const NZ_U8_MIN: NonZeroU8 = nz::u8!(1); // with numeric literal
const NZ_U8_MAX: NonZeroU8 = nz::u8!(u8::MAX); // with constant value
let sum = nz::u8!(NZ_U8_MAX.get() & NZ_U8_MIN.get() + 7); // with constant expression

Remarks

Non-zero macros cannot be used with constant function arguments as they are not considered as constant values.

Example

use core::num::NonZeroU64;

const fn wrapping_add_nz(a: u64, b: NonZeroU64) -> NonZeroU64 {
    // `a` and `b` is not constant
    nz::u64!(a.wrapping_add(b.get())) // <- cause of the compile error
}
let nz = wrapping_add_nz(2, nz::u64!(1));