@@ -53,35 +53,58 @@ fn test_truncated_file() {
53
53
}
54
54
}
55
55
56
+ #[ track_caller]
57
+ fn decode_chopped_anim ( r : ChoppedReader ) {
58
+ let frames = gif:: DecodeOptions :: new ( ) . read_info ( r) . unwrap ( )
59
+ . into_iter ( ) . enumerate ( )
60
+ . map ( |( n, f) | f. expect ( & n. to_string ( ) ) )
61
+ . count ( ) ;
62
+ assert_eq ! ( frames, 14 ) ;
63
+ }
64
+
56
65
#[ test]
57
66
fn one_byte_at_a_time ( ) {
58
- let r = OneByte {
67
+ decode_chopped_anim ( ChoppedReader {
68
+ chunk_len : 1 ,
59
69
data : include_bytes ! ( "../tests/samples/moon_impact.gif" ) ,
60
- } ;
61
- let frames = gif:: DecodeOptions :: new ( ) . read_info ( r) . unwrap ( )
62
- . into_iter ( ) . enumerate ( ) . map ( |( n, f) | {
63
- f. expect ( & n. to_string ( ) )
64
- } ) . count ( ) ;
65
- assert_eq ! ( frames, 14 ) ;
70
+ } ) ;
71
+ }
72
+
73
+ #[ test]
74
+ fn two_bytes_at_a_time ( ) {
75
+ decode_chopped_anim ( ChoppedReader {
76
+ chunk_len : 2 ,
77
+ data : include_bytes ! ( "../tests/samples/moon_impact.gif" ) ,
78
+ } ) ;
66
79
}
67
80
68
- struct OneByte < ' a > {
81
+ #[ test]
82
+ fn three_bytes_at_a_time ( ) {
83
+ decode_chopped_anim ( ChoppedReader {
84
+ chunk_len : 3 ,
85
+ data : include_bytes ! ( "../tests/samples/moon_impact.gif" ) ,
86
+ } ) ;
87
+ }
88
+
89
+ struct ChoppedReader < ' a > {
90
+ chunk_len : usize ,
69
91
data : & ' a [ u8 ] ,
70
92
}
71
93
72
- impl io:: BufRead for OneByte < ' _ > {
94
+ impl io:: BufRead for ChoppedReader < ' _ > {
73
95
fn fill_buf ( & mut self ) -> io:: Result < & [ u8 ] > {
74
- Ok ( & self . data [ ..self . data . len ( ) . min ( 1 ) ] )
96
+ Ok ( & self . data [ ..self . data . len ( ) . min ( self . chunk_len ) ] )
75
97
}
98
+
76
99
fn consume ( & mut self , n : usize ) {
77
- debug_assert ! ( n <= 1 ) ;
100
+ debug_assert ! ( n <= self . chunk_len ) ;
78
101
self . data = & self . data [ n..] ;
79
102
}
80
103
}
81
104
82
- impl io:: Read for OneByte < ' _ > {
105
+ impl io:: Read for ChoppedReader < ' _ > {
83
106
fn read ( & mut self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
84
- let n = self . data . len ( ) . min ( buf. len ( ) ) . min ( 1 ) ;
107
+ let n = self . data . len ( ) . min ( buf. len ( ) ) . min ( self . chunk_len ) ;
85
108
buf[ ..n] . copy_from_slice ( & self . data [ ..n] ) ;
86
109
self . data = & self . data [ n..] ;
87
110
Ok ( n)
0 commit comments