Skip to content

Commit

Permalink
Merge pull request rust-lang#29 from joshuawarner32/master
Browse files Browse the repository at this point in the history
expose total_in method, fixes rust-lang#28
  • Loading branch information
alexcrichton committed Oct 31, 2015
2 parents dcb00b6 + 5357b29 commit 3147e02
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ impl<R: Read> DecoderReader<R> {
pub fn into_inner(self) -> R {
self.inner.into_inner()
}

/// Returns the number of bytes that the decompressor has consumed.
///
/// Note that this will likely be smaller than what the decompressor actually
/// read from the underlying stream due to buffering.
pub fn total_in(&self) -> u64 {
self.inner.total_in()
}

/// Returns the number of bytes that the decompressor has produced.
pub fn total_out(&self) -> u64 {
self.inner.total_out()
}
}

impl<R: Read> Read for DecoderReader<R> {
Expand Down Expand Up @@ -233,6 +246,31 @@ mod tests {
assert!(ret == real);
}

#[test]
fn total_in() {
let mut real = Vec::new();
let mut w = EncoderWriter::new(Vec::new(), Default);
let v = thread_rng().gen_iter::<u8>().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
real.extend(to_write.iter().map(|x| *x));
w.write_all(to_write).unwrap();
}
let mut result = w.finish().unwrap();

let result_len = result.len();

for _ in 0..200 {
result.extend(v.iter().map(|x| *x));
}

let mut r = DecoderReader::new(&result[..]);
let mut ret = Vec::new();
r.read_to_end(&mut ret).unwrap();
assert!(ret == real);
assert_eq!(r.total_in(), result_len as u64);
}

#[test]
fn roundtrip2() {
let v = thread_rng()
Expand Down
8 changes: 8 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ impl<R: Read> DecoderReader<R> {
}
self.0.inner.read(buf)
}

pub fn total_in(&self) -> u64 {
self.0.stream.total_in()
}

pub fn total_out(&self) -> u64 {
self.0.stream.total_out()
}
}

impl<R: Read> Read for DecoderReader<R> {
Expand Down
38 changes: 38 additions & 0 deletions src/zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ impl<R: Read> DecoderReader<R> {
pub fn into_inner(self) -> R {
self.inner.into_inner()
}

/// Returns the number of bytes that the decompressor has consumed.
///
/// Note that this will likely be smaller than what the decompressor actually
/// read from the underlying stream due to buffering.
pub fn total_in(&self) -> u64 {
self.inner.total_in()
}

/// Returns the number of bytes that the decompressor has produced.
pub fn total_out(&self) -> u64 {
self.inner.total_out()
}
}

impl<R: Read> Read for DecoderReader<R> {
Expand Down Expand Up @@ -229,6 +242,31 @@ mod tests {
assert!(ret == real);
}

#[test]
fn total_in() {
let mut real = Vec::new();
let mut w = EncoderWriter::new(Vec::new(), Default);
let v = thread_rng().gen_iter::<u8>().take(1024).collect::<Vec<_>>();
for _ in 0..200 {
let to_write = &v[..thread_rng().gen_range(0, v.len())];
real.extend(to_write.iter().map(|x| *x));
w.write_all(to_write).unwrap();
}
let mut result = w.finish().unwrap();

let result_len = result.len();

for _ in 0..200 {
result.extend(v.iter().map(|x| *x));
}

let mut r = DecoderReader::new(&result[..]);
let mut ret = Vec::new();
r.read_to_end(&mut ret).unwrap();
assert!(ret == real);
assert_eq!(r.total_in(), result_len as u64);
}

#[test]
fn roundtrip2() {
let v = thread_rng()
Expand Down

0 comments on commit 3147e02

Please sign in to comment.