Crate nonn

source ·
Expand description

nonn Crates.io

Integer types that are known not to equal any single N, enabling some memory layout optimization.

Most of nonn’s code is lifted from core::num::nonzero and slightly adapted.

nonn is backed by core::num::NonZero*, ensuring

use std::mem::size_of;

assert_eq!(size_of::<Option<nonn::NonNU8<0>>>(), size_of::<u8>());
assert_eq!(size_of::<Option<nonn::NonNU16<1>>>(), size_of::<u16>());
assert_eq!(size_of::<Option<nonn::NonNU32<2>>>(), size_of::<u32>());
assert_eq!(size_of::<Option<nonn::NonNU64<3>>>(), size_of::<u64>());
assert_eq!(size_of::<Option<nonn::NonNU128<4>>>(), size_of::<u128>());
assert_eq!(size_of::<Option<nonn::NonNUsize<5>>>(), size_of::<usize>());
assert_eq!(size_of::<Option<nonn::NonNI8<6>>>(), size_of::<i8>());
assert_eq!(size_of::<Option<nonn::NonNI16<7>>>(), size_of::<i16>());
assert_eq!(size_of::<Option<nonn::NonNI32<8>>>(), size_of::<i32>());
assert_eq!(size_of::<Option<nonn::NonNI64<9>>>(), size_of::<i64>());
assert_eq!(size_of::<Option<nonn::NonNI128<10>>>(), size_of::<i128>());
assert_eq!(size_of::<Option<nonn::NonNIsize<11>>>(), size_of::<isize>());

nonn functions in no_std environments.

Limitations

NonN* types will inevitably introduce many branches in most methods. If you require higher performance, it is advisable to use either core::num::nonzero or the nonmax crate. These alternative options are more specialized and can thus avoid many of the checks nonn uses.

nonn currently does not implement Neg for its types, as Neg on NonNU*<N> should in theory result in NonNI*<-N>. This is currently not possible to my knowledge, even on nightly.

nonn requires at least Rust 1.57.0 to compile, but 1.67.0 or above is required for ilog2 and ilog10.

Structs

  • 8-bit signed integer type that is known not to equal to any single value N.
  • 16-bit signed integer type that is known not to equal to any single value N.
  • 32-bit signed integer type that is known not to equal to any single value N.
  • 64-bit signed integer type that is known not to equal to any single value N.
  • 128-bit signed integer type that is known not to equal to any single value N.
  • Pointer-sized signed integer type that is known not to equal to any single value N.
  • 8-bit unsigned integer type that is known not to equal to any single value N.
  • 16-bit unsigned integer type that is known not to equal to any single value N.
  • 32-bit unsigned integer type that is known not to equal to any single value N.
  • 64-bit unsigned integer type that is known not to equal to any single value N.
  • 128-bit unsigned integer type that is known not to equal to any single value N.
  • Pointer-sized unsigned integer type that is known not to equal to any single value N.