I am working on building a small game in Rust and have run into a problem I'm not sure how to solve. Essentially I would like to have a Controller trait which has an update method. The update method should accept a mutable entity reference and have a mutable reference to itself. The problem is that I want the entity to own its controller, which results in me needing to do a double mutable borrow. Here is a Rust Playground link which demonstrates what I am trying to do https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f33f548bb72373f47757092fbb0b615d
trait Controller{
fn update(&mut self, entity: &mut Entity);
}
struct MyController{
}
impl Controller for MyController{
fn update(&mut self, _entity: &mut Entity){
}
}
struct Entity{
controller: Option<Box<dyn Controller>>
}
impl Entity{
fn update(&mut self){
if let Some(controller) = self.controller.as_mut(){
controller.update(self);
}
}
}
fn main() {
let _entity = Entity{
controller: Some(Box::new(MyController{}))
};
}