我正在尝试使用 actix-web 1.0 编写一个 HTTP 端点。我已经减少了函数,使它只返回传递给它的用户,但编译器仍然给出错误。
extern crate actix_web;
extern crate chrono;
extern crate futures;
extern crate listenfd;
#[macro_use]
extern crate serde_derive;
extern crate dotenv;
use actix_web::{error, web, App, Error, HttpResponse, HttpServer};
use futures::future::Future;
#[derive(Debug, Deserialize, Serialize)]
pub struct LoginUser {
pub username: String,
pub password: String,
}
pub fn login(
login_user: web::Json<LoginUser>,
) -> impl Future<Item = HttpResponse, Error = error::BlockingError<Error>> {
web::block(move || {
let login_user = login_user.into_inner();
let user = LoginUser {
username: login_user.username,
password: login_user.password,
};
Ok(HttpResponse::Ok().json(user))
})
}
pub fn router(cfg: &mut web::ServiceConfig) {
cfg.service(web::scope("/").service(web::resource("").route(web::get().to(login))));
}
fn main() -> std::io::Result<()> {
HttpServer::new(move || App::new().configure(router))
.bind("127.0.0.1:3000")?
.run()
}
这是我的 cargo.toml。
[package]
name = "log"
version = "0.1.0"
authors = ["me@example.com"
edition = "2018"
[dependencies]
actix-files = "~0.1"
actix-web = "~1.0"
chrono = { version = "0.4.6", features = ["serde"] }
listenfd = "0.3"
diesel = {version = "1.4.1", features = ["postgres", "uuid", "r2d2", "chrono"]}
dotenv = "0.13"
failure = "0.1"
futures = "0.1"
scrypt = "0.2.0"
serde_derive="1.0"
serde_json="1.0"
serde="1.0"
我收到编译错误
| web::block(move || {
| ^^^^^^^^^^ `(dyn std::any::Any + 'static)` cannot be sent between threads safely
我认为这与login_user
in 中的使用有关web::block
,但很难从错误中分辨出来。在 Rust 或 actix 中安全地异步使用请求参数的首选方法是什么?