1

According to Actix-Web Extractors I can use extract() on the HttpRequest to convert the JSON-encoded body to a struct.

However Json::<LoginData>::extract(&req); returns a Box<Future<...>> which I cannot call and_then on.

How can I convert the Box<Future<...>> to just Future<...>?

Complete file:

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();
}

Error:

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 = "*"
599644
  • 561
  • 2
  • 15
  • 1
    Please provide a [MCVE] of the problem, including which definitions were imported into the code. It is also worth explaining what happens when you follow exactly what was proposed in that error message. – E_net4 Jun 12 '18 at 17:09
  • Same error. `use futures::future::Future;` is already there. I am stupid when it comes to Rust, but not *that* stupid to not be able to follow compiler suggestions :D – 599644 Jun 12 '18 at 17:19
  • 1
    It might be worth also including the installed dependencies' versions. – E_net4 Jun 12 '18 at 17:21
  • 1
    Try setting `futures = "0.1"`. – Jorge Israel Peña Jun 12 '18 at 17:27
  • I believe your question is answered by the answers of [Why is a trait not implemented for a type that clearly has it implemented?](https://stackoverflow.com/q/44437123/155423). You have both futures 0.1 and 0.2 present. If you disagree, please [edit] your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jun 12 '18 at 17:28
  • Where do I have "futures 0.1"? – 599644 Jun 12 '18 at 17:30
  • As shown in the duplicate question, actix-web pulls in actix which pulls in futures 0.1. – Shepmaster Jun 12 '18 at 17:34
  • I see. Yeah looks like the same issue. – 599644 Jun 12 '18 at 17:39

0 Answers0