-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add NonZero{I,U}{8,16,32,64,128,size}::{MIN,MAX}
#89065
Comments
I think this sounds like a good feature, and I would like to claim it for a first contribution. |
@rustbot claim |
Not that I mind the addition of these constants in general, but your motivating example is a bit unfortunate in that it's a slightly obfuscated #![feature(nonzero_ops)]
use std::num::NonZeroU64;
pub fn baz(x: u64) -> NonZeroU64 {
NonZeroU64::new(1).unwrap().saturating_add(x)
// or `NonZeroU64::new(x.saturating_add(1)).unwrap()` if you need a solution on stable
} Yes, that does contain an |
There are projects that forbid the usage of |
This might just be my head being in a more Ada space, but not having immediate access to the MIN and MAX for a given scalar type feels wrong to me. Obviously with a little forethought you can come up with a totally safe unwrap, but when you're doing something simple like initializing values for a comparison algorithm, it seems like it might be a bit better if you can use a constant which took care of that thinking about safety for you. |
@mjclements: Thank you. This:
is a good motivating example if you ask me. @EFanZh: Sounds like these projects blanket forbid #![feature(nonzero_ops)]
#![feature(const_option)]
use std::num::NonZeroU64;
pub fn bax(x: u64) -> NonZeroU64 {
const ONE: NonZeroU64 = NonZeroU64::new(1).unwrap();
ONE.saturating_add(x)
} Of course that currently requires nightly features and not sure how hard it would be to improve Clippy. Hopefully that will become viable for stable projects soon. |
Some interrelated thoughts on the stated problems this is trying to solve:
(None of this is intended to argue that |
I like the points @kpreid made. I would concur that ONE as a constant might be useful, but should not be instead of a MIN, even for the signed integers. I think having the lowest possible value clearly demarcated as such has value. (It would be kinda nice to have a fast way of making number literals of this type. I think that would probably help with adoption. Feel free to tag me if you open an issue!) |
Agreed.
Not sure I follow. The code example at the beginning used use std::num::NonZeroU64;
pub fn bax(x: u64) -> NonZeroU64 {
NonZeroU64::MIN.saturating_add(x)
} Luckily, I haven't seen anybody propose such a gross abuse of the
Yes, getting a literal |
Implement `MIN`/`MAX` constants for non-zero integers This adds the associated [`MIN`](https://doc.rust-lang.org/stable/std/primitive.usize.html#associatedconstant.MIN)/[`MAX`](https://doc.rust-lang.org/stable/std/primitive.usize.html#associatedconstant.MAX) constants to `NonZero{U,I}{8,16,32,64,128,size}`, requested in rust-lang#89065. This reimplements rust-lang#89077 due that PR being stagnant for 4 months. I am fine with closing this in favor of that one if the author revisits it. If so, I'd like to see that PR have the docs link to the `$Int`'s constants.
This is now available on nightly (docs). |
NonZero{I,U}{8,16,32,64,128}::{MIN,MAX}
NonZero{I,U}{8,16,32,64,128,size}::{MIN,MAX}
Excellent, glad these have landed. Am discussing having these MIN/MAX available from traits: |
Is there anything blocking the stabilization of this feature? |
I guess this feature is simple and mature enough for stable toolchains. @nvzqz Are you willing to create a stabilization report? |
@rust-lang/libs-api: For the unsigned nonzero integers: https://doc.rust-lang.org/1.66.0/std/num/struct.NonZeroU32.html#associatedconstant.MIN For the signed nonzero integers: https://doc.rust-lang.org/1.66.0/std/num/struct.NonZeroI32.html#associatedconstant.MIN |
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
…=dtolnay Stabilize `nonzero_min_max` ## Overall Stabilizes `nonzero_min_max` to allow the "infallible" construction of ordinary minimum and maximum `NonZero*` instances. The feature is fairly straightforward and already matured for some time in stable toolchains. ```rust let _ = NonZeroU8::MIN; let _ = NonZeroI32::MAX; ``` ## History * On 2022-01-25, implementation was [created](rust-lang#93293). ## Considerations * This report is fruit of the inanition observed after two unsuccessful attempts at getting feedback. * Other constant variants discussed at rust-lang#89065 (comment) are orthogonal to this feature. Fixes rust-lang#89065
Stabilize `nonzero_min_max` ## Overall Stabilizes `nonzero_min_max` to allow the "infallible" construction of ordinary minimum and maximum `NonZero*` instances. The feature is fairly straightforward and already matured for some time in stable toolchains. ```rust let _ = NonZeroU8::MIN; let _ = NonZeroI32::MAX; ``` ## History * On 2022-01-25, implementation was [created](rust-lang/rust#93293). ## Considerations * This report is fruit of the inanition observed after two unsuccessful attempts at getting feedback. * Other constant variants discussed at rust-lang/rust#89065 (comment) are orthogonal to this feature. Fixes rust-lang/rust#89065
Currently, there is no way to obtain a
NonZero*
value without introducing unsafe codes orunwrap()
calls, providing these constants can make it easier to use these types. For example:The text was updated successfully, but these errors were encountered: