Tokio-core no method named `and_then` for TcpStreamNew [SOLVED]


#1

Hi,

I am building an application which will connect to multiple servers. For now it just connect to one.
Sadly, I am getting this error:
> error: no method named and_then found for type tokio_core::net::TcpStreamNew in the current scope

  --> src/main.rs:49:26
   |
49 |     let connect = socket.and_then(|socket| {
   |                          ^^^^^^^^
   |

= note: the method and_then exists but the following trait bounds were not satisfied: tokio_core::net::TcpStreamNew : futures::Future
= help: items from traits can only be used if the trait is in scope; the following trait is implemented but not in scope, perhaps add a use for it:
= help: candidate #1: use futures::future::Future;

This is my code:
use tokio_core::reactor::Core;
use tokio_core::net::TcpStream;
use futures::future::Future;

fn main() {

let mut l = Core::new().unwrap();
let handle = l.handle();

let config = Config::init("config.toml".to_string()).unwrap();

let addr: Vec<SocketAddr> = config.peers.iter().map(|x| x.node_addr.parse().unwrap()).collect();

let local: SocketAddr = "127.0.0.1:9000".parse().unwrap();

// later connect to multiple servers
let socket = TcpStream::connect(&local, &handle);

let connect = socket.and_then(|socket| {
    // TODO something
});

l.run(connect).unwrap();
}

I do not understand this error message because I am already importing futures.


#2

This is a shot in the dark, but can you see if there are perhaps two version of the futures-rs crate in your Cargo.lock file?


#3

No, all crates use futures 0.1.6


#4

Nevermind, it compiles now

Honestly, I do not know what the error exactly solved. I changed the closure in and_then

use futures::future::Future;
use tokio_core::reactor::Core;
use tokio_core::net::TcpStream;

let mut l = Core::new().unwrap();
let handle = l.handle();

let local: SocketAddr = "127.0.0.1:9000".parse().unwrap();

let socket = TcpStream::connect(&local, &handle).and_then(move |socket| Ok(()));

l.run(socket).unwrap();