Trait error when I try to use await in handler #873
-
Hello, I'm relatively new to rust and I'm come across a very strange trait error. Full code: use axum::{
extract::{
ws::{Message, WebSocket, WebSocketUpgrade},
Extension, Query,
},
response::IntoResponse,
routing::get,
Router,
};
use std::sync::{Arc, Mutex};
type WebSockets = Arc<Mutex<Vec<WebSocket>>>;
#[derive(serde_derive::Deserialize)]
struct Parameters {
command: String,
}
#[tokio::main]
async fn main() {
// create vector of shared websockets
let ws_connections: WebSockets = Arc::new(Mutex::new(Vec::new()));
let app = Router::new()
.route("/ws", get(upgrade_ws))
.route("/run", post(handle_run))
.layer(Extension(ws_connections));
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}
async fn upgrade_ws(
ws: WebSocketUpgrade,
Extension(websockets): Extension<WebSockets>,
) -> impl IntoResponse {
ws.on_upgrade(move |socket| handle_socket(socket, websockets))
}
async fn handle_socket(socket: WebSocket, websockets: WebSockets) {
println!("New connection");
if let Ok(mut ws_connections) = websockets.lock() {
ws_connections.push(socket);
}
}
async fn handle_run(query: Query<Parameters>, Extension(websockets): Extension<WebSockets>) {
if let Ok(mut ws_connections) = websockets.lock() {
for ws in ws_connections.iter_mut() {
ws.send(Message::Text(query.command.clone())).await.unwrap();
}
}
} Error: error[E0277]: the trait bound `fn(Query<Parameters>, Extension<Arc<std::sync::Mutex<Vec<WebSocket>>>>) -> impl Future<Output = ()> {handle_run}: Handler<_, _>` is not satisfied
--> src/main.rs:27:28
|
27 | .route("/run", get(handle_run))
| --- ^^^^^^^^^^ the trait `Handler<_, _>` is not implemented for `fn(Query<Parameters>, Extension<Arc<std::sync::Mutex<Vec<WebSocket>>>>) -> impl Future<Output = ()> {handle_run}`
| |
| required by a bound introduced by this call
|
note: required by a bound in `axum::routing::get`
--> /home/advil/.cargo/registry/src/github.com-1ecc6299db9ec823/axum-0.4.8/src/routing/method_routing.rs:394:1
|
394 | top_level_handler_fn!(get, GET);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `axum::routing::get`
= note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info) I know the await probably has a hand in this because if I comment out the async fn handle_run(query: Query<Parameters>, Extension(websockets): Extension<WebSockets>) {
if let Ok(mut ws_connections) = websockets.lock() {
for ws in ws_connections.iter_mut() {
// ws.send(Message::Text(query.command.clone())).await.unwrap();
}
test().await;
}
}
async fn test() {} The error only occurs when I put the call inside the if statement though? Note: the purpose of this code is to send a message to all connections when it receives an http request. I'm aware that disconnected connections aren't being removed from the vector with this code. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
See https://docs.rs/axum/latest/axum/handler/index.html#debugging-handler-type-errors in particular the hint about |
Beta Was this translation helpful? Give feedback.
See https://docs.rs/axum/latest/axum/handler/index.html#debugging-handler-type-errors in particular the hint about
debug_handler
.