I have a Vec<Point>
with a simple struct Point {x: f32, y: f32, z: f32}
. My vector represents hundreds of thousands of lines in 3D (it could be a Vec<Vec<Point>>
in fact), so I keep track of the start/end of all lines.
pub struct Streamlines {
lengths: Vec<usize>,
offsets: Vec<usize>, // cumulative sum of lengths
data: Vec<Point>,
}
I want to create a non-consuming iterator for it, usable like:
for streamline in &streamlines {
for point in &streamline {
println!("{} {} {}", point.x, point.y, point.z);
}
println!("")
}
I found How to implement Iterator and IntoIterator for a simple struct? and started copyi-err, adapting :)
impl IntoIterator for Streamlines {
type Item = &[Point];
type IntoIter = StreamlinesIterator;
fn into_iter(self) -> Self::IntoIter {
StreamlinesIterator {
streamlines: self,
it_idx: 0
}
}
}
struct StreamlinesIterator {
streamlines: &Streamlines,
it_idx: usize
}
impl Iterator for StreamlinesIterator {
type Item = &[Point];
fn next(&mut self) -> Option<&[Point]> {
if self.it_idx < self.streamlines.lengths.len() {
let start = self.streamlines.offsets[self.it_idx];
self.it_idx += 1;
let end = self.streamlines.offsets[self.it_idx];
Some(self.streamlines.data[start..end])
}
else {
None
}
}
}
I used slices because I only want to return parts of the vector, then I added lifetimes because it's required, but now I have this error cannot infer an appropriate lifetime for lifetime parameter in generic type due to conflicting requirements
In fact, I don't actually know what I'm doing with the damn <'a>
.