2017-10-07 33 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)。

你的你的什麼關閉返回? ()。這是爲什麼?因爲您已在行尾添加了分號(;)。去掉它。

相關問題