如何在盒装的Future上调用`and_then`?

时间:2018-06-12 17:02:38

标签: rust

根据Actix-Web Extractors,我可以在extract()上使用HttpRequest将JSON编码的正文转换为结构。

但是Json::<LoginData>::extract(&req);会返回Box<Future<...>>,我无法呼叫and_then

如何将Box<Future<...>>转换为Future<...>

完整档案:

extern crate actix_web;
extern crate serde;
extern crate serde_json;
extern crate futures;
#[macro_use] extern crate serde_derive;
extern crate json;

use actix_web::http::{Method};
use actix_web::{server, App, HttpRequest, HttpResponse, Error, Json, FromRequest};

use futures::future::Future;

#[derive(Debug, Deserialize)]
struct LoginData {
    email: String,
    password: String,
}

fn login_handler(item: Json<LoginData>) -> Result<&'static str, Error> {
    Ok("hmmm")
}

fn login_handler2(req: HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
    let info = Json::<LoginData>::extract(&req);
    info.and_then(|f| { f })
}

fn main() {
    server::new(
        || App::new()
            .route("/login", Method::POST, login_handler))
        .bind("127.0.0.1:8080").unwrap()
        .run();
}

错误:

Compiling hello_world v0.1.0 (file:///Users/stavros/projects/test-rust/hello_world_so/hello_world)
error: the `and_then` method cannot be invoked on a trait object
  --> src/main.rs:25:10
   |
25 |     info.and_then(|f| { f })
   |          ^^^^^^^^
help: another candidate was found in the following trait, perhaps add a `use` for it:
   |
8  | use futures::future::Future;
   |

error: aborting due to previous error

error: Could not compile `hello_world`.

My Cargo.toml:

[package]
name = "hello_world"
version = "0.1.0"
authors = ["docker"]

[dependencies]
actix-web = "0.6.10"
env_logger = "0.5.10"
futures = "0.2.1"
mysql = "13.1.0"
serde = "1.0.66"
serde_derive = "1.0.66"
serde_json = "1.0.20"
tokio = "0.1"
json = "*"

0 个答案:

没有答案