I want to map a collection and return a mutable reference like this.
pub type IterMut<'s> = std::slice::Iter<'s, (EntityId, Vec<&'s mut dyn Component>)>;
pub fn iter_mut(&mut self) -> IterMut<'_>
{
let components: Vec<(EntityId, Vec<&mut dyn Component>)> = self.entity_indexes
.iter()
.filter_map(|(entity_id, _)| match self.get_mut(*entity_id) {
Some(components) => Some((*entity_id, components)),
None => None,
})
.collect();
components.iter()
}
The signature of the get_mut function is that :
pub fn get_mut(&mut self, entity_id: EntityId) -> Option<Vec<&mut dyn Component>>
I don't really understand why, but the compiler sends me an error
cannot infer an appropriate lifetime for autoref due to conflicting requirements
note: expected `std::slice::Iter<'_, (entity::EntityId, std::vec::Vec<&mut (dyn component::Component + 'static)>)>`
found `std::slice::Iter<'_, (entity::EntityId, std::vec::Vec<&mut (dyn component::Component + 'static)>)>`
I guess it is related to mutability cause the immutable version of this function don't cause any problem.