Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot compile handler with both ConnectInfo and Form extractors #2257

Closed
thomas-huegel opened this issue Oct 7, 2023 · 0 comments
Closed

Comments

@thomas-huegel
Copy link

Bug Report

Version

axum v0.6.20

Platform

Linux archlinux 6.4.8-arch1-Yagakimi-T2-1-t2 #1 SMP PREEMPT_DYNAMIC Fri, 04 Aug 2023 14:49:02 +0000 x86_64 GNU/Linux

Description

I cannot compile with both ConnectInfo and Form extractors, whereas they compile separately.

use axum::{extract::ConnectInfo, routing::post, Form, Router};

use std::net::SocketAddr;

#[derive(serde::Deserialize)]
pub struct LoginForm {
    username: String,
}

async fn truc() {
    let app = Router::new().route("/", post(handler));

    async fn handler(
        Form(_form): Form<LoginForm>,
        ConnectInfo(_addr): ConnectInfo<SocketAddr>,
    ) -> String {
        format!("Hello")
    }

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service_with_connect_info::<SocketAddr>())
        .await
        .expect("server failed");
}

If I remove either _form or _addr from the arguments of handler, it compiles.
Otherwise I get the following error message :

error[E0277]: the trait bound `fn(axum::Form<pb_with_connect_info::LoginForm>, ConnectInfo<std::net::SocketAddr>) -> impl std::future::Future<Output = std::string::String> {handler}: axum::handler::Handler<_, _, _>` is not satisfied
   --> src/pb_with_connect_info.rs:13:45
    |
13  |     let app = Router::new().route("/", post(handler));
    |                                        ---- ^^^^^^^ the trait `axum::handler::Handler<_, _, _>` is not implemented for fn item `fn(axum::Form<pb_with_connect_info::LoginForm>, ConnectInfo<std::net::SocketAddr>) -> impl std::future::Future<Output = std::string::String> {handler}`
    |                                        |
    |                                        required by a bound introduced by this call
    |
    = help: the following other types implement trait `axum::handler::Handler<T, S, B>`:
              <axum_extra::handler::or::Or<L, R, Lt, Rt, S, B> as axum::handler::Handler<(M, Lt, Rt), S, B>>
              <axum_extra::handler::IntoHandler<H, T, S, B> as axum::handler::Handler<T, S, B>>
              <axum::handler::Layered<L, H, T, S, B, B2> as axum::handler::Handler<T, S, B2>>
              <MethodRouter<S, B> as axum::handler::Handler<(), S, B>>
note: required by a bound in `axum::routing::post`
   --> /home/thomas/.cargo/registry/src/index.crates.io-6f17d22bba15001f/axum-0.6.20/src/routing/method_routing.rs:407:1
    |
407 | top_level_handler_fn!(post, POST);
    | ^^^^^^^^^^^^^^^^^^^^^^----^^^^^^^
    | |                     |
    | |                     required by a bound in this function
    | required by this bound in `post`
    = note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info)

Thanks for your help!

@tokio-rs tokio-rs locked and limited conversation to collaborators Oct 7, 2023
@davidpdrsn davidpdrsn converted this issue into discussion #2259 Oct 7, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant