Improve AyncRead impl

This commit is contained in:
asonix 2024-02-22 16:21:31 -06:00
parent de356c1f12
commit 00a08a8bc9

View file

@ -110,25 +110,26 @@ impl Stream for IoStream {
impl AsyncRead for BytesReader { impl AsyncRead for BytesReader {
fn poll_read( fn poll_read(
mut self: Pin<&mut Self>, mut self: Pin<&mut Self>,
_cx: &mut Context<'_>, _: &mut Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>, buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<std::io::Result<()>> { ) -> Poll<std::io::Result<()>> {
while buf.remaining() > 0 { while buf.remaining() > 0 {
if self.index == self.inner[0].len() { if let Some(bytes) = self.inner.front() {
if self.index == bytes.len() {
self.inner.pop_front(); self.inner.pop_front();
self.index = 0; self.index = 0;
continue;
} }
if self.inner.is_empty() { let upper_bound = (self.index + buf.remaining()).min(bytes.len());
break;
}
let upper_bound = (self.index + buf.remaining()).min(self.inner[0].len()); let slice = &bytes[self.index..upper_bound];
let slice = &self.inner[0][self.index..upper_bound];
buf.put_slice(slice); buf.put_slice(slice);
self.index += slice.len(); self.index += slice.len();
} else {
break;
}
} }
Poll::Ready(Ok(())) Poll::Ready(Ok(()))