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

后端 未结 2 2067
借酒劲吻你
借酒劲吻你 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:29

    Unfortunately the answer here is quite specific, but the question turns up for any kind of search for:

    the trait futures::Future is not implemented for ()

    A typical scenario for this kind of error is:

    foo.then(|stream| {
        // ... Do random things here
        final_statement();
    });
    

    This causes an error because the majority of the extension functions require the return type to implement IntoFuture. However, () does not implement IntoFuture, and by terminating the block with a ; the implicit return type is ().

    However, IntoFuture is implemented for Option and Result.

    Rather than just randomly removing semicolons vaguely in the hope this will somehow magically make your code compile, consider:

    You should be returning something that can be converted into a Future using IntoFuture.

    If you don't have a specific promise that you're returning, consider returning Ok(()) to say simply 'this is done' from your callback:

    foo.then(|stream| {
        // ... Do random things here
        final_statement();
        return Ok(()); // <-- Result(()) implements `IntoFuture`.
    });
    

    Note specifically I terminate this block with an explicit return statement; this is deliberate. This is a typical example of how the ergonomics of 'can omit semicolon to implicitly return object' is tangibly harmful; terminating the block with Ok(()); will continue to fail with the same error.

提交回复
热议问题