Elevate `NonZero*` types to true primitive integer types?

NonZero is useful, but in practice it is very limited. If I have a function that receives a x and does 100 / (x - 1), then in order to avoid division by zero, the type of x must be NonOne, not NonZero.

In current Rust we can build NonOne in library code by wrapping NonZero, like what the nonmax crate crate does. But this has overhead (unless llvm neatly see through the whole thing)

What Rust really needs is integer types bounded by a range, like in Ada. There was a proposal here (pattern types) that suggested something like type NonZero = i32 in 1.. and that would be excellent. (But just a range is still not enough to build NonOne; one would also need a way to combine multiple disjoint ranges, like, type NonOne = i32 in 0..=0 | 2..)

3 Likes