non-zero 0.2.0

A macro for creating constant non-zero integers (with type inference).
Documentation
  • Coverage
  • 100%
    2 out of 2 items documented1 out of 1 items with examples
  • Size
  • Source code size: 33.77 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 462.02 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 13s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • SLUCHABLUB/non-zero
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • SLUCHABLUB

non-zero

A macro for creating constant non-zero integers (with type inference).

There are multiple crates that address the issue of initialising non-zero integers. However, most of them lack type inference. This is why I created non_zero!. It uses const blocks to evaluate the literals at compile time whilst achieving type inference.

The definition is essentially this:

macro_rules! non_zero {
    ($n:expr) => {
        const {
            NonZero::new($n).unwrap()
        }
    };
}

Some things of note:

  • $n:expr allows for any expression to be passed to the macro, so long as it is evaluable in a const context.
  • The const block is discussed above.
  • NonZero is the std generic non-zero type to which all NonZero*** are aliased. The generic argument can be inferred, not only from $n but also from the macro's usage.
  • .unwrap() inside the const block will cause a compile-time error, not a runtime one.

The above implementation is somewhat simplified; the real definition produces prettier errors than unwrap and is more hygienic.

Naming

This crate uses the same name as std does for its types. Namely, with a hyphen between "non" and "zero". In snake_case the hyphen becomes an underscore and in PascalCase it creates a word break.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.