Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NonZero* types for BigInt and related numbers #435

Open
ActuallyHappening opened this issue Apr 23, 2024 · 2 comments
Open

NonZero* types for BigInt and related numbers #435

ActuallyHappening opened this issue Apr 23, 2024 · 2 comments

Comments

@ActuallyHappening
Copy link

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

@cuviper
Copy link
Member

cuviper commented Apr 23, 2024

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.

@ActuallyHappening
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants