I am trying to create an entity struct that allows you to pass a boxed function to it. I'm having an issue though where it doesn't allow me to have to mutable references to the struct.
The problem is that the self.render field could be mutated or even destroyed during the f(self) call, which would leave f as a dangling reference.
One way to solve this is by moving the Lambda out of the Entity before calling it, and then moving it back in afterward:
impl Entity {
pub fn render(&mut self) {
use std::mem::replace;
let temp: Lambda = Box::new(|_| {});
let f = replace(&mut self.render, temp);
f(self);
self.render = f;
}
}
Or you could pass the Lambda some arguments that do not include a way for it to mutate itself. For example, you could pass it each of the other fields as a separate argument. Or you could gather the other fields into a sub-struct.
Thanks for the reply. I think i'm going to go with your second idea (about creating a second struct for the other data). This really helps a lot. Thanks again.