the trait for<'a> tower_service::Service<IncomingStream<'a>>
is not implemented for Router<S>
#2564
-
SummaryI can't get Here is a minimal example: use std::sync::Arc;
use axum::{extract::State, routing::get, Router};
#[derive(Clone)]
struct Struct { }
async fn handler(State(_s): State<Arc<Struct>>) {}
#[tokio::main]
async fn main() {
let state = Arc::new(Struct { });
let app: Router<Arc<Struct>> = Router::new().route("/", get(handler)).with_state(state.clone());
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
let serve_future = axum::serve(listener, app);
} The error that I get is that
If I try axum version0.7.4 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
You annotated the You should be able to fix the problem by removing the type annotation, as in let app = Router::new().route("/", get(handler)).with_state(state.clone()); |
Beta Was this translation helpful? Give feedback.
You annotated the
app
binding with a type ofRouter<Arc<Struct>>
. The (first) type parameter is for the state that needs to be supplied for the routes,.with_state
changes that type parameter to any other state type that any routes added later might need. Before the router can be passed toserve
, it must be transformed to aRouter<()>
.You should be able to fix the problem by removing the type annotation, as in