Skip to main content

Crate nonzero_macros

Crate nonzero_macros 

Source
Expand description

§nonzero_macros

LibAFL logo

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
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
# `nonzero_macros`

A collection of lightweight, no_std, dependency-free macros for LibAFL and general Rust development.

§Macros

  • nonzero!: A compile-time checked way to create NonZero* types. Panics if the value is zero.
  • try_nonzero!: A compile-time checked way to create Option<NonZero*> types. Returns None if the value is zero.
  • nonnull_raw_mut!: A safe wrapper around &raw mut that returns a NonNull pointer.

Macros§

nonnull_raw_mut
Get a core::ptr::NonNull to a global static mut (or similar).
nonzero
Zero-cost way to construct NonZero* types from core::num at compile-time.
try_nonzero
Construct Option<NonZero*> types from core::num.