Async stream split


i have the following scenario. I have a Transport trait that shall implement AsyncRead and AsyncWrite:
pub trait Transport : AsyncRead + AsyncWrite { }

For this trait i have two implementation, one is based on tokio::net::TcpStream and the other on tokio_openssl::SslStream

In the end both implmentation are like the tcp one having a different new function:

pub struct TcpTransport {
    stream: TcpStream,

Now i wanted to implement AsyncRead and AsyncWrite that will just redirect calls to the corresponding stream, e.g.:

impl AsyncRead for TcpTransport {
  fn poll_read(self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>, buf: &mut tokio::io::ReadBuf<'_>) -> std::task::Poll<std::io::Result<()>> {;

But this results in
error[E0599]: no method named 'poll_read' found for struct 'tokio::net::TcpStream' in the current scope

The poll_read method takes a Pin<&mut TcpStream>, so you must wrap the reference in a Pin.


Since TcpStream is Unpin, you do not worry about the implications of using Pin, since it does not do anything for Unpin types.

Thanks. It must be
Pin::new(&mut self.get_mut().stream).poll_read(cx, buf) but you gave me the direction to go

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.