num icon indicating copy to clipboard operation
num copied to clipboard

`NonZero*` types for `BigInt` and related numbers

Open ActuallyHappening opened this issue 10 months ago • 2 comments

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

ActuallyHappening avatar Apr 23 '24 01:04 ActuallyHappening

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.

cuviper avatar Apr 23 '24 18:04 cuviper

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

ActuallyHappening avatar May 23 '24 11:05 ActuallyHappening