You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to develop a module that I can reuse across many Axum applications where the states type is unknown. My idea is to return a router I can merge in to the main application.
When using standard traits, the example compiles. However, as soon as I introduce an async trait and call await, I encounter a compile error. I tried using the debug_handler macro to help debug, but it gave an error stating that it doesn't support generic handlers.
I experimented with the async_trait trait macro, and it compiled without any issues. So now I'm trying to understand why the code fails without the async_trait macro.
Error
error[E0277]: the trait bound `fn(State<U>, Form<foo::SignUpForm>) -> impl std::future::Future<Output = impl IntoResponse> {foo::sign_up::<S, U>}: Handler<_, _>` is not satisfied
--> crates/identity_management/src/foo.rs:17:44
|
17 | .route("/signup", get("TODO").post(sign_up::<S,U>))
| ---- ^^^^^^^^^^^^^^ the trait `Handler<_, _>` is not implemented for fn item `fn(State<U>, Form<foo::SignUpForm>) -> impl std::future::Future<Output = impl IntoResponse> {foo::sign_up::<S, U>}`
| |
| required by a bound introduced by this call
|
= help: the following other types implement trait `Handler<T, S>`:
<Layered<L, H, T, S> as Handler<T, S>>
<MethodRouter<S> as Handler<(), S>>
note: required by a bound in `MethodRouter::<S>::post`
--> /Users/foo/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.7.5/src/routing/method_routing.rs:590:5
|
590 | chained_handler_fn!(post, POST);
| ^^^^^^^^^^^^^^^^^^^^----^^^^^^^
| | |
| | required by a bound in this associated function
| required by this bound in `MethodRouter::<S>::post`
= note: this error originates in the macro `chained_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0277`.
warning: `identity-management` (lib) generated 3 warnings
error: could not compile `identity-management` (lib) due to 1 previous error; 3 warnings emitted
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Summary
I am trying to develop a module that I can reuse across many Axum applications where the states type is unknown. My idea is to return a router I can merge in to the main application.
Using the dependency-injection example at https://github.com/tokio-rs/axum/blob/main/examples/dependency-injection I've created the below code sample where I parameterize over the state
S
and retrieve a required dependency from the state by applying aFromRef<S>
bound.When using standard traits, the example compiles. However, as soon as I introduce an async trait and call
await
, I encounter a compile error. I tried using thedebug_handler
macro to help debug, but it gave an error stating that it doesn't support generic handlers.I experimented with the
async_trait
trait macro, and it compiled without any issues. So now I'm trying to understand why the code fails without the async_trait macro.Error
Sample code
axum version
0.7.4
Beta Was this translation helpful? Give feedback.
All reactions