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

**URL:** <https://internals.rust-lang.org/t/elevate-nonzero-types-to-true-primitive-integer-types/18040>\
**Category:** language design\
**Created:** [December 27, 2022, 7:12pm UTC](https://internals.rust-lang.org/t/elevate-nonzero-types-to-true-primitive-integer-types/18040 "2022-12-27T19:12:46Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [January 7, 2023, 4:57pm UTC](https://internals.rust-lang.org/t/elevate-nonzero-types-to-true-primitive-integer-types/18040/9 "2023-01-07T16:57:35Z")

</div>

> [@bascule](#):
>
> I would also argue that `NonZero` is useful in and of itself. One important application is using type safety to ensure panic-free division, inversions, modulus/remainder, etc by using the type system to ensure the divisor is non-zero.

`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](https://github.com/LPGhatguy/nonmax/) 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](https://internals.rust-lang.org/t/thoughts-on-pattern-types-and-subtyping/17675)) 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..`)

---

_[View the full topic](https://internals.rust-lang.org/t/elevate-nonzero-types-to-true-primitive-integer-types/18040)._
