@@ -33,21 +33,6 @@ impl error::Error for DecodingFormatError {
33
33
}
34
34
}
35
35
36
- impl DecodingFormatError {
37
- // Cold hints the optimizer that the error paths are less likely.
38
- //
39
- // This function isn't inlined to reduce code size
40
- // when it's often used with a string literal.
41
- #[ cold]
42
- fn new (
43
- err : impl Into < Box < dyn error:: Error + Send + Sync > > ,
44
- ) -> Self {
45
- DecodingFormatError {
46
- underlying : err. into ( ) ,
47
- }
48
- }
49
- }
50
-
51
36
#[ derive( Debug ) ]
52
37
/// Decoding error.
53
38
pub enum DecodingError {
@@ -58,11 +43,11 @@ pub enum DecodingError {
58
43
}
59
44
60
45
impl DecodingError {
61
- #[ inline ]
62
- pub ( crate ) fn format (
63
- err : impl Into < Box < dyn error :: Error + Send + Sync > > ,
64
- ) -> Self {
65
- DecodingError :: Format ( DecodingFormatError :: new ( err ) )
46
+ #[ cold ]
47
+ pub ( crate ) fn format ( err : & ' static str ) -> Self {
48
+ DecodingError :: Format ( DecodingFormatError {
49
+ underlying : err . into ( ) ,
50
+ } )
66
51
}
67
52
}
68
53
@@ -93,6 +78,13 @@ impl From<io::Error> for DecodingError {
93
78
}
94
79
}
95
80
81
+ impl From < io:: ErrorKind > for DecodingError {
82
+ #[ cold]
83
+ fn from ( err : io:: ErrorKind ) -> Self {
84
+ DecodingError :: Io ( io:: Error :: from ( err) )
85
+ }
86
+ }
87
+
96
88
impl From < DecodingFormatError > for DecodingError {
97
89
#[ inline]
98
90
fn from ( err : DecodingFormatError ) -> Self {
@@ -118,6 +110,7 @@ pub enum FrameDataType {
118
110
119
111
/// Indicates whether a certain object has been decoded
120
112
#[ derive( Debug ) ]
113
+ #[ non_exhaustive]
121
114
pub enum Decoded < ' a > {
122
115
/// Decoded nothing.
123
116
Nothing ,
@@ -257,11 +250,11 @@ impl LzwReader {
257
250
Ok ( LzwStatus :: Done | LzwStatus :: Ok ) => { } ,
258
251
Ok ( LzwStatus :: NoProgress ) => {
259
252
if self . check_for_end_code {
260
- return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "No end code in lzw stream" ) ) ;
253
+ return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "no end code in lzw stream" ) ) ;
261
254
}
262
255
} ,
263
- Err ( LzwError :: InvalidCode ) => {
264
- return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "invalid code in LZW stream" ) . into ( ) ) ;
256
+ Err ( err @ LzwError :: InvalidCode ) => {
257
+ return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , err ) . into ( ) ) ;
265
258
}
266
259
}
267
260
Ok ( ( decoded. consumed_in , decoded. consumed_out ) )
@@ -272,7 +265,6 @@ impl LzwReader {
272
265
pub struct StreamingDecoder {
273
266
state : State ,
274
267
lzw_reader : LzwReader ,
275
- skip_extensions : bool ,
276
268
skip_frame_decoding : bool ,
277
269
check_frame_consistency : bool ,
278
270
allow_unknown_blocks : bool ,
@@ -289,7 +281,6 @@ pub struct StreamingDecoder {
289
281
290
282
/// One version number of the GIF standard.
291
283
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash ) ]
292
- #[ non_exhaustive]
293
284
pub enum Version {
294
285
/// Version 87a, from May 1987.
295
286
V87a ,
@@ -324,7 +315,6 @@ impl StreamingDecoder {
324
315
StreamingDecoder {
325
316
state : Magic ( 0 , [ 0 ; 6 ] ) ,
326
317
lzw_reader : LzwReader :: new ( options. check_for_end_code ) ,
327
- skip_extensions : true ,
328
318
skip_frame_decoding : options. skip_frame_decoding ,
329
319
check_frame_consistency : options. check_frame_consistency ,
330
320
allow_unknown_blocks : options. allow_unknown_blocks ,
@@ -435,15 +425,6 @@ impl StreamingDecoder {
435
425
self . version
436
426
}
437
427
438
- /// Configure whether extensions are saved or skipped.
439
- #[ deprecated = "Does not work as intended. In fact, doesn't do anything. This may disappear soon." ]
440
- pub fn set_extensions ( & mut self , extensions : Extensions ) {
441
- self . skip_extensions = match extensions {
442
- Extensions :: Skip => true ,
443
- Extensions :: Save => false ,
444
- }
445
- }
446
-
447
428
fn next_state ( & mut self , buf : & [ u8 ] , write_into : & mut OutputBuffer < ' _ > ) -> Result < ( usize , Decoded < ' _ > ) , DecodingError > {
448
429
macro_rules! goto (
449
430
( $n: expr, $state: expr) => ( {
@@ -464,7 +445,7 @@ impl StreamingDecoder {
464
445
} )
465
446
) ;
466
447
467
- let b = * buf. get ( 0 ) . ok_or_else ( || DecodingError :: format ( "empty buf" ) ) ?;
448
+ let b = * buf. get ( 0 ) . ok_or_else ( || io :: ErrorKind :: UnexpectedEof ) ?;
468
449
469
450
match self . state {
470
451
Magic ( i, mut version) => if i < 6 {
@@ -474,7 +455,7 @@ impl StreamingDecoder {
474
455
self . version = match & version[ 3 ..] {
475
456
b"87a" => Version :: V87a ,
476
457
b"89a" => Version :: V89a ,
477
- _ => return Err ( DecodingError :: format ( "unsupported GIF version " ) )
458
+ _ => return Err ( DecodingError :: format ( "malformed GIF header " ) )
478
459
} ;
479
460
goto ! ( U16Byte1 ( U16Value :: ScreenWidth , b) )
480
461
} else {
@@ -533,7 +514,7 @@ impl StreamingDecoder {
533
514
let global_table = global_flags & 0x80 != 0 ;
534
515
let table_size = if global_table {
535
516
let table_size = PLTE_CHANNELS * ( 1 << ( ( global_flags & 0b111 ) + 1 ) as usize ) ;
536
- self . global_color_table . try_reserve_exact ( table_size) . map_err ( |_| io:: Error :: from ( io :: ErrorKind :: OutOfMemory ) ) ?;
517
+ self . global_color_table . try_reserve_exact ( table_size) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
537
518
table_size
538
519
} else {
539
520
0usize
@@ -587,7 +568,7 @@ impl StreamingDecoder {
587
568
if local_table {
588
569
let entries = PLTE_CHANNELS * ( 1 << ( table_size + 1 ) ) ;
589
570
let mut pal = Vec :: new ( ) ;
590
- pal. try_reserve_exact ( entries) . map_err ( |_| io:: Error :: from ( io :: ErrorKind :: OutOfMemory ) ) ?;
571
+ pal. try_reserve_exact ( entries) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
591
572
frame. palette = Some ( pal) ;
592
573
goto ! ( LocalPalette ( entries) )
593
574
} else {
@@ -664,9 +645,7 @@ impl StreamingDecoder {
664
645
}
665
646
}
666
647
} else {
667
- Err ( DecodingError :: format (
668
- "unknown extention block encountered"
669
- ) )
648
+ Err ( DecodingError :: format ( "unknown block type encountered" ) )
670
649
}
671
650
}
672
651
SkipBlock ( left) => {
@@ -717,7 +696,7 @@ impl StreamingDecoder {
717
696
( len, len)
718
697
} ,
719
698
OutputBuffer :: Vec ( vec) => {
720
- vec. try_reserve ( n) . map_err ( |_| io:: Error :: from ( io :: ErrorKind :: OutOfMemory ) ) ?;
699
+ vec. try_reserve ( n) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
721
700
vec. extend_from_slice ( & buf[ ..n] ) ;
722
701
( n, n)
723
702
} ,
@@ -787,5 +766,5 @@ impl StreamingDecoder {
787
766
788
767
#[ test]
789
768
fn error_cast ( ) {
790
- let _ : Box < dyn error:: Error > = DecodingError :: Format ( DecodingFormatError :: new ( "testing" ) ) . into ( ) ;
769
+ let _ : Box < dyn error:: Error > = DecodingError :: format ( "testing" ) . into ( ) ;
791
770
}
0 commit comments