I would like to define a struct that implements Iterator
such that the items yielded are references to one of the the struct's fields.
Lets say I have defined my struct like this:
struct InnerType;
struct MyStruct {
field: InnerType
}
The following does not work because the Associated Type Item
requires an explicit lifetime parameter:
impl Iterator for MyStruct {
type Item = &InnerType;
fn next(&mut self) -> Option<Self::Item> { Some(&self.field) }
}
Adding a lifetime parameter there doesn't work either because "the lifetime parameter 'a
is not constrained by the impl trait, self type, or predicates".
impl<'a> Iterator for MyStruct {
type Item = &'a InnerType;
fn next(&mut self) -> Option<Self::Item> { Some(&self.field) }
}
Not sure what I'm missing. What is going on here? Is there some reason(s) not to have an iterator which yields items borrowing from itself?