如何修复 "actix_web::handler::AsyncFactory<_, _> is not implemented" 错误?
How can I fix "actix_web::handler::AsyncFactory<_, _> is not implemented" error?
这是我的代码。我正在尝试从 Rust subreddit 获取帖子。我会使用 Actix 的内置客户端,但它不适用于 Windows,因此使用 reqwest
代替:
use actix_web::{self, middleware, web, App, HttpRequest, HttpServer};
use futures::future::Future;
use reqwest::{
self,
r#async::{Client},
};
fn get_rust_posts(
_req: HttpRequest,
client: web::Data<Client>,
) -> impl Future<Item = String, Error = reqwest::Error> {
client
.get("http://www.reddit.com/r/rust.json")
.send()
.and_then(|mut resp| resp.text())
.map_err(|err| {
println!("Error in get rust posts: {}", err);
err
})
}
fn main() {
let mut server = HttpServer::new(|| {
App::new()
.data(Client::new())
.wrap(middleware::Logger::default())
.service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
});
server.bind(("0.0.0.0", 8000)).unwrap().run().unwrap();
}
[dependencies]
actix-web = "1.0.8"
futures = "0.1.29"
reqwest = "0.9.21"
这是错误:
error[E0277]: the trait bound `fn(actix_web::request::HttpRequest, actix_web::data::Data<reqwest::async_impl::client::Client>) -> impl futures::future::Future
{get_rust_posts}: actix_web::handler::AsyncFactory<_, _>` is not satisfied
--> src\main.rs:29:72
|
29 | .service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
| ^^^^^^^^ the trait `actix_web::handler::AsyncFactory<_, _>` is not implemented for
`fn(actix_web::request::HttpRequest, actix_web::data::Data<reqwest::async_impl::client::Client>) -> impl futures::future::Future {get_rust_posts}
to_async 需要一个实现特征 AsyncFactory
的处理函数。将 Future
的 Item
更改为 actix_web::HttpResponse
并将 reqwest
请求放入 actix_web::web::block
满足该约束。
我也用缓慢的响应测试了它,尽管 "block" 在 actix_web::web::block
中,它似乎可以处理并发请求,我认为这是因为处理程序本身是异步的。
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use futures::Future;
use reqwest;
use reqwest::Client;
static REDDIT: &str = "http://www.reddit.com/r/rust.json";
fn get_rust_posts(
_req: actix_web::HttpRequest,
client: web::Data<Client>,
) -> impl Future<Item = HttpResponse, Error = actix_web::Error> {
let builder = client.get(REDDIT);
actix_web::web::block(move || builder.send())
.from_err()
.and_then(|mut res| match res.text() {
Ok(body) => HttpResponse::Ok()
.content_type("application/json")
.body(body),
Err(error) => {
println!("get_request error: {}", error);
HttpResponse::InternalServerError()
.content_type("application/json")
.body(format!("{{\"error\": \"Error getting response text.\"}}"))
}
})
}
fn main() {
HttpServer::new(|| {
App::new()
.data(Client::new())
.wrap(middleware::Logger::default())
.service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
})
.bind("127.0.0.1:5000")
.unwrap()
.run()
.unwrap();
}
这是我的代码。我正在尝试从 Rust subreddit 获取帖子。我会使用 Actix 的内置客户端,但它不适用于 Windows,因此使用 reqwest
代替:
use actix_web::{self, middleware, web, App, HttpRequest, HttpServer};
use futures::future::Future;
use reqwest::{
self,
r#async::{Client},
};
fn get_rust_posts(
_req: HttpRequest,
client: web::Data<Client>,
) -> impl Future<Item = String, Error = reqwest::Error> {
client
.get("http://www.reddit.com/r/rust.json")
.send()
.and_then(|mut resp| resp.text())
.map_err(|err| {
println!("Error in get rust posts: {}", err);
err
})
}
fn main() {
let mut server = HttpServer::new(|| {
App::new()
.data(Client::new())
.wrap(middleware::Logger::default())
.service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
});
server.bind(("0.0.0.0", 8000)).unwrap().run().unwrap();
}
[dependencies]
actix-web = "1.0.8"
futures = "0.1.29"
reqwest = "0.9.21"
这是错误:
error[E0277]: the trait bound `fn(actix_web::request::HttpRequest, actix_web::data::Data<reqwest::async_impl::client::Client>) -> impl futures::future::Future
{get_rust_posts}: actix_web::handler::AsyncFactory<_, _>` is not satisfied
--> src\main.rs:29:72
|
29 | .service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
| ^^^^^^^^ the trait `actix_web::handler::AsyncFactory<_, _>` is not implemented for
`fn(actix_web::request::HttpRequest, actix_web::data::Data<reqwest::async_impl::client::Client>) -> impl futures::future::Future {get_rust_posts}
to_async 需要一个实现特征 AsyncFactory
的处理函数。将 Future
的 Item
更改为 actix_web::HttpResponse
并将 reqwest
请求放入 actix_web::web::block
满足该约束。
我也用缓慢的响应测试了它,尽管 "block" 在 actix_web::web::block
中,它似乎可以处理并发请求,我认为这是因为处理程序本身是异步的。
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use futures::Future;
use reqwest;
use reqwest::Client;
static REDDIT: &str = "http://www.reddit.com/r/rust.json";
fn get_rust_posts(
_req: actix_web::HttpRequest,
client: web::Data<Client>,
) -> impl Future<Item = HttpResponse, Error = actix_web::Error> {
let builder = client.get(REDDIT);
actix_web::web::block(move || builder.send())
.from_err()
.and_then(|mut res| match res.text() {
Ok(body) => HttpResponse::Ok()
.content_type("application/json")
.body(body),
Err(error) => {
println!("get_request error: {}", error);
HttpResponse::InternalServerError()
.content_type("application/json")
.body(format!("{{\"error\": \"Error getting response text.\"}}"))
}
})
}
fn main() {
HttpServer::new(|| {
App::new()
.data(Client::new())
.wrap(middleware::Logger::default())
.service(web::resource("/get/rust/posts").route(web::get().to_async(get_rust_posts)))
})
.bind("127.0.0.1:5000")
.unwrap()
.run()
.unwrap();
}