Hi, I'm struggling to wrap my head around building an iterator for a dyn trait and kind of failing. The code is is supposed to iterate over two vectors and return a tuple of the element of both for each index. The Item:: is what the iterator is supposed to return, it's set by an api so I can't change it.
It looks to me like it finally does return the right type (which was what I was struggling with for a while, but now I get this
cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
note: ...so that reference does not outlive borrowed content
note: expectedstd::iter::Iterator
foundstd::iter::Iterator
...on self.entities in this code:
let ent = self.entities.get_mut(self.count);
Any idea where I'm going wrong? Is it solvable or am I just doing this the wrong way all together? Just started coding rust yesterday after reading the docs so I'm quite new.
struct EntityIterator<'a> {
count: usize,
entity: &'a mut Vec<Box<dyn Entity + 'a>>,
data: &'a Vec<Data>
}
impl<'a> EntityIterator<'a> {
pub fn new(entities: &'a mut Vec<Box<dyn Entity + 'a>>, data: &'a Vec<Data>) -> Self {
Self {
count: 0,
entities: entities,
data: data,
}
}
}
impl<'a> Iterator for EntityIterator<'a> {
type Item = (Data, &'a mut dyn Entity);
fn next(&mut self) -> Option<Self::Item> {
let ent = self.entities.get_mut(self.count);
let data = self.data.get(self.count);
match data {
Some(x) => {
match ent {
Some(y) =>
{
let i = *x;
Some((i, &mut**y))
}
None => panic!("Vectors mismatch!")
}
}
None => return None
}
}
}