Why does generic Axum route not compile with inner async call?

I am building a generic Axum API. My router should be able to work with any of my Handler implementations. However my code generates an error:

use std::marker::PhantomData;
use std::sync::Arc;
use axum::extract::State;
use axum::Router;
use axum::routing::get;
use tokio::sync::Mutex;

trait Handler{
    async fn handle(&mut self);
}

struct GenericRouter<Backend, Handler>{
    _marker: PhantomData<(Backend, Handler)>
}

struct AxumBackend;

impl<H> GenericRouter<AxumBackend, H>
where
    H: Handler + Send + 'static,
{
    async fn route(state: State<Arc<Mutex<H>>>) {
        let mut state = state.lock().await;

        state.handle().await;
    }

    fn build(&self, handler: H) -> Router {
        let shared_state = Arc::new(Mutex::new(handler));

        Router::new()
            .route("/", get(Self::route))
            .with_state(shared_state)
    }
}
error[E0277]: the trait bound `fn(axum::extract::State<Arc<tokio::sync::Mutex<H>>>) -> impl Future<Output = ()> {GenericRouter::<reproducable_example::AxumBackend, H>::route}: axum::handler::Handler<_, _>` is not satisfied
   --> src/reproducable_example.rs:34:29
    |
34  |             .route("/", get(Self::route))
    |                         --- ^^^^^^^^^^^ the trait `axum::handler::Handler<_, _>` is not implemented for fn item `fn(State<Arc<Mutex<H>>>) -> impl Future<Output = ()> {GenericRouter::<AxumBackend, H>::route}`
    |                         |
    |                         required by a bound introduced by this call
    |
    = help: the following other types implement trait `axum::handler::Handler<T, S>`:
              `Layered<L, H, T, S>` implements `axum::handler::Handler<T, S>`
              `MethodRouter<S>` implements `axum::handler::Handler<(), S>`
note: required by a bound in `axum::routing::get`

    |
385 | top_level_handler_fn!(get, GET);
    | ^^^^^^^^^^^^^^^^^^^^^^---^^^^^^
    | |                     |
    | |                     required by a bound in this function
    | required by this bound in `get`
    = note: this error originates in the macro `top_level_handler_fn` (in Nightly builds, run with -Z macro-backtrace for more info)

If I comment state.handle().await; out then it works. So I assume the issue is not about function signatures or trait bounds.

It’s not obvious, because the exact traits implemented for the return value of a async function are implicit, but the problem is the signature of route, specifically the problem is that handle does not guarantee to return a future that is Send and that’s required for axum routes.

Awaiting a non-Send future inside of route makes the future it returns also non-Send. Since it’s not a guarantee that handle is Send, the compiler can’t assume it is and turns the future returned by route into a non-Send one when you await the result of handle().

The solution is to enforce the Sendness of the future returned from a Handler::handle:

use std::future::Future;
trait Handler {
    fn handle(&mut self) -> impl Future<Output = ()> + Send;
}

For the definition, this is the only way to require the Send on the returned value. You can still use async/await syntax for your implementations of the trait:

impl Handler for () {
    async fn handle(&mut self) {}
}

is valid, even for the new trait definition because async fn foo() is just syntax sugar for fn foo() -> impl Future<Output = ()> [+ Send] [+ Sync]. The compiler will check if the Future produced by this asynchronous method does indeed implement Send and error if it doesn’t.

You might want to do the same for route i.e. use fn route(…) -> impl Future<Output = ()> + Send, to get better diagnostics like the following:

error: future cannot be sent between threads safely
  --> src/main.rs:24:46
   |
24 |     fn route(state: State<Arc<Mutex<H>>>) -> impl Future<Output = ()> + Send {
   |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ future created by async block is not `Send`
   |
   = help: within `{async block@src/main.rs:25:10: 29:10}`, the trait `Send` is not implemented for `impl Future<Output = ()>`, which is required by `{async block@src/main.rs:25:10: 29:10}: Send`
note: future is not `Send` as it awaits another future which is not `Send`
  --> src/main.rs:28:13
   |
28 |             state.handle().await;
   |             ^^^^^^^^^^^^^^ await occurs here on type `impl Future<Output = ()>`, which is not `Send`
help: `Send` can be made part of the associated future's guarantees for all implementations of `Handler::handle`
   |
11 -     async fn handle(&mut self);
11 +     fn handle(&mut self) -> impl Future<Output = ()> + Send;
   |

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật