Skip to content

Commit

Permalink
Add forwarding impls for opposite read/write trait
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton committed Apr 8, 2017
1 parent c728bca commit 39812e6
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ impl<W: Write> Write for EncoderWriter<W> {
}
}

impl<W: Read + Write> Read for EncoderWriter<W> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.get_mut().unwrap().read(buf)
}
}

impl<R: Read> EncoderReader<R> {
/// Creates a new encoder which will read uncompressed data from the given
/// stream and emit the compressed stream.
Expand Down Expand Up @@ -165,6 +171,16 @@ impl<R: Read> Read for EncoderReader<R> {
}
}

impl<W: Read + Write> Write for EncoderReader<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: BufRead> EncoderReaderBuf<R> {
/// Creates a new encoder which will read uncompressed data from the given
/// stream and emit the compressed stream.
Expand Down Expand Up @@ -212,6 +228,16 @@ impl<R: BufRead> Read for EncoderReaderBuf<R> {
}
}

impl<W: BufRead + Write> Write for EncoderReaderBuf<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: Read> DecoderReader<R> {
/// Creates a new decoder which will decompress data read from the given
/// stream.
Expand Down Expand Up @@ -279,6 +305,16 @@ impl<R: Read> Read for DecoderReader<R> {
}
}

impl<W: Read + Write> Write for DecoderReader<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: BufRead> DecoderReaderBuf<R> {
/// Creates a new decoder which will decompress data read from the given
/// stream.
Expand Down Expand Up @@ -347,6 +383,16 @@ impl<R: BufRead> Read for DecoderReaderBuf<R> {
}
}

impl<W: BufRead + Write> Write for DecoderReaderBuf<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<W: Write> DecoderWriter<W> {
/// Creates a new decoder which will write uncompressed data to the stream.
///
Expand Down Expand Up @@ -410,6 +456,12 @@ impl<W: Write> Write for DecoderWriter<W> {
}
}

impl<W: Read + Write> Read for DecoderWriter<W> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.get_mut().unwrap().read(buf)
}
}

#[cfg(test)]
mod tests {
use std::io::prelude::*;
Expand Down
66 changes: 66 additions & 0 deletions src/gz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ impl<W: Write> Write for EncoderWriter<W> {
}
}

impl<R: Read + Write> Read for EncoderWriter<R> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.get_mut().read(buf)
}
}

impl<W: Write> Drop for EncoderWriter<W> {
fn drop(&mut self) {
if self.inner.get_mut().is_some() {
Expand Down Expand Up @@ -352,6 +358,16 @@ impl<R: Read> Read for EncoderReader<R> {
}
}

impl<R: Read + Write> Write for EncoderReader<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: BufRead> EncoderReaderBuf<R> {
/// Creates a new encoder which will use the given compression level.
///
Expand Down Expand Up @@ -423,6 +439,16 @@ impl<R: BufRead> Read for EncoderReaderBuf<R> {
}
}

impl<R: BufRead + Write> Write for EncoderReaderBuf<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: Read> DecoderReader<R> {
/// Creates a new decoder from the given reader, immediately parsing the
/// gzip header.
Expand Down Expand Up @@ -465,6 +491,16 @@ impl<R: Read> Read for DecoderReader<R> {
}
}

impl<R: Read + Write> Write for DecoderReader<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: Read> MultiDecoderReader<R> {
/// Creates a new decoder from the given reader, immediately parsing the
/// gzip header. If the gzip stream contains multiple members all will be
Expand Down Expand Up @@ -508,6 +544,16 @@ impl<R: Read> Read for MultiDecoderReader<R> {
}
}

impl<R: Read + Write> Write for MultiDecoderReader<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: BufRead> DecoderReaderBuf<R> {
/// Creates a new decoder from the given reader, immediately parsing the
/// gzip header.
Expand Down Expand Up @@ -594,6 +640,16 @@ impl<R: BufRead> Read for DecoderReaderBuf<R> {
}
}

impl<R: BufRead + Write> Write for DecoderReaderBuf<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: BufRead> MultiDecoderReaderBuf<R> {
/// Creates a new decoder from the given reader, immediately parsing the
/// gzip header. If the gzip stream contains multiple members all will be
Expand Down Expand Up @@ -700,6 +756,16 @@ impl<R: BufRead> Read for MultiDecoderReaderBuf<R> {
}
}

impl<R: BufRead + Write> Write for MultiDecoderReaderBuf<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl Header {
/// Returns the `filename` field of this gzip stream's header, if present.
pub fn filename(&self) -> Option<&[u8]> {
Expand Down
52 changes: 52 additions & 0 deletions src/zlib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ impl<W: Write> Write for EncoderWriter<W> {
}
}

impl<W: Read + Write> Read for EncoderWriter<W> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.get_mut().unwrap().read(buf)
}
}

impl<R: Read> EncoderReader<R> {
/// Creates a new encoder which will read uncompressed data from the given
/// stream and emit the compressed stream.
Expand Down Expand Up @@ -153,6 +159,16 @@ impl<R: Read> Read for EncoderReader<R> {
}
}

impl<R: Read + Write> Write for EncoderReader<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: BufRead> EncoderReaderBuf<R> {
/// Creates a new encoder which will read uncompressed data from the given
/// stream and emit the compressed stream.
Expand Down Expand Up @@ -200,6 +216,16 @@ impl<R: BufRead> Read for EncoderReaderBuf<R> {
}
}

impl<R: BufRead + Write> Write for EncoderReaderBuf<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: Read> DecoderReader<R> {
/// Creates a new decoder which will decompress data read from the given
/// stream.
Expand Down Expand Up @@ -267,6 +293,16 @@ impl<R: Read> Read for DecoderReader<R> {
}
}

impl<R: Read + Write> Write for DecoderReader<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<R: BufRead> DecoderReaderBuf<R> {
/// Creates a new decoder which will decompress data read from the given
/// stream.
Expand Down Expand Up @@ -327,6 +363,16 @@ impl<R: BufRead> Read for DecoderReaderBuf<R> {
}
}

impl<R: BufRead + Write> Write for DecoderReaderBuf<R> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.get_mut().write(buf)
}

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

impl<W: Write> DecoderWriter<W> {
/// Creates a new decoder which will write uncompressed data to the stream.
///
Expand Down Expand Up @@ -386,6 +432,12 @@ impl<W: Write> Write for DecoderWriter<W> {
}
}

impl<W: Read + Write> Read for DecoderWriter<W> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.inner.get_mut().unwrap().read(buf)
}
}

#[cfg(test)]
mod tests {
use std::io::prelude::*;
Expand Down

0 comments on commit 39812e6

Please sign in to comment.