The trait bound `(): futures::Future` is not satisfied when using TcpConnectionNew

后端 未结 2 2071
借酒劲吻你
借酒劲吻你 2021-01-04 18:20

I am trying to write a simple TCP client in Rust using Tokio crate. My code is pretty close to this example minus the TLS:

extern crate futures;
extern crate         


        
2条回答
  •  长情又很酷
    2021-01-04 18:48

    TL;DR: remove the semicolon after io::write_all.


    Review the definition of and_then:

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

    The closure (F) has to return some type (B) that can be converted into a future (B: IntoFuture) with an error type that matches the starting closure (Error = Self::Error).

    What does your closure return? (). Why is that? Because you've placed a semicolon (;) at the end of your line. () does not implement the trait IntoFuture, which is indicated by the error message part "on the impl of futures::IntoFuture for ()":

    impl IntoFuture for F {
        type Future = F;
        type Item = F::Item;
        type Error = F::Error;
    }
    

    Removing the semicolon will cause the Future returned by io::write_all to be returned back to and_then and the program will compile.

    In general, futures work by combining together smaller components which are themselves futures. All of this works together to build a single large future which is essentially a state machine. It's good to keep this in mind, as you will almost always need to return a future when using such combinators.

提交回复
热议问题