#non-zero

no-std non0

Compile-time checked non-zero integers with type inference and first-class const support

1 unstable release

0.1.0 Jul 9, 2024

#658 in Math

MIT/Apache

7KB
91 lines

Compile-time checked non-zero integers with type inference and first-class const support.

const UNSIGNED: NonZeroUsize = nonzero!(1);
const SIGNED: NonZeroI8 = nonzero!(-1);
           // ^ correctly infers return type

const MY_CONST: usize = 20;
const MY_NONZERO_CONST: NonZeroUsize = nonzero!(MY_CONST - 19);
              // refer to other constant values ^
let runtime = nonzero!(0); // eager evaluation

Comparison with other libraries

  • nonzero uses a proc-macro that parses the expression passed to it, precluding
    • Type inference.
    • Referencing constants.

lib.rs:

Compile-time checked non-zero integers with type inference and first-class const support.

const UNSIGNED: NonZeroUsize = nonzero!(1);
const SIGNED: NonZeroI8 = nonzero!(-1);
           // ^ correctly infers return type

const MY_CONST: usize = 20;
const MY_NONZERO_CONST: NonZeroUsize = nonzero!(MY_CONST - 19);
              // refer to other constant values ^
let runtime = nonzero!(0); // eager evaluation

Comparison with other libraries

  • nonzero uses a proc-macro that parses the expression passed to it, precluding
    • Type inference.
    • Referencing constants.

Dependencies

~0.6–0.8MB
~10K SLoC