I have a point struct, like so:
#[derive(Clone, Copy, Debug)]
struct Point {
x: i32,
y: i32,
}
I would like to iterate between points like so:
let start = Point { x: 0, y: 0 };
let end = Point { x: 3, y: 3 };
for p in start..end {
// p should be
// (0, 0)
// (1, 0)
// (2, 0)
// (0, 1)
// (1, 1)
// (2, 1)
// (0, 2)
// etc
}
However, I cannot seem to implement this. start..end produces a Range<Pos>, which is a type in the standard library. So I cannot make a custom impl Iterator for Range<Pos>, as none of those types are in the current crate.
I also cannot impl Step for Point, as the iterator must know the start and end of the range. Step does not give you this information.
Lastly, I cannot write a wrapper for Iterator, as impl MyIterator for Range<Point> will fail with an error saying that Step is not implemented for Point.
Is there any way to solve this? My current solution is to define a to function on a Point, so that you can write this:
for p in start.to(end) {
// works as expected
}
The to function produces a custom iterator which does what I'm looking for. However, I still would like to use the .. syntax.
EDIT: AFAIK, this is what is needed for this to work. Based on these comments, this looks impossible.