Trying to figure out how to solve borrow problems like these:
fn update(&mut self, component_map: &mut HashMap<ComponentType, HashMap<u32, Box<dyn Component>>>, dt: f32) {
let move_components = component_map.get(&ComponentType::Movement).unwrap();
let transform_components = component_map.get_mut(&ComponentType::Transform).unwrap();
for (id, component) in move_components.iter(){
transform_components.get_mut(id).unwrap().to_transform_mut().unwrap().position += dt*component.to_movement().unwrap().velocity;
}
}
So I want to mutate the transform component for every id based on data in the movement component but rust is not letting me even fetch both as I'm getting both a mutable and immutable reference from the component_map. Rust of course don't know that this isn't a problem since I'm fetching two different component HashMaps even though I'm fetching them from the same HashMap.
This feels like it should be rust 101 how to fix these types of borrow situations, but I'm really struggling to figure out how. I guess I could first loop over all the ids in the movement components, store them in a Vec and them loop over that vec, fetching exactly one movement component at a time, copying the velocity value and then fetching exactly one transform component and mutating that but then I'm following a ton of pointers and doing a lot of unnecessary computation that I should be able to avoid . Any tips, especially of how this type of problem would be solved in idiomatic rust would be helpful!