nonzero_macros
Macros for compile-time checked NonZero integers and safe NonNull pointer creation in no_std environments.
Far simpler than other crates, as it leverages the modern generic core::num::NonZero type.
Lightweight, no_std, and dependency-free.
Macros
nonzero!
Constructs a NonZero* type at compile-time. Panics if the value is zero.
use nonzero;
use NonZeroUsize;
const MY_VAL: NonZeroUsize = nonzero!;
try_nonzero!
Constructs an Option<NonZero*> type. Works at both compile-time and runtime.
Returns None if the value is zero.
use try_nonzero;
use NonZeroUsize;
const MY_VAL: = try_nonzero!;
const MY_ZERO: = try_nonzero!; // None
let x = 10;
let val = try_nonzero!;
nonnull_raw_mut!
Constructs a core::ptr::NonNull from a mutable reference expression. Useful for static muts.
use nonnull_raw_mut;
static mut MY_STATIC: i32 = 0;
let ptr = nonnull_raw_mut!;