-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
[ER] NonZeroX Step and better constructors #73121
Comments
There's not really a point in calling |
The less unwraps you have, the less you have to think (or in more reliable coding to prove) about possible ways for them to fire at run-time and ruin your execution. |
I'd like also the .step_by(usize).
I think this isn't always true (I've seen such cases in my code, where the inliner wasn't able to get rid of all the unwrap calls), if you have iterators chains, like (where pred computes a division by the input argument): (1 .. 1000)
.map(...)
.filter(pred)
... The compiler is not always able (or willing) to inline everything, so predicates and other lambdas get called by values that are nonzero, but the functions lose the information about the input argument being nonzero. But if you have a range of NonZero values the type system can't lose this information and new improvements like #79134 give you some extra performance in non-inlined functions: const NZ1: NonZeroU32 = NonZeroU32::new(1).unwrap();
const NZ1000: NonZeroU32 = NonZeroU32::new(1000).unwrap();
(NZ1 .. NZ1000)
.map(...)
.filter(pred)
... |
This is also being discussed in the libs-team: rust-lang/libs-team#130. |
@leonardo-m it's reasonable that the compiler cannot optimize after you have mapped the integer, right? |
It could be nice to have std::iter::Step for the NonZeroX numbers, so in this code instead of having about 1000 unwrap:
You only need two:
If also a const-generics-based constructor is added to stdlib then the number of run-time unwraps goes to zero (the two panics become compile-time):
A further improvement should come from const generics of arbitrary type (currently not allowed):
The text was updated successfully, but these errors were encountered: