Currently, I have one AppState
struct that has two fields:
pub struct AppState {
players: HashMap<String, PlayerValue>,
cars: HashMap<String, CarValue>,
}
I have implemented a multi-threaded server application using tokio that processes requests and responds to them by mutating the fields in AppState
.
For instance:
// Process stop request
let mut app_state = app_state_clone.lock().await; // `app_state_clone` is `Arc<Mutex<AppState>>`
app_state.stop_players().await?;
app_state.stop_cars().await?;
Ok(())
This code compiles and works as expected. However, in this case we first wait on the completion of the app_state.stop_players()
future, and then again wait on the completion of app_state.stop_cars()
.
However, since both methods could point to different parts of the structure and do not mutate the same fields, I thought I could just use try_join!
to wait concurrently on the tasks.
// Process stop request
let mut app_state = app_state_clone.lock().await; // `app_state_clone` is `Arc<Mutex<AppState>>`
let stop_players_f = app_state.stop_players();
let stop_cars_f = app_state.stop_cars();
try_join!(stop_players_f, stop_cars_f);
Ok(())
This will lead to the compile error:
cannot borrow
app_state
as mutable more than once at a time
I have searched for ways to restructure my code to fix this and found the following SO answer:
let mut x = X { a: 1, b: 2 };
let a = &mut x.a;
let b = &mut x.b;
Here the compiler can see that
a
andb
never point to the same data, even though they do point inside of the same structure.
Inspired by this, I thought I can restructure my code as follows:
pub struct AppState {
players_state: PlayersState,
cars_state: CarsState,
}
pub struct PlayersState {
players: HashMap<String, PlayerValue>,
}
pub struct CarsState {
cars: HashMap<String, CarValue>,
}
Code in the server method:
// Process stop request
let players_state = &mut app_state.players_state;
let cars_state = &mut app_state.cars_state;
let stop_players_f = players_state.stop_players();
let stop_cars_f = cars_state.stop_cars();
try_join!(stop_players_f, stop_cars_f);
Ok(())
However, this will just lead to the same error:
cannot borrow
app_state
as mutable more than once at a time
EDIT: Here is the full compile error:
error[E0499]: cannot borrow `app_state` as mutable more than once at a time
--> crates/my-app/src/app.rs:1786:68
|
1785 | ... let players_state = &mut app_state.players_state;
| --------- first mutable borrow occurs here
1786 | ... let cars_state = &mut app_state.cars_state;
| ^^^^^^^^^ second mutable borrow occurs here
...
1791 | ... let stop_players_f = players_state.stop_players();
| --------------------------- first borrow later used here
And here is the implementation of PlayersState
:
impl PlayersState {
pub fn players(&self) -> &HashMap<String, PlayerValue> {
&self.players
}
pub fn players_mut(&mut self) -> &mut HashMap<String, PlayerValue> {
&mut self.players
}
pub async fn stop_players(&self) -> Result<(), StopPlayersError> {
for player in self.players.values() {
match player {
PlayerValue::MyOnePlayer(p) => {
p.stop().await?;
}
}
}
Ok(())
}
}
Note: While mut
in stop_players
is not required, it is necessary in the stop_cars
function.
More insights into this problem would be greatly appreciated, since I do not seem to understand how I can solve this.
EDIT:
The following code represents an actual minimal example that reproduces the error:
use std::collections::HashMap;
use tokio::try_join;
use tokio::sync::Mutex;
use std::sync::Arc;
pub struct App {
state: Arc<Mutex<AppState>>,
}
pub struct AppState {
players_state: PlayersState,
cars_state: CarsState,
}
pub enum PlayerValue {
MyOnePlayer(PlayerInner)
}
pub struct PlayerInner;
impl PlayerInner {
async fn stop(&self) -> Result<(), ()> { Ok(()) }
}
pub struct PlayersState {
players: HashMap<String, PlayerValue>,
}
impl PlayersState {
pub async fn stop_players(&self) -> Result<(), ()> {
for player in self.players.values() {
match player {
PlayerValue::MyOnePlayer(p) => {
p.stop().await?;
}
}
}
Ok(())
}
}
pub struct CarsState {
cars: HashMap<String, ()>,
}
impl CarsState {
async fn stop_cars(&mut self) -> Result<(), ()> { Ok(()) }
}
pub async fn check() -> Result<(), ()> {
// Init on app startup
let state = Arc::new(Mutex::new(AppState {
players_state: PlayersState {
players: vec![].into_iter().collect()
},
cars_state: CarsState {
cars: vec![].into_iter().collect()
},
}));
let app = App {
state
};
// This code will be executed when we process a request
// `app.state` is in the real 'code' a clone, because I have to use it in the request/response loop and UI loop
let mut app_state = app.state.lock().await;
let players_state = &mut app_state.players_state;
let cars_state = &mut app_state.cars_state;
let stop_players_f = players_state.stop_players();
let stop_cars_f = cars_state.stop_cars();
try_join!(stop_players_f, stop_cars_f);
Ok(())
}