Hi,
There seems to be a conflict with my websocket handler and the application state type. Here is hopefully enough context:
#[debug_handler]
async fn master_ws_handler(
ws: WebSocketUpgrade,
ConnectInfo(addr): ConnectInfo<SocketAddr>,
State(locked_state): State<SharedState>
) -> impl IntoResponse {
let game = locked_state.clone();
ws.on_upgrade(move |socket| async move { game.handle_master_socket(socket, addr) } )
}
#[derive(Default)]
pub struct AppState {
players: Vec<Player>,
color_used: Vec<bool>,
game_state: GameState,
master_socket: Option<WebSocket>
}
pub type SharedState = Arc<RwLock<AppState>>;
// in main...
let shared_state = SharedState::default();
// in the router:
[...]
.with_state(Arc::clone(&shared_state))
Here is the error I'm getting:
error[E0277]: `(dyn hyper::upgrade::Io + std::marker::Send + 'static)` cannot be shared between threads safely
--> src/main.rs:140:9
|
140 | ws: WebSocketUpgrade,
| ^^^^^^^^^^^^^^^^ `(dyn hyper::upgrade::Io + std::marker::Send + 'static)` cannot be shared between threads safely
|
= help: the trait `Sync` is not implemented for `(dyn hyper::upgrade::Io + std::marker::Send + 'static)`
= help: the trait `FromRequestParts<S>` is implemented for `WebSocketUpgrade`
= note: required for `Unique<(dyn hyper::upgrade::Io + std::marker::Send + 'static)>` to implement `Sync`
= note: required because it appears within the type `Box<dyn Io + Send>`
= note: required because it appears within the type `Rewind<Box<dyn Io + Send>>`
= note: required because it appears within the type `Upgraded`
= note: required because it appears within the type `AllowStd<Upgraded>`
= note: required because it appears within the type `WebSocket<AllowStd<Upgraded>>`
= note: required because it appears within the type `WebSocketStream<Upgraded>`
= note: required because it appears within the type `WebSocket`
= note: required because it appears within the type `Option<WebSocket>`
note: required because it appears within the type `AppState`
--> src/state.rs:24:12
|
24 | pub struct AppState {
| ^^^^^^^^
= note: required for `std::sync::RwLock<AppState>` to implement `Sync`
= note: required for `Arc<std::sync::RwLock<AppState>>` to implement `std::marker::Send`
= note: required for `WebSocketUpgrade` to implement `FromRequestParts<Arc<std::sync::RwLock<AppState>>>`
= help: see issue #48214
I'm not sure why an Option<Websocket>
within the Player type would prevent the Websocket from being used in a handler function.
Anything obvious I'm doing wrong? Thanks for any input! Been stymieing me for a couple weeks now.