#non-zero #const #no-std

no-std nz

Collection of 100% safe macros for creating non-zero integers type more easily

17 unstable releases (3 breaking)

Uses old Rust 2015

new 0.4.0-beta.2 May 5, 2024
0.4.0-beta.1 May 3, 2024
0.3.5 May 3, 2024
0.3.3 Mar 8, 2024
0.1.4 Jul 29, 2023

#840 in Rust patterns

Download history 4/week @ 2024-02-11 76/week @ 2024-02-18 9/week @ 2024-02-25 140/week @ 2024-03-03 42/week @ 2024-03-10 6/week @ 2024-03-17 14/week @ 2024-03-31 2/week @ 2024-04-07 3/week @ 2024-04-21 305/week @ 2024-04-28

310 downloads per month

Zlib OR Apache-2.0 OR MIT

14KB

nz

github crates.io docs.rs rust-ci unsafety license

Table of contents

Description

The nz crate provides a collection of macros that simplify the creation of the NonZero type. With these macros, you can easily generate constants of the generic type using literals, constant values or expressions at compiletime.

Disclaimer

This beta version of nz uses the inline_const and generic_nonzero features that require the nightly toolchain until they are both included in a future stable release.

Changelog

All changes to nz crate are documented in CHANGELOG.md.

Features

  • No unsafe code
  • No dependencies
  • no_std compatible
  • Supports every type that implements the ZeroablePrimitive marker trait
  • Compile-time evaluation

Macros

Type Macro
NonZero<i8> nz::i8!
NonZero<i16> nz::i16!
NonZero<i32> nz::i32!
NonZero<i64> nz::i64!
NonZero<i128> nz::i128!
NonZero<isize> nz::isize!
NonZero<u8> nz::u8!
NonZero<u16> nz::u16!
NonZero<u32> nz::u32!
NonZero<u64> nz::u64!
NonZero<u128> nz::u128!
NonZero<usize> nz::usize!

Usage

use std::num::NonZero;

// A `NonZero<T>` type can be constructed from different types of
// arguments with the matching `nz` macro.
// Such argument can be an integer literal,
const NZ_MIN: NonZero<u8> = nz::u8!(1);
let nz_two = nz::u8!(2);
// a constant value,
const NZ_MAX: NonZero<u8> = nz::u8!(u8::MAX);
const SIX: u8 = 6;
let six = nz::u8!(SIX);
// or even a constant expression.
const RES: NonZero<u8> = nz::u8!({ 3 + 7 } - NZ_MIN.get());
let res = nz::u8!((NZ_MIN.get() & NZ_MAX.get()) + 7);
let five = nz::u8!({ const FIVE: u8 = 5; FIVE });
// However, a non-constant expression results in a compile-time error.
// const __ERR: NonZero<u8> = nz::u8!({ 3 + 7 } - nz_two.get());
# assert_eq!(1, NZ_MIN.get());

License

This library is distributed under the terms of either of the following licenses at your option:

No runtime deps