Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add AsyncBufRead implementation for ProgressBarIter #315

Merged
merged 7 commits into from Oct 11, 2021
22 changes: 22 additions & 0 deletions src/iter.rs
@@ -1,4 +1,6 @@
use crate::progress_bar::ProgressBar;
#[cfg(feature = "tokio")]
x0f5c3 marked this conversation as resolved.
Show resolved Hide resolved
use std::borrow::BorrowMut;
use std::convert::TryFrom;
use std::io::{self, IoSliceMut};
use std::iter::FusedIterator;
Expand Down Expand Up @@ -199,6 +201,26 @@ impl<W: tokio::io::AsyncSeek + Unpin> tokio::io::AsyncSeek for ProgressBarIter<W
Pin::new(&mut self.it).poll_complete(cx)
}
}
#[cfg(feature = "tokio")]
x0f5c3 marked this conversation as resolved.
Show resolved Hide resolved
impl<W: tokio::io::AsyncBufRead + Unpin + tokio::io::AsyncRead> tokio::io::AsyncBufRead
for ProgressBarIter<W>
{
fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> {
let pb = self.progress.clone();
Pin::new(self.get_mut().it.borrow_mut())
.poll_fill_buf(cx)
.map(|x| {
x.map(|buf| {
pb.inc(buf.len() as u64);
buf
})
})
x0f5c3 marked this conversation as resolved.
Show resolved Hide resolved
}

fn consume(mut self: Pin<&mut Self>, amt: usize) {
Pin::new(&mut self.it).consume(amt);
}
}

impl<W: io::Write> io::Write for ProgressBarIter<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
Expand Down