No poll_read() in tokio ReadHalf?

I struggle to understand why I can't call AsyncRead::poll_read() on a ReadHalf in tokio. Clearly AsyncRead is implemented by ReadHalf. AsyncRead in turn requires the poll_read() method. Still it does not seem to be visible. I guess it's somehow related to visibility exports in tokio, but I couldn't find a lead.

Here's an example (I'm using tokio 0.2.9)

use tokio::io::AsyncRead;
use tokio::io::Result;
use tokio::net::TcpListener;
use tokio::stream::StreamExt;

#[tokio::main]
async fn main() -> Result<()>{
    let mut listener = TcpListener::bind("localhost:9187").await?;

    let server = async move {
        let mut incoming = listener.incoming();
        while let Some(connection) = incoming.next().await {
            if let Ok(mut tcp_stream) = connection {
                tokio::spawn(async move {
                    let (reader, _writer) = tcp_stream.split();
                    let pn = reader.poll_read();
                });
            }
        }
    };

    Ok(server.await)
}

The error message I get is:

error[E0599]: no method named `poll_read` found for type `tokio::net::tcp::split::ReadHalf<'_>` in the current scope
  --> src/main.rs:16:37
   |
16 |                     let pn = reader.poll_read();
   |                                     ^^^^^^^^^ method not found in `tokio::net::tcp::split::ReadHalf<'_>`

Why can't the AsyncRead::poll_read() method be found?

Look at the signature of poll_read, it takes Pin<&mut Self>, and you don't have one.

I don't think you want to call that, though. Look at AsyncReadExt, I assume you want to import that and use those methods. I might be wrong, of course :slight_smile:

Well you're right, I'm blind :man_facepalming: I spent hours baffling at this!

I actually don't use it (using AsyncReadExt as you suggested). I just tried something out, stumbled across this and got curious.

It's clear now, thank you!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.