-
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
step_by() API change #43063
Comments
Is this a deliberate regression? Trying to follow along with the discussion for the new step_by, but it seems quite tangled and I don't see a definitive RFC. cc @SimonSapin based on your comments in #43012 |
#27741 (comment) looks like a decision to move One way to think of it is as a number of iterations rather than a "distance" between points. |
There are situations where I'd like to write:
That is, negative stepping even when the range is on unsigned values :-) (This code was disallowed even by the old step_by, but it's handy). |
@leonardo-m This has been discussed before and is quickly dismissed because it is confusing. Should I guess you need to write your own iterator or function producing |
I think it should give [20, 18, 16, 14, 12, 10, 8, 6, 4, 2]. Do you suggest to close this issue down then? |
@leonardo-m I do think |
It's a quite limiting API. What about using isize? |
@leonardo-m What would that do? Just |
It's a single operation. |
Right-- I'm asking how that operation should be implemented generically for iterators. It seems like the most obvious way would be to call |
The "-2" isn't always hard-coded, in general it's a variable, and it can change at run-time. So you don't know if you should put a rev() there or not. If you look in other threads people are proposing solutions to this... a way to reverse the scanning conditionally... |
As I’ve said elsewhere, I ended up writing this for Servo: struct MaybeReverse<I> {
iter: I,
reverse: bool,
}
impl<I: DoubleEndedIterator> Iterator for MaybeReverse<I> {
type Item = I::Item;
fn next(&mut self) -> Option<I::Item> {
if self.reverse {
self.iter.next_back()
} else {
self.iter.next()
}
}
} |
Yes, I saw that... step_by() is going to be a very commonly used function, and I think most times it will be called on ranges of integral values. Currently you can't use it ( #43064 ). If you want Rust code to compile fast you need to keep extrinsic complexity low and abstraction as low as possible, especially for commonly used things. |
@leonardo-m #43064 is not controversial. I’m working on a PR. |
#43077 should fix the performance problem. |
Thank you. In my last comment I was talking about general Rust code compilation speed (while Issue 43064 is about run-time performance). Code where you don't know at compile-time if you need to step forwards or backwards exists, I think I have such code in some matrix scanning strategies, but it's uncommon. |
I close this down because most API discussions already happened elsewhere, and they are settled. |
In past step_by() needed an argument of the same type of the range:
But now it gives an error and asks for an usize:
Is this desired (and I have to change all my Nightly code)?
This is especially bad when I've used negative step values:
(a - 2 .. 1).step_by(-2)
Now you need to write something like:
(2 + a % 2 .. a - 1).rev().step_by(2)
See also #43012
The text was updated successfully, but these errors were encountered: