Crates.io | nz |
lib.rs | nz |
version | 0.4.1 |
source | src |
created_at | 2023-07-29 01:16:38.766786 |
updated_at | 2024-07-16 17:55:38.933015 |
description | Collection of 100% safe macros for creating non-zero integers more easily. |
homepage | |
repository | https://github.com/noelhorvath/nz |
max_upload_size | |
id | 929018 |
size | 36,726 |
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.
All changes to nz
crate are documented in CHANGELOG.md.
no_std
compatibleZeroablePrimitive
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());
This library is distributed under the terms of either of the following licenses at your option: