2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 11:21:24 +00:00

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() {
self.inner.pop_front(); if self.index == bytes.len() {
self.index = 0; self.inner.pop_front();
} self.index = 0;
continue;
}
if self.inner.is_empty() { let upper_bound = (self.index + buf.remaining()).min(bytes.len());
let slice = &bytes[self.index..upper_bound];
buf.put_slice(slice);
self.index += slice.len();
} else {
break; break;
} }
let upper_bound = (self.index + buf.remaining()).min(self.inner[0].len());
let slice = &self.inner[0][self.index..upper_bound];
buf.put_slice(slice);
self.index += slice.len();
} }
Poll::Ready(Ok(())) Poll::Ready(Ok(()))