Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upstep_by() API change #43063
Comments
leonardo-m
referenced this issue
Jul 5, 2017
Merged
Delete deprecated & unstable range-specific `step_by` #43012
bstrie
added
the
T-libs
label
Jul 5, 2017
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
#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. |
This comment has been minimized.
This comment has been minimized.
|
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). |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
|
I think it should give [20, 18, 16, 14, 12, 10, 8, 6, 4, 2]. Do you suggest to close this issue down then? |
This comment has been minimized.
This comment has been minimized.
|
@leonardo-m I do think |
This comment has been minimized.
This comment has been minimized.
|
It's a quite limiting API. What about using isize? |
This comment has been minimized.
This comment has been minimized.
|
@leonardo-m What would that do? Just |
This comment has been minimized.
This comment has been minimized.
|
It's a single operation. |
This comment has been minimized.
This comment has been minimized.
|
Right-- I'm asking how that operation should be implemented generically for iterators. It seems like the most obvious way would be to call |
This comment has been minimized.
This comment has been minimized.
|
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... |
This comment has been minimized.
This comment has been minimized.
|
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()
}
}
} |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
@leonardo-m #43064 is not controversial. I’m working on a PR. |
This comment has been minimized.
This comment has been minimized.
|
#43077 should fix the performance problem. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
I close this down because most API discussions already happened elsewhere, and they are settled. |
leonardo-m commentedJul 5, 2017
•
edited
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