Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the trait bound `tokio::net::tcp::stream::TcpStream: tokio_io::async_read::AsyncRead` is not satisfied

I can't compile a simple application to test tokio-codec. tokio::net::tcp::stream::TcpStream implements AsyncRead and -Write. But when i try to compile the code below, i get the error below. I am still new to Rust and Tokio, so no doubt im missing something obvious (i hope)...

main.rs:

use tokio::net::TcpListener;
use tokio::prelude::*;
use tokio_codec::{ Framed, LinesCodec };


#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
  let mut listener = TcpListener::bind("127.0.0.1:12321").await?;

  loop {
    let (socket, _addr) = listener.accept().await?;

    tokio::spawn(async move {
      let (_sink, mut stream) = Framed::new(socket, LinesCodec::new()).split();

      while let Some(Ok(line)) = stream.next().await {
        println!("{:?}", line);
      }
    });
  }
}

Cargo.toml:

[dependencies]
tokio = { version = "0.2.6", features = ["full"] }
tokio-codec = "0.1.1"

Output:

error[E0277]: the trait bound `tokio::net::tcp::stream::TcpStream: tokio_io::async_read::AsyncRead` is not satisfied
  --> src\main.rs:14:36
   |
14 |       let (mut sink, mut stream) = Framed::new(socket, LinesCodec::new()).split();
   |                                    ^^^^^^^^^^^ the trait `tokio_io::async_read::AsyncRead` is not implemented for `tokio::net::tcp::stream::TcpStream`
   |
   = note: required by `tokio_io::_tokio_codec::framed::Framed::<T, U>::new`

error[E0277]: the trait bound `tokio::net::tcp::stream::TcpStream: tokio_io::async_write::AsyncWrite` is not satisfied
  --> src\main.rs:14:36
   |
14 |       let (mut sink, mut stream) = Framed::new(socket, LinesCodec::new()).split();
   |                                    ^^^^^^^^^^^ the trait `tokio_io::async_write::AsyncWrite` is not implemented for `tokio::net::tcp::stream::TcpStream`
   |
   = note: required by `tokio_io::_tokio_codec::framed::Framed::<T, U>::new`

error[E0599]: no method named `split` found for type `tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec>` in the current scope
  --> src\main.rs:14:75
   |
14 |       let (mut sink, mut stream) = Framed::new(socket, LinesCodec::new()).split();
   |                                                                           ^^^^^ method not found in `tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec>`
   |
   = note: the method `split` exists but the following trait bounds were not satisfied:
           `&mut tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec> : tokio::io::util::async_buf_read_ext::AsyncBufReadExt`
           `&tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec> : tokio::io::util::async_buf_read_ext::AsyncBufReadExt`
           `tokio_io::_tokio_codec::framed::Framed<tokio::net::tcp::stream::TcpStream, tokio_codec::lines_codec::LinesCodec> : tokio::io::util::async_buf_read_ext::AsyncBufReadExt`

How can i solve this?

like image 890
Henk de Marie Avatar asked Dec 30 '19 14:12

Henk de Marie


1 Answers

tokio-codec is an outdated crate that depends on a pre-async/await version of Tokio (0.1.7)

Codecs seem to have been moved to tokio-util that depends on Tokio 0.2, so you should have more luck with that.

Generally, when the compiler tells you that a type does not implement a trait, but in documentation you see it does, this means you have two different versions of the crate that defines the trait in your project (Tokio 0.1 and 0.2 in this case).

like image 143
justinas Avatar answered Oct 15 '22 22:10

justinas