Future trait : error[E0308]: match arms have incompatible types

#1

Hi,

I am running following code:

extern crate futures;
extern crate gcm;
extern crate http;
extern crate reqwest;
#[macro_use]
extern crate serde_derive;
extern crate tokio;

use std::io;
use std::mem;

use futures::{Future, Stream};
use futures::future::{err, ok};
use reqwest::async::{Chunk, Client, Decoder};
use tokio::io::ErrorKind;

#[derive(Deserialize, Debug, Serialize)]
struct Test {
    count: u8,
}

fn not_working() -> impl Future<Item=Test, Error=()> {
    let result = serde_json::to_string(&Test { count: 9 });

    match result {
        Err(e) => err(()),
        Ok(body) => {
            Client::new()
                .post("https://hyper.rs")
                .body(body)
                .send()
                .and_then(|mut res| {
                    println!("{}", res.status());
                    let x = res.json::<Test>();
                    x
                })
                .map_err(|err| ())
                .and_then(|ch| ok(ch))
        }
    }
}

/*fn working() -> impl Future<Item=Test, Error=()> {
   let body = serde_json::to_string(&Test { count: 9 }).unwrap();

    Client::new()
        .post("https://hyper.rs")
        .body(body)
        .send()
        .and_then(|mut res| {
            println!("{}", res.status());
            let x = res.json::<Test>();
            x
        })
        .map_err(|err| ())
        .and_then(|ch| ok(ch))
}*/


fn wrapper() -> impl Future<Item=(), Error=()> {
    not_working().and_then(|res| {
        println!("Got test {:?}", serde_json::to_string(&res));
        ok(())
    }).map_err(|err| ())
}


fn main() {
    tokio::run(wrapper());
}

This is giving me

error[E0308]: match arms have incompatible types
  --> src/main.rs:25:5
   |
25 | /     match result {
26 | |         Err(e) => err(()),
27 | |         Ok(body) => {
28 | |             Client::new()
...  |
39 | |         }
40 | |     }
   | |_____^ expected struct `futures::FutureResult`, found struct `futures::AndThen`
   |
   = note: expected type `futures::FutureResult<_, _>`
              found type `futures::AndThen<futures::MapErr<futures::AndThen<impl futures::Future, impl futures::Future, [closure@src/main.rs:32:27: 36:18]>, [closure@src/main.rs:37:26: 37:34]>, futures::FutureResult<Test, _>, [closure@src/main.rs:38:27: 38:38]>`
note: match arm with an incompatible type
  --> src/main.rs:27:21
   |
27 |           Ok(body) => {
   |  _____________________^
28 | |             Client::new()
29 | |                 .post("https://hyper.rs")
30 | |                 .body(body)
...  |
38 | |                 .and_then(|ch| ok(ch))
39 | |         }
   | |_________^

error: aborting due to 2 previous errors

If I am using working() method in wrapper it is working . Can u pls help understand the reason and solution for not_working() method.

Thanks in advance

0 Likes

#2

It would be good if you could expand on what you don’t understand about the error message.

A Rust value can only ever be of one type, but you are trying to use two different types (futures::FutureResult<...> and futures::AndThen<...>). There’s nothing that is futures-specific here. The same error occurs in plain Rust:

fn main() {
    let v = match true {
        true => "string",
        false => 42,
    };
}
error[E0308]: match arms have incompatible types
 --> src/main.rs:2:13
  |
2 |       let v = match true {
  |  _____________^
3 | |         true => "string",
4 | |         false => 42,
  | |                  -- match arm with an incompatible type
5 | |     };
  | |_____^ expected &str, found integer
  |
  = note: expected type `&str`
             found type `{integer}`

See also:

0 Likes

#3

Thanks shepmaster. Got it . What could be possible ways to solve this situation (if I want to use match) ?

0 Likes

#4

Those are what I linked to in my first post. Was there a specific question about the content of those links that you had?

TL;DR: Either or Box<dyn Future> .

0 Likes

#5

Thanks.Box did solve .

0 Likes