actix / actix/actix-web

Cannot infer type for type parameter when defining handler with #[get("/")] and generics

Abierto
#2,866 3 comentarios 7 reacciones 0 asignados Ver en GitHub
A-web-codegen C-improvement good-first-issue
Lenguaje dominante
Rust
Estrellas
24.8k
Forks
1.9k
Merge medio
23 h 10 min
PR fusionados (30 d)
26

Descripción

## Expected Behavior
Pass type checking

## Current Behavior
```console
error[E0282]: type annotations needed
--> examples/cannnot-compile.rs:28:10
|
28 | async fn index(client: web::Data) -> impl Responder {
| ^^^^^ cannot infer type for type parameter `T` declared on the function `index`

Some errors have detailed explanations: E0107, E0282.
For more information about an error, try `rustc --explain E0107`.
error: could not compile `my-project` due to 2 previous errors
```

## Possible Solution
Use `.route("/", web::get().to(index::)` instead of `service(index::)` and `get` method macro, but is there any good way to keep using `get` macro?

## Steps to Reproduce (for bugs)
```rust
use actix_web::{
dev::Server,
get,
web::{self, Data},
App, HttpServer, Responder,
};
use serde::Serialize;

#[derive(Debug, Serialize, Clone, Copy)]
pub struct User {
id: u64,
}

pub trait UserRepository {
fn get_user(&self) -> User;
}

#[derive(Clone)]
struct UserClient;

impl UserRepository for UserClient {
fn get_user(&self) -> User {
User { id: 0 }
}
}

// when uncommenting following the line, the type checking is unaccepted
// because of cannot infer type parameter T
// #[get("/")]
async fn index(client: web::Data) -> impl Responder {
let user = client.into_inner().get_user();
web::Json(user)
}

pub fn create_server(
search: T,
) -> Result {
let server = HttpServer::new(move || {
App::new()
.app_data(Data::new(search.clone()))
.route("/", web::get().to(index::))
// .service(index::)
})
.bind("127.0.0.1:8080")?
.run();
Ok(server)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
let user_client = UserClient;
create_server(user_client).unwrap().await
}

```

## Context

## Your Environment

- Rust Version rustc 1.62.0 (a8314ef7d 2022-06-27)
- Actix Web Version: 4.0.1

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.