nonzero_lit
A small macro crate providing safe, easy, and fully zero-cost way to construct constant or literal instances of the NonZero* types from core::num.
Features
-
Crate fully supports
no_std. -
All
NonZerotypes are supported:core::num::NonZeroUsizevia thenonzero_lit::usize!macro.core::num::NonZeroIsizevia thenonzero_lit::isize!macro.core::num::NonZeroU128via thenonzero_lit::u128!macro.core::num::NonZeroI128via thenonzero_lit::i128!macro.core::num::NonZeroU64via thenonzero_lit::u64!macro.core::num::NonZeroI64via thenonzero_lit::i64!macro.core::num::NonZeroU32via thenonzero_lit::u32!macro.core::num::NonZeroI32via thenonzero_lit::i32!macro.core::num::NonZeroU16via thenonzero_lit::u16!macro.core::num::NonZeroI16via thenonzero_lit::i16!macro.core::num::NonZeroU8via thenonzero_lit::u8!macro.core::num::NonZeroI8via thenonzero_lit::i8!macro.
-
Fully zero cost, even for debug builds — we always evaluate the constant as a
const. -
Input to the macros can be arbitrary constant expressions. This includes
const fncalls, which would be more difficult to verify the result as non-zero by hand. -
Misuse (trying to make a
NonZero$Intwith a zero value) is always detected at compile time, even when the macro is not being used to initialize a constant. -
No unsafe code.
Usage
Add this to your Cargo.toml:
[]
= "0.1"
Examples
let x = i32!;
assert_eq!;
const FERRIS: NonZeroU32 = u32!;
assert_eq!;
const FERRIS: NonZeroU32 = u32!;
assert_eq!;
License
Public domain, as explained here. If that's unacceptable, it's also available under either the Apache-2.0 or MIT licenses, at your option.