Move the pattern matching of genericzipwriter

This commit is contained in:
Mathijs van de Nes 2015-03-17 17:06:32 +01:00
parent 11fa463cb4
commit 0339ed2742

View file

@ -71,23 +71,19 @@ impl<W: Write+io::Seek> Write for ZipWriter<W>
{ {
if self.files.len() == 0 { return Err(io::Error::new(io::ErrorKind::Other, "No file has been started", None)) } if self.files.len() == 0 { return Err(io::Error::new(io::ErrorKind::Other, "No file has been started", None)) }
self.stats.update(buf); self.stats.update(buf);
match self.inner match self.inner.ref_mut()
{ {
GenericZipWriter::Storer(ref mut w) => w.write(buf), Some(ref mut w) => w.write(buf),
GenericZipWriter::Deflater(ref mut w) => w.write(buf), None => Err(io::Error::new(io::ErrorKind::BrokenPipe, "ZipWriter was already closed", None)),
GenericZipWriter::Bzip2(ref mut w) => w.write(buf),
GenericZipWriter::Closed => Err(io::Error::new(io::ErrorKind::BrokenPipe, "ZipWriter was already closed", None)),
} }
} }
fn flush(&mut self) -> io::Result<()> fn flush(&mut self) -> io::Result<()>
{ {
match self.inner match self.inner.ref_mut()
{ {
GenericZipWriter::Storer(ref mut w) => w.flush(), Some(ref mut w) => w.flush(),
GenericZipWriter::Deflater(ref mut w) => w.flush(), None => Err(io::Error::new(io::ErrorKind::BrokenPipe, "ZipWriter was already closed", None)),
GenericZipWriter::Bzip2(ref mut w) => w.flush(),
GenericZipWriter::Closed => Err(io::Error::new(io::ErrorKind::BrokenPipe, "ZipWriter was already closed", None)),
} }
} }
} }
@ -254,6 +250,15 @@ impl<W: Write+io::Seek> GenericZipWriter<W>
Ok(()) Ok(())
} }
fn ref_mut(&mut self) -> Option<&mut Write> {
match *self {
GenericZipWriter::Storer(ref mut w) => Some(w as &mut Write),
GenericZipWriter::Deflater(ref mut w) => Some(w as &mut Write),
GenericZipWriter::Bzip2(ref mut w) => Some(w as &mut Write),
GenericZipWriter::Closed => None,
}
}
fn is_closed(&self) -> bool fn is_closed(&self) -> bool
{ {
match *self match *self