Generic Async Closures In Router #3582
Answered
by
jplatte
Shadowcat650
asked this question in
Q&A
-
SummaryHow do I use generics in router closures? Concrete types like Minimal example (does not compile): #[extension_trait::extension_trait]
impl RouterExt for axum::Router {
fn register<T: serde::Serialize + serde::de::DeserializeOwned + 'static>(self) -> Self {
self.route("/test", axum::routing::post(async |_r: axum::Json<T>| { }))
}
}It produces this error: I can't figure out why the Handler trait bound is not satisfied with the closure. #[extension_trait::extension_trait]
impl RouterExt for axum::Router {
fn register_request<Svr, Req, Fut, Hdlr>(self, server: Arc<Svr>, handler: Hdlr) -> Self
where
Req: Request,
Fut: Future<Output = Response<Req::ResponseData>> + Send,
Hdlr: Fn(&Svr, Req) -> Fut + Clone + Send + 'static,
{
let router = |axum::Json(req): axum::Json<Req>| async {
let resp = handler(&server, req).await;
axum::Json(resp)
};
self.route(Req::NAME, axum::routing::post(router))
}
}I would like to avoid manually creating a structure and implementing Handler for it if possible. axum version0.8.7 |
Beta Was this translation helpful? Give feedback.
Answered by
jplatte
Dec 25, 2025
Replies: 1 comment 1 reply
-
|
Try requiring |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
Shadowcat650
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Try requiring
T: Send/Req: Sendas well.