Skip to content

Issues encountered while using TcpStream.read()  #318

@piperck

Description

@piperck

Hi team.
When I tried using the tokio-uring TcpStream.read method, I found that polling it never yields ready data.
Here is a code example where I attempt to implement the hyper::rt::Read trait using the read method provided by TcpStream

pub struct HyperStream {
    inner: TcpStream,
}

impl hyper::rt::Read for HyperStream {
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        mut buf: ReadBufCursor<'_>,
    ) -> Poll<Result<(), Error>> {
        unsafe {
            println!("call poll read");
            let read_slice = {
                let buffer = buf.as_mut();
                buffer.as_mut_ptr().write_bytes(0, buffer.len());
                std::slice::from_raw_parts_mut(buffer.as_mut_ptr() as *mut u8, buffer.len())
            };

            println!("call self.inner.read before");
            let mut read_future = self.inner.read(read_slice.to_vec());
            println!("leave self.inner.read");
            tokio::pin!(read_future);

            match read_future.poll(cx) {
                Poll::Ready((Ok(bytes_read), _)) => {
                    buf.advance(bytes_read);
                    Poll::Ready(Ok(()))
                }
                Poll::Ready((Err(e), _)) => Poll::Ready(Err(e)),
                Poll::Pending =>
                    {
                        println!("is pending status?");
                        let _ = cx.waker();
                        Poll::Pending
                    },
            }
        }
    }
}

The hyper::rt::Read requires me to return a Poll<Result>. So I can not directly use self.inner.read(read_slice.to_vec()).await
So my idea was to use self.inner.read(read_slice.to_vec()) to get a future, and then manually use poll to get the result.
However, I found that in the example below, using read_future.poll can never yields a result.

Can someone point out where I went wrong? I checked the tcp_eco example and found that directly using TcpStream.read().await can get a result. I don't understand what the difference is here.🤔

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions