Skip to content

Commit

Permalink
Stop iterator on error
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Jan 22, 2024
1 parent b4d2fd3 commit 0d71f7f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,13 +470,15 @@ impl<R: Read> IntoIterator for Decoder<R> {
fn into_iter(self) -> Self::IntoIter {
DecoderIter {
inner: self,
ended: false,
}
}
}

/// Use `decoder.into_iter()` to iterate over the frames
pub struct DecoderIter<R: Read> {
inner: Decoder<R>,
ended: bool,
}

impl<R: Read> DecoderIter<R> {
Expand All @@ -494,10 +496,20 @@ impl<R: Read> Iterator for DecoderIter<R> {
type Item = Result<Frame<'static>, DecodingError>;

fn next(&mut self) -> Option<Self::Item> {
match self.inner.read_next_frame() {
Ok(Some(_)) => self.inner.take_current_frame().map(Ok),
Ok(None) => None,
Err(err) => Some(Err(err)),
if !self.ended {
match self.inner.read_next_frame() {
Ok(Some(_)) => self.inner.take_current_frame().map(Ok),
Ok(None) => {
self.ended = true;
None
},
Err(err) => {
self.ended = true;
Some(Err(err))
},
}
} else {
None
}
}
}
2 changes: 1 addition & 1 deletion tests/stall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn test_truncated_file() {
let truncated = &data[..len];
// it's expected to fail often, but should not stall or panic
if let Ok(d) = gif::DecodeOptions::new().read_info(truncated) {
let _ = d.into_iter().take_while(|f| f.is_ok()).count();
let _ = d.into_iter().count();
}
}
}
Expand Down

0 comments on commit 0d71f7f

Please sign in to comment.