num
num copied to clipboard
`NonZero*` types for `BigInt` and related numbers
Hey guys,
I'm wanting to express certain invariants in my rust types while also handling arbitrarily large numbers, and so was refactoring from NonZeroI32
to NonZeroBigInt
when I realised that NonZeroBigInt
doesn't actually exist, and no analogue to this type already exists.
Even if the compiler doesn't do niche-value optimisation as I think NonZeroI32
and related std library types do, can we still get a type NonZeroBigInt
and related types (e.g. NonZeroBigUInt
)?
Thanks
It's possible to express this, but I am adverse to duplicating a bunch of API surface on such types, and there would not be any niche benefit since the value is behind a heap allocation. We do already get niches from the inner Vec
though, which was greatly expanded in rust-lang/rust#106790 too.
I'm considering writing a library specifically for expressing a wrapping newtype pattern which only restricts the valid values of the inner type using a simple derive macro, e.g. NoWhitespaceString or NonZeroMyNum The hesitancy to add this type I totally understand, but I kinda want to do something to help solve as well
The ability to express invariants in my type that then get 'magically' applied to serde, std::fmt::Display/Debug e.t.c. is one feature of Rust I very much enjoy