I am experimenting rust by implementing a simple game, and quickly ran into a situation where I find it is hard to decide what is a better approach and I think it is related to a broader question about references and lifetimes in Rust, hopefully, someone has better experience on this.
Assuming I have a Game
that represents the game world, and GameObject
is the trait for all item in the game. And a GameState
to keep track of object collisions for every update, Collision will need to hold reference to other GameObject
in order to tell which ones collide, in order to do that I will need to add a lifetime to the Collision
then add lifetimes to GameState
and then add to Game
if I want the Game object to hold the state, Or I can maybe copy the GameState for every update tick.
trait GameObject {
fn draw(&self, renderer: &Renderer);
fn update(&mut self, state: &GameState, delta: f64);
}
struct Collision<'a> {
obj1: &'a Box<dyn GameObject>,
obj2: &'a Box<dyn GameObject>
}
struct GameState<'a> {
// other states...
collisions: Vec<Collision<'a>>,
}
struct Game {
items: Box<dyn GameObject>,
}
impl Game {
fn draw(&self, renderer: &Renderer){
for item in &self.items {
item.draw(render);
}
}
fn update(&mut self, delta: f64){
// create a state everytime? or hold a reference to it?
for item in &self.items {
item.update(delta);
}
}
}
So that's where I am 100% sure what would be the best approach, or in general, once we give a lifetime to an object, then any other object holds this object will need lifetime types, not maybe we should reorganize the code structure to avoid lifetimes? Thanks.