Skip to content

Commit

Permalink
Merge pull request rust-lang#155 from kornelski/master
Browse files Browse the repository at this point in the history
Replace try!() with ?
  • Loading branch information
alexcrichton authored Apr 20, 2018
2 parents cfc895c + ac0e1a6 commit 9eb6555
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/bufreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ impl<R: Read> Read for BufReader<R> {
return self.inner.read(buf);
}
let nread = {
let mut rem = try!(self.fill_buf());
try!(rem.read(buf))
let mut rem = self.fill_buf()?;
rem.read(buf)?
};
self.consume(nread);
Ok(nread)
Expand All @@ -92,7 +92,7 @@ impl<R: Read> BufRead for BufReader<R> {
// If we've reached the end of our internal buffer then we need to fetch
// some more data from the underlying reader.
if self.pos == self.cap {
self.cap = try!(self.inner.read(&mut self.buf));
self.cap = self.inner.read(&mut self.buf)?;
self.pos = 0;
}
Ok(&self.buf[self.pos..self.cap])
Expand Down
2 changes: 1 addition & 1 deletion src/crc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<R> CrcReader<R> {

impl<R: Read> Read for CrcReader<R> {
fn read(&mut self, into: &mut [u8]) -> io::Result<usize> {
let amt = try!(self.inner.read(into));
let amt = self.inner.read(into)?;
self.crc.update(&into[..amt]);
Ok(amt)
}
Expand Down
10 changes: 5 additions & 5 deletions src/deflate/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<W: Write> DeflateEncoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn reset(&mut self, w: W) -> io::Result<W> {
try!(self.inner.finish());
self.inner.finish()?;
self.inner.data.reset();
Ok(self.inner.replace(w))
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<W: Write> DeflateEncoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn finish(mut self) -> io::Result<W> {
try!(self.inner.finish());
self.inner.finish()?;
Ok(self.inner.take_inner())
}

Expand All @@ -135,7 +135,7 @@ impl<W: Write> DeflateEncoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn flush_finish(mut self) -> io::Result<W> {
try!(self.inner.flush());
self.inner.flush()?;
Ok(self.inner.take_inner())
}

Expand Down Expand Up @@ -261,7 +261,7 @@ impl<W: Write> DeflateDecoder<W> {
/// This function will perform I/O to finish the stream, and if that I/O
/// returns an error then that will be returned from this function.
pub fn reset(&mut self, w: W) -> io::Result<W> {
try!(self.inner.finish());
self.inner.finish()?;
self.inner.data = Decompress::new(false);
Ok(self.inner.replace(w))
}
Expand Down Expand Up @@ -301,7 +301,7 @@ impl<W: Write> DeflateDecoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn finish(mut self) -> io::Result<W> {
try!(self.inner.finish());
self.inner.finish()?;
Ok(self.inner.take_inner())
}

Expand Down
26 changes: 13 additions & 13 deletions src/gz/bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ fn bad_header() -> io::Error {

fn read_le_u16<R: Read>(r: &mut R) -> io::Result<u16> {
let mut b = [0; 2];
try!(r.read_exact(&mut b));
r.read_exact(&mut b)?;
Ok((b[0] as u16) | ((b[1] as u16) << 8))
}

fn read_gz_header<R: Read>(r: &mut R) -> io::Result<GzHeader> {
let mut crc_reader = CrcReader::new(r);
let mut header = [0; 10];
try!(crc_reader.read_exact(&mut header));
crc_reader.read_exact(&mut header)?;

let id1 = header[0];
let id2 = header[1];
Expand All @@ -56,9 +56,9 @@ fn read_gz_header<R: Read>(r: &mut R) -> io::Result<GzHeader> {
let os = header[9];

let extra = if flg & FEXTRA != 0 {
let xlen = try!(read_le_u16(&mut crc_reader));
let xlen = read_le_u16(&mut crc_reader)?;
let mut extra = vec![0; xlen as usize];
try!(crc_reader.read_exact(&mut extra));
crc_reader.read_exact(&mut extra)?;
Some(extra)
} else {
None
Expand All @@ -67,7 +67,7 @@ fn read_gz_header<R: Read>(r: &mut R) -> io::Result<GzHeader> {
// wow this is slow
let mut b = Vec::new();
for byte in crc_reader.by_ref().bytes() {
let byte = try!(byte);
let byte = byte?;
if byte == 0 {
break;
}
Expand All @@ -81,7 +81,7 @@ fn read_gz_header<R: Read>(r: &mut R) -> io::Result<GzHeader> {
// wow this is slow
let mut b = Vec::new();
for byte in crc_reader.by_ref().bytes() {
let byte = try!(byte);
let byte = byte?;
if byte == 0 {
break;
}
Expand All @@ -94,7 +94,7 @@ fn read_gz_header<R: Read>(r: &mut R) -> io::Result<GzHeader> {

if flg & FHCRC != 0 {
let calced_crc = crc_reader.crc().sum() as u16;
let stored_crc = try!(read_le_u16(&mut crc_reader));
let stored_crc = read_le_u16(&mut crc_reader)?;
if calced_crc != stored_crc {
return Err(corrupt());
}
Expand Down Expand Up @@ -221,7 +221,7 @@ impl<R: BufRead> Read for GzEncoder<R> {
let tmp = into;
into = &mut tmp[amt..];
}
match try!(self.inner.read(into)) {
match self.inner.read(into)? {
0 => {
self.eof = true;
self.pos = 0;
Expand Down Expand Up @@ -305,7 +305,7 @@ impl<R: BufRead> GzDecoder<R> {
let mut len = 0;

while len < buf.len() {
match try!(self.inner.get_mut().get_mut().read(&mut buf[len..])) {
match self.inner.get_mut().get_mut().read(&mut buf[len..])? {
0 => return Err(corrupt()),
n => len += n,
}
Expand Down Expand Up @@ -361,9 +361,9 @@ impl<R: BufRead> Read for GzDecoder<R> {
if into.is_empty() {
return Ok(0);
}
match try!(self.inner.read(into)) {
match self.inner.read(into)? {
0 => {
try!(self.finish());
self.finish()?;
Ok(0)
}
n => Ok(n),
Expand Down Expand Up @@ -452,7 +452,7 @@ impl<R: BufRead> MultiGzDecoder<R> {
let mut len = 0;

while len < buf.len() {
match try!(self.inner.get_mut().get_mut().read(&mut buf[len..])) {
match self.inner.get_mut().get_mut().read(&mut buf[len..])? {
0 => return Err(corrupt()),
n => len += n,
}
Expand Down Expand Up @@ -519,7 +519,7 @@ impl<R: BufRead> Read for MultiGzDecoder<R> {
let another_error = io::ErrorKind::Other.into();
return Err(mem::replace(e, another_error));
}
match try!(self.inner.read(into)) {
match self.inner.read(into)? {
0 => match self.finish_member() {
Ok(0) => Ok(0),
Ok(_) => self.read(into),
Expand Down
16 changes: 8 additions & 8 deletions src/gz/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ impl<W: Write> GzEncoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn try_finish(&mut self) -> io::Result<()> {
try!(self.write_header());
try!(self.inner.finish());
self.write_header()?;
self.inner.finish()?;

while self.crc_bytes_written < 8 {
let (sum, amt) = (self.crc.sum() as u32, self.crc.amount());
Expand All @@ -107,7 +107,7 @@ impl<W: Write> GzEncoder<W> {
(amt >> 24) as u8,
];
let inner = self.inner.get_mut();
let n = try!(inner.write(&buf[self.crc_bytes_written..]));
let n = inner.write(&buf[self.crc_bytes_written..])?;
self.crc_bytes_written += n;
}
Ok(())
Expand All @@ -127,13 +127,13 @@ impl<W: Write> GzEncoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn finish(mut self) -> io::Result<W> {
try!(self.try_finish());
self.try_finish()?;
Ok(self.inner.take_inner())
}

fn write_header(&mut self) -> io::Result<()> {
while self.header.len() > 0 {
let n = try!(self.inner.get_mut().write(&self.header));
let n = self.inner.get_mut().write(&self.header)?;
self.header.drain(..n);
}
Ok(())
Expand All @@ -143,15 +143,15 @@ impl<W: Write> GzEncoder<W> {
impl<W: Write> Write for GzEncoder<W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
assert_eq!(self.crc_bytes_written, 0);
try!(self.write_header());
let n = try!(self.inner.write(buf));
self.write_header()?;
let n = self.inner.write(buf)?;
self.crc.update(&buf[..n]);
Ok(n)
}

fn flush(&mut self) -> io::Result<()> {
assert_eq!(self.crc_bytes_written, 0);
try!(self.write_header());
self.write_header()?;
self.inner.flush()
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/zio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ where
loop {
let (read, consumed, ret, eof);
{
let input = try!(obj.fill_buf());
let input = obj.fill_buf()?;
eof = input.is_empty();
let before_out = data.total_out();
let before_in = data.total_in();
Expand Down Expand Up @@ -167,10 +167,10 @@ impl<W: Write, D: Ops> Writer<W, D> {

pub fn finish(&mut self) -> io::Result<()> {
loop {
try!(self.dump());
self.dump()?;

let before = self.data.total_out();
try!(self.data.run_vec(&[], &mut self.buf, D::Flush::finish()));
self.data.run_vec(&[], &mut self.buf, D::Flush::finish())?;
if before == self.data.total_out() {
return Ok(());
}
Expand Down Expand Up @@ -206,7 +206,7 @@ impl<W: Write, D: Ops> Writer<W, D> {
// TODO: should manage this buffer not with `drain` but probably more of
// a deque-like strategy.
while self.buf.len() > 0 {
let n = try!(self.obj.as_mut().unwrap().write(&self.buf));
let n = self.obj.as_mut().unwrap().write(&self.buf)?;
if n == 0 {
return Err(io::ErrorKind::WriteZero.into());
}
Expand All @@ -225,7 +225,7 @@ impl<W: Write, D: Ops> Write for Writer<W, D> {
// As a result we execute this in a loop to ensure that we try our
// darndest to write the data.
loop {
try!(self.dump());
self.dump()?;

let before_in = self.data.total_in();
let ret = self.data.run_vec(buf, &mut self.buf, D::Flush::none());
Expand Down Expand Up @@ -256,7 +256,7 @@ impl<W: Write, D: Ops> Write for Writer<W, D> {
// give us a chunk of memory the same size as our own internal buffer,
// at which point we assume it's reached the end.
loop {
try!(self.dump());
self.dump()?;
let before = self.data.total_out();
self.data
.run_vec(&[], &mut self.buf, D::Flush::none())
Expand Down
10 changes: 5 additions & 5 deletions src/zlib/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<W: Write> ZlibEncoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn reset(&mut self, w: W) -> io::Result<W> {
try!(self.inner.finish());
self.inner.finish()?;
self.inner.data.reset();
Ok(self.inner.replace(w))
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<W: Write> ZlibEncoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn finish(mut self) -> io::Result<W> {
try!(self.inner.finish());
self.inner.finish()?;
Ok(self.inner.take_inner())
}

Expand All @@ -135,7 +135,7 @@ impl<W: Write> ZlibEncoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn flush_finish(mut self) -> io::Result<W> {
try!(self.inner.flush());
self.inner.flush()?;
Ok(self.inner.take_inner())
}

Expand Down Expand Up @@ -260,7 +260,7 @@ impl<W: Write> ZlibDecoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn reset(&mut self, w: W) -> io::Result<W> {
try!(self.inner.finish());
self.inner.finish()?;
self.inner.data = Decompress::new(true);
Ok(self.inner.replace(w))
}
Expand Down Expand Up @@ -300,7 +300,7 @@ impl<W: Write> ZlibDecoder<W> {
/// This function will perform I/O to complete this stream, and any I/O
/// errors which occur will be returned from this function.
pub fn finish(mut self) -> io::Result<W> {
try!(self.inner.finish());
self.inner.finish()?;
Ok(self.inner.take_inner())
}

Expand Down
8 changes: 4 additions & 4 deletions tests/gunzip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ fn test_extract_success_multi() {
// Tries to extract path into memory (assuming a .gz file).
fn extract_file(path_compressed: &Path) -> io::Result<Vec<u8>> {
let mut v = Vec::new();
let f = try!(File::open(path_compressed));
try!(GzDecoder::new(f).read_to_end(&mut v));
let f = File::open(path_compressed)?;
GzDecoder::new(f).read_to_end(&mut v)?;
Ok(v)
}

// Tries to extract path into memory (decompressing all members in case
// of a multi member .gz file).
fn extract_file_multi(path_compressed: &Path) -> io::Result<Vec<u8>> {
let mut v = Vec::new();
let f = try!(File::open(path_compressed));
try!(MultiGzDecoder::new(f).read_to_end(&mut v));
let f = File::open(path_compressed)?;
MultiGzDecoder::new(f).read_to_end(&mut v)?;
Ok(v)
}

0 comments on commit 9eb6555

Please sign in to comment.