Tokio::run(fetch()); type mismatch resolving `<impl futures::Future as futures::Future>::Item == ()`

#1

Hi ,

I (rust beginner) was exploring https://github.com/seanmonstar/reqwest/blob/master/examples/async.rs example . Which returns empty future . What I want to do is return future with some value but I am getting

error[E0271]: type mismatch resolving `<impl futures::Future as futures::Future>::Item == ()`
  --> src/main.rs:35:5
   |
35 |     tokio::run(fetch());
   |     ^^^^^^^^^^ expected struct `Test`, found ()
   |
   = note: expected type `Test`
              found type `()`
   = note: required by `tokio::run`

error: aborting due to previous error

Code

extern crate futures;
extern crate gcm;
extern crate http;
extern crate reqwest;
extern crate tokio;

use std::mem;

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

struct Test{
    count:u8,
}

fn fetch() -> impl Future<Item=Test, Error=()> {

    Client::new()
        .get("https://hyper.rs")
        .send()
        .and_then(|mut res| {
            println!("{}", res.status());

            let body = mem::replace(res.body_mut(), Decoder::empty());
            body.concat2()
        })
        .map_err(|err| ())
        .and_then(|ch| Ok(Test{count:1}))

}


fn main() {
    tokio::run(fetch());
}
#2

Tokio expects a future that returns (). So your function return type should be

impl Future<Item=(), Error=()>
1 Like
#3

See also

TL;DR:

let mut runtime = tokio::runtime::Runtime::new().expect("Unable to create a runtime");
let s = runtime.block_on(example());
println!("{:?}", s);