Skip to content

Dependency Injection question #2297

Answered by jplatte
Daniel-Xu asked this question in Q&A
Discussion options

You must be logged in to vote

For the return type, the compiler can infer the concrete type based on the function body. For an impl Trait argument, it is simply impossible for the compiler to infer the correct type. You can still have generic handlers, but you have to use a named generic parameter, and specify it when registering the handler:

pub async fn get_films<R>(State(repo): State<R>) -> impl IntoResponse
where
    R: FilmRepo + Send + Sync
{
    StatusCode::OK
}

Router::new()
    .route("/", get_films::<FilmRepoImpl>)

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Daniel-Xu
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants