Skip to content

Commit

Permalink
rt: stream: improve TokioAsyncRead
Browse files Browse the repository at this point in the history
avoid unnecessary conversion in tokio runtime

Signed-off-by: rupansh-arch <rupanshsekar@hotmail.com>
  • Loading branch information
rupansh committed Dec 24, 2020
1 parent c6ff49e commit 206bcff
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/runtime/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,18 +281,27 @@ impl AsyncWrite for AsyncTcpStream {

impl TokioAsyncRead for AsyncTcpStream {
fn poll_read(
self: Pin<&mut Self>,
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf,
) -> Poll<tokio::io::Result<()>> {
let s = buf.initialize_unfilled();
let bread = match AsyncRead::poll_read(self, cx, s) {
Poll::Pending => return Poll::Pending,
Poll::Ready(b) => b?,
};

buf.advance(bread);
Poll::Ready(Ok(()))
return match self.deref_mut() {
#[cfg(feature = "tokio-runtime")]
Self::Tokio(ref mut stream) => {
Pin::new(stream).poll_read(cx, buf)
},
#[cfg(feature = "async-std-runtime")]
Self::AsyncStd(ref mut stream) => {
let s = buf.initialize_unfilled();
let bread = match Pin::new(stream).poll_read(cx, buf) {
Poll::Pending => return Poll::Pending,
Poll::Ready(b) => b?,
};

buf.advance(bread);
Poll::Ready(Ok(()))
}
}
}
}

Expand Down

0 comments on commit 206bcff

Please sign in to comment.