1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//! # 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.
/// A macro for creating non-zero integers.
///
/// See the [crate level docs](crate).