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 Send
ness 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;
|