|
1 | 1 | #![cfg(feature = "std")]
|
2 | 2 |
|
3 |
| -use std::{fs, sync::mpsc, thread, time::Duration}; |
| 3 | +use std::{fs, sync::mpsc, thread, time::Duration, io}; |
4 | 4 |
|
5 | 5 | #[test]
|
6 | 6 | fn try_decode_crash_regression() {
|
@@ -42,3 +42,38 @@ fn decode(data: &[u8]) -> Result<(), gif::DecodingError> {
|
42 | 42 |
|
43 | 43 | Ok(())
|
44 | 44 | }
|
| 45 | + |
| 46 | +#[test] |
| 47 | +fn one_byte_at_a_time() { |
| 48 | + let r = OneByte { |
| 49 | + data: include_bytes!("../tests/samples/moon_impact.gif"), |
| 50 | + }; |
| 51 | + let frames = gif::DecodeOptions::new().read_info(r).unwrap() |
| 52 | + .into_iter().enumerate().map(|(n, f)| { |
| 53 | + f.expect(&n.to_string()) |
| 54 | + }).count(); |
| 55 | + assert_eq!(frames, 14); |
| 56 | +} |
| 57 | + |
| 58 | +struct OneByte<'a> { |
| 59 | + data: &'a [u8], |
| 60 | +} |
| 61 | + |
| 62 | +impl io::BufRead for OneByte<'_> { |
| 63 | + fn fill_buf(&mut self) -> io::Result<&[u8]> { |
| 64 | + Ok(&self.data[..self.data.len().min(1)]) |
| 65 | + } |
| 66 | + fn consume(&mut self, n: usize) { |
| 67 | + debug_assert!(n <= 1); |
| 68 | + self.data = &self.data[n..]; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +impl io::Read for OneByte<'_> { |
| 73 | + fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 74 | + let n = self.data.len().min(buf.len()).min(1); |
| 75 | + buf[..n].copy_from_slice(&self.data[..n]); |
| 76 | + self.data = &self.data[n..]; |
| 77 | + Ok(n) |
| 78 | + } |
| 79 | +} |
0 commit comments