Here is my code:
fn update(&mut self) -> bool {
let now = std::time::Instant::now();
let delta_time = now - self.last_update;
self.last_update = now;
match self.state {
ClientState::MainMenu(ref mut menu) => {
// TODO: handle menu transition and rendering
true
}
ClientState::Playing(ref mut game) => {
game.update(delta_time);
self.render(game)
}
ClientState::PauseMenu(ref mut menu, ref mut game) => {
// TODO: do not update/render game in SP mode while in the menu (full pause)
game.update(delta_time);
self.render(game)
// TODO: handle menu transition and rendering
}
}
}
fn render(&self, game: &Game) -> bool {
match self.renderer_to_surface.render(game) {
Ok(_) => {}
Err(wgpu::SurfaceError::Lost) => {
self.renderer_to_surface.reconfigure();
}
Err(_) => return false,
}
true
}
For information, game.update
is a mutating method, that’s why in match arms Playing
and PauseMenu
I need to access game
as a mutable reference. But past calling this method, I don’t need the mutable borrow anymore since self.render
is a non-mutating method. However the compiler still complains at self.render(game)
that self
cannot be immutable borrowed because it’s still borrowed mutably since ref mut game
.
But NLL should be able to drop this mutable borrow before starting a new immutable borrow, since the mutable reference is not used anymore in the scope. Do you know why it won’t do this?
Here is an exemple of the code that reproduces this error.
I know other questions are linked to NLL not dropping borrow when we think it should do, but the code case seems different here. There is no conditional lifetime of the mutable reference here. In all case in my function, the mutable reference game
is not used past calling update
or after the match clause.