2017-10-07 34 views
1

我正尝试使用Tokio箱子编写一个简单的TCP客户端。我的代码是非常接近this example减去TLS:当使用TcpConnectionNew时,绑定`():futures :: Future`的特性不被满足

extern crate futures; 
extern crate tokio_core; 
extern crate tokio_io; 

use futures::Future; 
use tokio_core::net::TcpStream; 
use tokio_core::reactor::Core; 
use tokio_io::io; 

fn main() { 
    let mut core = Core::new().unwrap(); 
    let handle = core.handle(); 

    let connection = TcpStream::connect(&"127.0.0.1:8080".parse().unwrap(), &handle); 

    let server = connection.and_then(|stream| { 
     io::write_all(stream, b"hello"); 
    }); 

    core.run(server).unwrap(); 
} 

但是,编译失败,出现错误:

error[E0277]: the trait bound `(): futures::Future` is not satisfied 
    --> src/main.rs:16:29 
    | 
16 |  let server = connection.and_then(|stream| { 
    |        ^^^^^^^^ the trait `futures::Future` is not implemented for `()` 
    | 
    = note: required because of the requirements on the impl of `futures::IntoFuture` for `()` 

error[E0277]: the trait bound `(): futures::Future` is not satisfied 
    --> src/main.rs:20:10 
    | 
20 |  core.run(server).unwrap(); 
    |   ^^^ the trait `futures::Future` is not implemented for `()` 
    | 
    = note: required because of the requirements on the impl of `futures::IntoFuture` for `()` 

我觉得很奇怪,因为根据the documentation应该执行。

我使用

  • 锈1.19.0
  • 期货0.1.16
  • TOKIO内核0.1.10
  • TOKIO-io的0.1.3

什么我错过了吗?

回答

5

审查and_then定义:

fn and_then<F, B>(self, f: F) -> AndThen<Self, B, F> 
where 
    F: FnOnce(Self::Item) -> B, 
    B: IntoFuture<Error = Self::Error>, 
    Self: Sized, 

封闭(F)必须返回某种类型(B),其可以与起始封闭(匹配的错误类型被转换成一个未来(B: IntoFutureError = Self::Error)。

你的你的什么关闭返回? ()。这是为什么?因为您已在行尾添加了分号(;)。去掉它。

相关问题