In axum I'm getting an error about Generics, please help!

Hello folks, good morning to all.

I'm trying to learn axum and stucked in a place. This is working example: axum/examples/dependency-injection/src/main.rs at v0.7.x · tokio-rs/axum · GitHub

This is my repo: GitHub - kodmanyagha/axum-examples

I'm getting error exactly in here: axum-examples/src/http/routes/generic_routes.rs at ae84b4cc1d18dfb52e0707f95e26c12660c66c57 · kodmanyagha/axum-examples · GitHub

Error message:

the trait bound `fn(State<generic_state::AppStateGeneric<dyn_state::InMemoryUserRepo>>, axum::extract::Path<u64>) -> Result<Json<dyn_state::User>, StatusCode> {generic_handlers::get_user_generic::<dyn_state::InMemoryUserRepo>}: Handler<_, _>` is not satisfied
the following other types implement trait `Handler<T, S>`:
  <axum::handler::Layered<L, H, T, S> as Handler<T, S>>
  <MethodRouter<S> as Handler<(), S>>

I'm kindly requesting your suggestions from expert axum/rust developers. Thanks a lot.

There is a class of errors in axum that can be difficult figure out because of all the macro foo going on behind the scenes. I cant look at your code in detail right now, but try using this on the route thats causing the problem, it should give you a cleaner error: debug_handler in axum_macros - Rust

1 Like

Probably I must read the source code of axum macros. Do you suggest this?

No necessary, that macro I sent you is meant to make those errors clearer, just slap it on top of your route handler and it will tell you the actual reason why things aren't working.

1 Like

I used this macro and found the problem, handle functions has to be async but my functions not async, this is the mistake :man_facepalming: Thanks for help @jonnyso

But in generic handlers I'm getting this error: ``#[axum_macros::debug_handler] doesn't support generic functions Probably I can't use debug_handler trait on generic functions. What do you suggest?

Edit: I fixed the bug in the project, thanks again @jonnyso : generic handler fix · kodmanyagha/axum-examples@d43ea2e · GitHub