Merge pull request #53 from JohnTitor/remove-try-macros

Remove `try!` macros
This commit is contained in:
HeroicKatora authored and GitHub committed 2019-08-29 21:40:27 +02:00
commit f6a91065d9
3 files changed
+48 -48

No files matched your search

+6 -6
View File
@@ -242,7 +242,7 @@ impl Entry {
(Type::SSHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { (Type::SSHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
Ok(Signed(i32::from(decoder.read_sshort()?))) Ok(Signed(i32::from(decoder.read_sshort()?)))
}), }),
(Type::LONG, 1) => Ok(Unsigned(try!(self.r(bo).read_u32()))), (Type::LONG, 1) => Ok(Unsigned(self.r(bo).read_u32()?)),
(Type::SLONG, 1) => Ok(Signed(self.r(bo).read_i32()?)), (Type::SLONG, 1) => Ok(Signed(self.r(bo).read_i32()?)),
(Type::LONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| { (Type::LONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
Ok(Unsigned(decoder.read_long()?)) Ok(Unsigned(decoder.read_long()?))
@@ -251,9 +251,9 @@ impl Entry {
Ok(Signed(decoder.read_slong()?)) Ok(Signed(decoder.read_slong()?))
}), }),
(Type::RATIONAL, 1) => { (Type::RATIONAL, 1) => {
try!(decoder.goto_offset(try!(self.r(bo).read_u32()))); decoder.goto_offset(self.r(bo).read_u32()?)?;
let numerator = try!(decoder.read_long()); let numerator = decoder.read_long()?;
let denominator = try!(decoder.read_long()); let denominator = decoder.read_long()?;
Ok(Rational(numerator, denominator)) Ok(Rational(numerator, denominator))
} }
(Type::SRATIONAL, 1) => { (Type::SRATIONAL, 1) => {
@@ -278,8 +278,8 @@ impl Entry {
if n as usize > limits.decoding_buffer_size { if n as usize > limits.decoding_buffer_size {
return Err(TiffError::LimitsExceeded); return Err(TiffError::LimitsExceeded);
} }
try!(decoder.goto_offset(try!(self.r(bo).read_u32()))); decoder.goto_offset(self.r(bo).read_u32()?)?;
let string = try!(decoder.read_string(n as usize)); let string = decoder.read_string(n as usize)?;
Ok(Ascii(string)) Ok(Ascii(string))
} }
_ => Err(TiffError::UnsupportedError( _ => Err(TiffError::UnsupportedError(
+37 -37
View File
@@ -284,7 +284,7 @@ impl<R: Read + Seek> Decoder<R> {
fn read_header(&mut self) -> TiffResult<()> { fn read_header(&mut self) -> TiffResult<()> {
let mut endianess = Vec::with_capacity(2); let mut endianess = Vec::with_capacity(2);
try!(self.reader.by_ref().take(2).read_to_end(&mut endianess)); self.reader.by_ref().take(2).read_to_end(&mut endianess)?;
match &*endianess { match &*endianess {
b"II" => { b"II" => {
self.byte_order = ByteOrder::LittleEndian; self.byte_order = ByteOrder::LittleEndian;
@@ -300,12 +300,12 @@ impl<R: Read + Seek> Decoder<R> {
)) ))
} }
} }
if try!(self.read_short()) != 42 { if self.read_short()? != 42 {
return Err(TiffError::FormatError( return Err(TiffError::FormatError(
TiffFormatError::TiffSignatureInvalid, TiffFormatError::TiffSignatureInvalid,
)); ));
} }
self.next_ifd = match try!(self.read_long()) { self.next_ifd = match self.read_long()? {
0 => None, 0 => None,
n => Some(n), n => Some(n),
}; };
@@ -314,7 +314,7 @@ impl<R: Read + Seek> Decoder<R> {
/// Initializes the decoder. /// Initializes the decoder.
pub fn init(mut self) -> TiffResult<Decoder<R>> { pub fn init(mut self) -> TiffResult<Decoder<R>> {
try!(self.read_header()); self.read_header()?;
self.next_image()?; self.next_image()?;
Ok(self) Ok(self)
} }
@@ -323,13 +323,13 @@ impl<R: Read + Seek> Decoder<R> {
/// If there is no further image in the TIFF file a format error is returned. /// If there is no further image in the TIFF file a format error is returned.
/// To determine whether there are more images call `TIFFDecoder::more_images` instead. /// To determine whether there are more images call `TIFFDecoder::more_images` instead.
pub fn next_image(&mut self) -> TiffResult<()> { pub fn next_image(&mut self) -> TiffResult<()> {
self.ifd = Some(try!(self.read_ifd())); self.ifd = Some(self.read_ifd()?);
self.width = try!(self.get_tag_u32(ifd::Tag::ImageWidth)); self.width = self.get_tag_u32(ifd::Tag::ImageWidth)?;
self.height = try!(self.get_tag_u32(ifd::Tag::ImageLength)); self.height = self.get_tag_u32(ifd::Tag::ImageLength)?;
self.strip_decoder = None; self.strip_decoder = None;
self.photometric_interpretation = match FromPrimitive::from_u32(try!( self.photometric_interpretation = match FromPrimitive::from_u32(
self.get_tag_u32(ifd::Tag::PhotometricInterpretation) self.get_tag_u32(ifd::Tag::PhotometricInterpretation)?
)) { ) {
Some(val) => val, Some(val) => val,
None => { None => {
return Err(TiffError::UnsupportedError( return Err(TiffError::UnsupportedError(
@@ -337,7 +337,7 @@ impl<R: Read + Seek> Decoder<R> {
)) ))
} }
}; };
if let Some(val) = try!(self.find_tag_u32(ifd::Tag::Compression)) { if let Some(val) = self.find_tag_u32(ifd::Tag::Compression)? {
match FromPrimitive::from_u32(val) { match FromPrimitive::from_u32(val) {
Some(method) => self.compression_method = method, Some(method) => self.compression_method = method,
None => { None => {
@@ -347,17 +347,17 @@ impl<R: Read + Seek> Decoder<R> {
} }
} }
} }
if let Some(val) = try!(self.find_tag_u32(ifd::Tag::SamplesPerPixel)) { if let Some(val) = self.find_tag_u32(ifd::Tag::SamplesPerPixel)? {
self.samples = val as u8 self.samples = val as u8
} }
match self.samples { match self.samples {
1 => { 1 => {
if let Some(val) = try!(self.find_tag_u32(ifd::Tag::BitsPerSample)) { if let Some(val) = self.find_tag_u32(ifd::Tag::BitsPerSample)? {
self.bits_per_sample = vec![val as u8] self.bits_per_sample = vec![val as u8]
} }
} }
3 | 4 => { 3 | 4 => {
if let Some(val) = try!(self.find_tag_u32_vec(ifd::Tag::BitsPerSample)) { if let Some(val) = self.find_tag_u32_vec(ifd::Tag::BitsPerSample)? {
self.bits_per_sample = val.iter().map(|&v| v as u8).collect() self.bits_per_sample = val.iter().map(|&v| v as u8).collect()
} }
} }
@@ -426,7 +426,7 @@ impl<R: Read + Seek> Decoder<R> {
#[inline] #[inline]
pub fn read_offset(&mut self) -> Result<[u8; 4], io::Error> { pub fn read_offset(&mut self) -> Result<[u8; 4], io::Error> {
let mut val = [0; 4]; let mut val = [0; 4];
try!(self.reader.read_exact(&mut val)); self.reader.read_exact(&mut val)?;
Ok(val) Ok(val)
} }
@@ -446,13 +446,13 @@ impl<R: Read + Seek> Decoder<R> {
// Count 4 bytes // Count 4 bytes
// Value 4 bytes either a pointer the value itself // Value 4 bytes either a pointer the value itself
fn read_entry(&mut self) -> TiffResult<Option<(ifd::Tag, ifd::Entry)>> { fn read_entry(&mut self) -> TiffResult<Option<(ifd::Tag, ifd::Entry)>> {
let tag = ifd::Tag::from_u16(try!(self.read_short())); let tag = ifd::Tag::from_u16(self.read_short()?);
let type_: ifd::Type = match FromPrimitive::from_u16(try!(self.read_short())) { let type_: ifd::Type = match FromPrimitive::from_u16(self.read_short()?) {
Some(t) => t, Some(t) => t,
None => { None => {
// Unknown type. Skip this entry according to spec. // Unknown type. Skip this entry according to spec.
try!(self.read_long()); self.read_long()?;
try!(self.read_long()); self.read_long()?;
return Ok(None); return Ok(None);
} }
}; };
@@ -460,8 +460,8 @@ impl<R: Read + Seek> Decoder<R> {
tag, tag,
ifd::Entry::new( ifd::Entry::new(
type_, type_,
try!(self.read_long()), // count self.read_long()?, // count
try!(self.read_offset()), // offset self.read_offset()?, // offset
), ),
))) )))
} }
@@ -475,16 +475,16 @@ impl<R: Read + Seek> Decoder<R> {
TiffFormatError::ImageFileDirectoryNotFound, TiffFormatError::ImageFileDirectoryNotFound,
)) ))
} }
Some(offset) => try!(self.goto_offset(offset)), Some(offset) => self.goto_offset(offset)?,
} }
for _ in 0..try!(self.read_short()) { for _ in 0..self.read_short()? {
let (tag, entry) = match try!(self.read_entry()) { let (tag, entry) = match self.read_entry()? {
Some(val) => val, Some(val) => val,
None => continue, // Unknown data type in tag, skip None => continue, // Unknown data type in tag, skip
}; };
dir.insert(tag, entry); dir.insert(tag, entry);
} }
self.next_ifd = match try!(self.read_long()) { self.next_ifd = match self.read_long()? {
0 => None, 0 => None,
n => Some(n), n => Some(n),
}; };
@@ -501,7 +501,7 @@ impl<R: Read + Seek> Decoder<R> {
let limits = self.limits.clone(); let limits = self.limits.clone();
Ok(Some(try!(entry.val(&limits, self)))) Ok(Some(entry.val(&limits, self)?))
} }
/// Tries to retrieve a tag and convert it to the desired type. /// Tries to retrieve a tag and convert it to the desired type.
@@ -523,7 +523,7 @@ impl<R: Read + Seek> Decoder<R> {
/// Tries to retrieve a tag. /// Tries to retrieve a tag.
/// Returns an error if the tag is not present /// Returns an error if the tag is not present
pub fn get_tag(&mut self, tag: ifd::Tag) -> TiffResult<ifd::Value> { pub fn get_tag(&mut self, tag: ifd::Tag) -> TiffResult<ifd::Value> {
match try!(self.find_tag(tag)) { match self.find_tag(tag)? {
Some(val) => Ok(val), Some(val) => Ok(val),
None => Err(TiffError::FormatError( None => Err(TiffError::FormatError(
TiffFormatError::RequiredTagNotFound(tag), TiffFormatError::RequiredTagNotFound(tag),
@@ -550,8 +550,8 @@ impl<R: Read + Seek> Decoder<R> {
length: u32, length: u32,
max_uncompressed_length: usize, max_uncompressed_length: usize,
) -> TiffResult<usize> { ) -> TiffResult<usize> {
let color_type = try!(self.colortype()); let color_type = self.colortype()?;
try!(self.goto_offset(offset)); self.goto_offset(offset)?;
let (bytes, mut reader): (usize, Box<EndianReader>) = match self.compression_method { let (bytes, mut reader): (usize, Box<EndianReader>) = match self.compression_method {
CompressionMethod::None => { CompressionMethod::None => {
let order = self.reader.byte_order; let order = self.reader.byte_order;
@@ -561,20 +561,20 @@ impl<R: Read + Seek> Decoder<R> {
) )
} }
CompressionMethod::LZW => { CompressionMethod::LZW => {
let (bytes, reader) = try!(LZWReader::new( let (bytes, reader) = LZWReader::new(
&mut self.reader, &mut self.reader,
length as usize, length as usize,
max_uncompressed_length max_uncompressed_length
)); )?;
(bytes, Box::new(reader)) (bytes, Box::new(reader))
} }
CompressionMethod::PackBits => { CompressionMethod::PackBits => {
let order = self.reader.byte_order; let order = self.reader.byte_order;
let (bytes, reader) = try!(PackBitsReader::new( let (bytes, reader) = PackBitsReader::new(
&mut self.reader, &mut self.reader,
order, order,
length as usize length as usize
)); )?;
(bytes, Box::new(reader)) (bytes, Box::new(reader))
} }
method => { method => {
@@ -594,16 +594,16 @@ impl<R: Read + Seek> Decoder<R> {
(ColorType::RGB(8), DecodingBuffer::U8(ref mut buffer)) (ColorType::RGB(8), DecodingBuffer::U8(ref mut buffer))
| (ColorType::RGBA(8), DecodingBuffer::U8(ref mut buffer)) | (ColorType::RGBA(8), DecodingBuffer::U8(ref mut buffer))
| (ColorType::CMYK(8), DecodingBuffer::U8(ref mut buffer)) => { | (ColorType::CMYK(8), DecodingBuffer::U8(ref mut buffer)) => {
try!(reader.read_exact(&mut buffer[..bytes])); reader.read_exact(&mut buffer[..bytes])?;
bytes bytes
} }
(ColorType::RGBA(16), DecodingBuffer::U16(ref mut buffer)) (ColorType::RGBA(16), DecodingBuffer::U16(ref mut buffer))
| (ColorType::RGB(16), DecodingBuffer::U16(ref mut buffer)) => { | (ColorType::RGB(16), DecodingBuffer::U16(ref mut buffer)) => {
try!(reader.read_u16_into(&mut buffer[..bytes / 2])); reader.read_u16_into(&mut buffer[..bytes / 2])?;
bytes / 2 bytes / 2
} }
(ColorType::Gray(16), DecodingBuffer::U16(ref mut buffer)) => { (ColorType::Gray(16), DecodingBuffer::U16(ref mut buffer)) => {
try!(reader.read_u16_into(&mut buffer[..bytes / 2])); reader.read_u16_into(&mut buffer[..bytes / 2])?;
if self.photometric_interpretation == PhotometricInterpretation::WhiteIsZero { if self.photometric_interpretation == PhotometricInterpretation::WhiteIsZero {
for datum in buffer[..bytes / 2].iter_mut() { for datum in buffer[..bytes / 2].iter_mut() {
*datum = 0xffff - *datum *datum = 0xffff - *datum
@@ -612,7 +612,7 @@ impl<R: Read + Seek> Decoder<R> {
bytes / 2 bytes / 2
} }
(ColorType::Gray(n), DecodingBuffer::U8(ref mut buffer)) if n <= 8 => { (ColorType::Gray(n), DecodingBuffer::U8(ref mut buffer)) if n <= 8 => {
try!(reader.read_exact(&mut buffer[..bytes])); reader.read_exact(&mut buffer[..bytes])?;
if self.photometric_interpretation == PhotometricInterpretation::WhiteIsZero { if self.photometric_interpretation == PhotometricInterpretation::WhiteIsZero {
for byte in buffer[..bytes].iter_mut() { for byte in buffer[..bytes].iter_mut() {
*byte = 0xff - *byte *byte = 0xff - *byte
+5 -5
View File
@@ -85,12 +85,12 @@ impl LZWReader {
{ {
let order = reader.byte_order; let order = reader.byte_order;
let mut compressed = vec![0; compressed_length as usize]; let mut compressed = vec![0; compressed_length as usize];
try!(reader.read_exact(&mut compressed[..])); reader.read_exact(&mut compressed[..])?;
let mut uncompressed = Vec::with_capacity(max_uncompressed_length); let mut uncompressed = Vec::with_capacity(max_uncompressed_length);
let mut decoder = lzw::DecoderEarlyChange::new(lzw::MsbReader::new(), 8); let mut decoder = lzw::DecoderEarlyChange::new(lzw::MsbReader::new(), 8);
let mut bytes_read = 0; let mut bytes_read = 0;
while bytes_read < compressed_length && uncompressed.len() < max_uncompressed_length { while bytes_read < compressed_length && uncompressed.len() < max_uncompressed_length {
let (len, bytes) = try!(decoder.decode_bytes(&compressed[bytes_read..])); let (len, bytes) = decoder.decode_bytes(&compressed[bytes_read..])?;
bytes_read += len; bytes_read += len;
uncompressed.extend_from_slice(bytes); uncompressed.extend_from_slice(bytes);
} }
@@ -155,7 +155,7 @@ impl PackBitsReader {
fn read_packbits_run<R: Read + Seek>(reader: &mut R, buffer: &mut Vec<u8>) -> io::Result<usize> { fn read_packbits_run<R: Read + Seek>(reader: &mut R, buffer: &mut Vec<u8>) -> io::Result<usize> {
let mut header: [u8; 1] = [0]; let mut header: [u8; 1] = [0];
let bytes = try!(reader.read(&mut header)); let bytes = reader.read(&mut header)?;
match bytes { match bytes {
0 => Ok(0), 0 => Ok(0),
@@ -163,7 +163,7 @@ fn read_packbits_run<R: Read + Seek>(reader: &mut R, buffer: &mut Vec<u8>) -> io
-128 => Ok(1), -128 => Ok(1),
h if h >= -127 && h <= -1 => { h if h >= -127 && h <= -1 => {
let new_len = buffer.len() + (1 - h as isize) as usize; let new_len = buffer.len() + (1 - h as isize) as usize;
try!(reader.read_exact(&mut header)); reader.read_exact(&mut header)?;
buffer.resize(new_len, header[0]); buffer.resize(new_len, header[0]);
Ok(2) Ok(2)
} }
@@ -171,7 +171,7 @@ fn read_packbits_run<R: Read + Seek>(reader: &mut R, buffer: &mut Vec<u8>) -> io
let num_vals = h as usize + 1; let num_vals = h as usize + 1;
let start = buffer.len(); let start = buffer.len();
buffer.resize(start + num_vals, 0); buffer.resize(start + num_vals, 0);
try!(reader.read_exact(&mut buffer[start..])); reader.read_exact(&mut buffer[start..])?;
Ok(num_vals + 1) Ok(num_vals + 1)
} }
}, },