Skip to content

Commit

Permalink
feat: Add progress::Write to automatically pass bytes written to a …
Browse files Browse the repository at this point in the history
…progress instance (#279)
  • Loading branch information
Byron committed Jan 1, 2022
1 parent 542c0df commit 0a749a2
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions git-features/src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ pub fn steps() -> Option<Unit> {
}

/// A structure passing every [`read`][std::io::Read::read()] call through to the contained Progress instance using [`inc_by(bytes_read)`][Progress::inc_by()].
pub struct Read<R, P> {
pub struct Read<T, P> {
/// The implementor of [`std::io::Read`] to which progress is added
pub inner: R,
pub inner: T,
/// The progress instance receiving progress information on each invocation of `reader`
pub progress: P,
}

impl<R, P> io::Read for Read<R, P>
impl<T, P> io::Read for Read<T, P>
where
R: io::Read,
T: io::Read,
P: Progress,
{
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
Expand All @@ -54,9 +54,9 @@ where
}
}

impl<R, P> io::BufRead for Read<R, P>
impl<T, P> io::BufRead for Read<T, P>
where
R: io::BufRead,
T: io::BufRead,
P: Progress,
{
fn fill_buf(&mut self) -> io::Result<&[u8]> {
Expand All @@ -67,3 +67,29 @@ where
self.inner.consume(amt)
}
}

/// A structure passing every [`write`][std::io::Write::write()] call through to the contained Progress instance using [`inc_by(bytes_written)`][Progress::inc_by()].
///
/// This is particularly useful if the final size of the bytes to write is known or can be estimated precisely enough.
pub struct Write<T, P> {
/// The implementor of [`std::io::Write`] to which progress is added
pub inner: T,
/// The progress instance receiving progress information on each invocation of `reader`
pub progress: P,
}

impl<T, P> io::Write for Write<T, P>
where
T: io::Write,
P: Progress,
{
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let written = self.inner.write(buf)?;
self.progress.inc_by(written);
Ok(written)
}

fn flush(&mut self) -> io::Result<()> {
self.inner.flush()
}
}

0 comments on commit 0a749a2

Please sign in to comment.