I'm relatively new to rust, so I'm not completely familiar with the way traits and iterators work. I'm trying to make an entity component system, but I keep getting stuck on this "get_components" method. It's meant to return an iterator for a specific type of component, which a system can then use to update the components.
pub fn get_components<T: Component>(&self) -> Option<IterMut<Option<&mut T>>> {
match self.components.get_mut(&TypeId::of::<T>()) {
Some(components) => Some(components.iter_mut().map(|slot| match slot {
Some(component) => component.downcast_mut::<T>(),
None => None,
})),
None => None,
}
}
The components HashMap looks like this:
components: HashMap<TypeId, Vec<Option<Box<dyn Any>>>>,
The error that I'm getting says:
"mismatched types
expected struct `std::slice::IterMut<'_, Option<&mut T>>`
found struct `Map<std::slice::IterMut<'_, Option<Box<(dyn Any + 'static)>>>, [closure@src\scene.rs:75:64: 75:70]>`"
Any help would really be appreciated!