-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
A-codegenproject: actix-web-codegenproject: actix-web-codegenC-improvementCategory: an improvement to existing functionalityCategory: an improvement to existing functionalitygood-first-issueeasy to pick up for newcomerseasy to pick up for newcomers
Description
Expected Behavior
Pass type checking
Current Behavior
error[E0282]: type annotations needed
--> examples/cannnot-compile.rs:28:10
|
28 | async fn index<T: UserRepository>(client: web::Data<T>) -> 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::<T>)
instead of service(index::<T>)
and get
method macro, but is there any good way to keep using get
macro?
Steps to Reproduce (for bugs)
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<T: UserRepository>(client: web::Data<T>) -> impl Responder {
let user = client.into_inner().get_user();
web::Json(user)
}
pub fn create_server<T: UserRepository + Send + Sync + 'static + Clone>(
search: T,
) -> Result<Server, std::io::Error> {
let server = HttpServer::new(move || {
App::new()
.app_data(Data::new(search.clone()))
.route("/", web::get().to(index::<T>))
// .service(index::<T>)
})
.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
tuguzT, guimarqu, BSathvik, hunjixin, fadeevab and 1 more
Metadata
Metadata
Assignees
Labels
A-codegenproject: actix-web-codegenproject: actix-web-codegenC-improvementCategory: an improvement to existing functionalityCategory: an improvement to existing functionalitygood-first-issueeasy to pick up for newcomerseasy to pick up for newcomers