Serve<Router, Router> is not a future #2505
-
SummaryI ran into this a few times, it's easily fixable by wrapping
Same with using
axum version0.7.2 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
It’s because tokio::spawn takes a Future and not an IntoFuture. That trait didn’t exist when tokio went 1.0. |
Beta Was this translation helpful? Give feedback.
-
also, I would like to document another super-complex-compiler-error-taking-hours-to-solve close to this one here: when building routers from another fn, ie, with the following code: #[tokio::main]
async fn main() {
let app = axum::Router::new().nest("/test", other_mod::router());
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
mod other_mod {
use axum::{extract::State, routing::get, Router};
#[derive(Clone)]
struct AppState {}
pub fn router() -> Router {
Router::new()
.with_state(AppState {})
.route("/", get(get_something))
}
async fn get_something(State(_state): State<AppState>) {}
} you get the following error:
And if you get the bad idea of adding the type parameter like that: #[tokio::main]
async fn main() {
let app = axum::Router::new().nest("/test", other_mod::router());
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
mod other_mod {
use axum::{extract::State, routing::get, Router};
#[derive(Clone)]
struct AppState {}
pub fn router() -> Router<AppState> {
Router::new()
.route("/", get(get_something))
.with_state(AppState {})
}
async fn get_something(State(_state): State<AppState>) {}
} you end up with the following error:
This code compile: #[tokio::main]
async fn main() {
let app = axum::Router::new().nest("/test", other_mod::router());
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
mod other_mod {
use axum::{extract::State, routing::get, Router};
#[derive(Clone)]
struct AppState {}
pub fn router() -> Router {
Router::new()
.route("/", get(get_something))
.with_state(AppState {})
}
async fn get_something(State(_state): State<AppState>) {}
} |
Beta Was this translation helpful? Give feedback.
It’s because tokio::spawn takes a Future and not an IntoFuture. That trait didn’t exist when tokio went 1.0.