nonzero_macros 0.16.1

Macros for compile-time checked `NonZero` integers and safe `NonNull` pointer creation in `no_std` environments.
Documentation
  • Coverage
  • 100%
    4 out of 4 items documented4 out of 4 items with examples
  • Size
  • Source code size: 22.0 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 49.5 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 3s Average build duration of successful builds.
  • all releases: 3s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • AFLplusplus/LibAFL
    2688 488 105
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • aflpp

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_macros::nonzero;
use core::num::NonZeroUsize;

const MY_VAL: NonZeroUsize = nonzero!(10);

try_nonzero!

Constructs an Option<NonZero*> type. Works at both compile-time and runtime. Returns None if the value is zero.

use nonzero_macros::try_nonzero;
use core::num::NonZeroUsize;

const MY_VAL: Option<NonZeroUsize> = try_nonzero!(10);
const MY_ZERO: Option<NonZeroUsize> = try_nonzero!(0); // None

let x = 10;
let val = try_nonzero!(x);

nonnull_raw_mut!

Constructs a core::ptr::NonNull from a mutable reference expression. Useful for static muts.

use nonzero_macros::nonnull_raw_mut;

static mut MY_STATIC: i32 = 0;
let ptr = nonnull_raw_mut!(MY_STATIC);

License