I would like to implement a struct that owns some data and internally stores an iterator over the data. I tried to write the iterator type down explicitly, but it becomes very long an complicated. My real world example involves multiple iterators that are the result of a filter
having a predicate and I did not manage to get that signature right.
So I tried something like this:
struct MyStruct<I>
where I: Iterator
{
data: Vec<u8>,
it: I
}
impl<I: Iterator> MyStruct<I> {
fn new(data: Vec<u8>) -> Self {
MyStruct {
data,
it: data.iter()
}
}
}
fn main() {
let data = vec![1,2,3];
let my = MyStruct::new(data);
}
I expected - or rather hoped - that I
would be inferred automatically, but that does not work. I also tried to make it
a Box<dyn Iterator>
. But then I run into errors that ask me to specify specific casts that require me to specify lifetimes of the items returned by the iterator. I kind of understand why that's needed but I have no idea how to specify the lifetimes.
In case that makes the problem more plausible: MyStruct
will itself become an iterator. It will implement a very specific iteration strategy over the owned Vec<>. To do so I need to store the iterators as internal state.
Could somebody give me a hint how to solve something like that?