Axum: Generic function as handler
⚓ Rust 📅 2026-03-04 👤 surdeus 👁️ 3I am trying to use something like the following to create routes for types that implement a trait:
trait HasPayload {
type Payload: DeserializeOwned;
}
async fn handler_doesnt_work<T: HasPayload>(Json(payload): Json<T::Payload>) -> impl IntoResponse {
/* ... */
}
async fn handler_works<T: HasPayload>() -> impl IntoResponse {
/* ... */
}
fn router<T: HasPayload>() -> Router {
Router::new()
.route("/works", get(handler_works::<T>))
.route("/doesnt_work", post(handler_doesnt_work::<T>))
}
However handler_doesnt_work doesn't satisfy the trait bounds. Why is this, and how can I fix or work around it? #[axum::debug_handler] says it doesn't support generic functions
2 posts - 2 participants
🏷️ Rust_feed