[英]Rust actix-web: the trait `Handler<_, _>` is not implemented
I've moved from using actix-web
3.xx to 4.xx and the code that's been running perfectly fine before is now throwing this error:我已经从使用
actix-web
3.xx 转移到 4.xx 并且之前运行良好的代码现在抛出了这个错误:
the trait bound `fn(actix_web::web::Query<TweetParams>, actix_web::web::Data<Pool<Postgres>>) -> impl std::future::Future {tweets4}: Handler<_, _>` is not satisfied
--> src/routes/all_routes.rs:74:14
|
74 | pub async fn tweets4(
| ^^^^^^^ the trait `Handler<_, _>` is not implemented for `fn(actix_web::web::Query<TweetParams>, actix_web::web::Data<Pool<Postgres>>) -> impl std::future::Future {tweets4}`
After some googling it seems there is indeed a Handler
trait in the actix
ecosystem (however, not actix-web I think).经过一番谷歌搜索后,似乎在
actix
生态系统中确实存在Handler
特征(但是,我认为不是 actix-web)。
I can't figure out where I need to implement the trait.我不知道我需要在哪里实现这个特征。 The error message seems to suggest that it's missing on the function itself, but my understanding is that you can only implement traits on
structs
and enums
, not functions?该错误消息似乎表明 function 本身缺少它,但我的理解是您只能在
structs
和enums
上实现特征,而不是函数?
Here's the handler code:这是处理程序代码:
#[get("/tweets4")]
pub async fn tweets4(
form: web::Query<TweetParams>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, HttpResponse> {
let fake_json_data = r#"
{ "name": "hi" }
"#;
let v: Value = serde_json::from_str(fake_json_data)
.map_err(|_| HttpResponse::InternalServerError().finish())?;
sqlx::query!(
r#"
INSERT INTO users
(id, created_at, twitter_user_id, twitter_name, twitter_handle, profile_image, profile_url, entire_user)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
"#,
Uuid::new_v4(),
Utc::now(),
"3",
"4",
"5",
"6",
"7",
v,
)
.execute(pool.as_ref())
.await
.map_err(|e| {
println!("error is {}", e);
HttpResponse::InternalServerError().finish()
})?;
Ok(HttpResponse::Ok().finish())
}
What am I getting wrong?我怎么了?
If helpful, the entire project is on github here .如果有帮助,整个项目都在github上。
After sufficient trial and error I discovered that:经过充分的反复试验,我发现:
HttpResponse
error type and I had to replace with my own:HttpResponse
错误类型,我不得不用自己的替换:#[derive(Debug)]
pub struct MyError(String); // <-- needs debug and display
impl std::fmt::Display for MyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "A validation error occured on the input.")
}
}
impl ResponseError for MyError {} // <-- key
#[get("/tweets4")]
pub async fn tweets4(
form: web::Query<TweetParams>,
pool: web::Data<PgPool>,
) -> Result<HttpResponse, MyError> {
let fake_json_data = r#"
{ "name": "hi" }
"#;
let v: Value = serde_json::from_str(fake_json_data).map_err(|e| {
println!("error is {}", e);
MyError(String::from("oh no")) // <-- here
})?;
sqlx::query!(
//query
)
.execute(pool.as_ref())
.await
.map_err(|e| {
println!("error is {}", e);
MyError(String::from("oh no")) // <-- and here
})?;
Ok(HttpResponse::Ok().finish())
}
Hope helps someone in the future!希望将来可以帮助某人!
If you still face the same error, another solution is just to switch to 1.0.8如果仍然遇到同样的错误,另一种解决方案就是切换到 1.0.8
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.