actix / actix/actix-web

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

Offen
#2,866 3 Kommentare 7 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
A-web-codegen C-improvement good-first-issue
Vorherrschende Sprache
Rust
Sterne
24.8k
Forks
1.9k
Ø Merge
23 Std. 10 Min.
Gemergte PRs (30 T.)
26

Beschreibung

## 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

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.