@@ -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 {
@@ -260,11 +252,11 @@ impl LzwReader {
260
252
Ok ( LzwStatus :: Done ) | Ok ( LzwStatus :: Ok ) => { } ,
261
253
Ok ( LzwStatus :: NoProgress ) => {
262
254
if self . check_for_end_code {
263
- return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "No end code in lzw stream" ) ) ;
255
+ return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "no end code in lzw stream" ) ) ;
264
256
}
265
257
} ,
266
- Err ( LzwError :: InvalidCode ) => {
267
- return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , "invalid code in LZW stream" ) . into ( ) ) ;
258
+ Err ( err @ LzwError :: InvalidCode ) => {
259
+ return Err ( io:: Error :: new ( io:: ErrorKind :: InvalidData , err ) . into ( ) ) ;
268
260
}
269
261
}
270
262
Ok ( ( decoded. consumed_in , decoded. consumed_out ) )
@@ -446,7 +438,7 @@ impl StreamingDecoder {
446
438
} )
447
439
) ;
448
440
449
- let b = * buf. get ( 0 ) . ok_or_else ( || DecodingError :: format ( "empty buf" ) ) ?;
441
+ let b = * buf. get ( 0 ) . ok_or_else ( || io :: ErrorKind :: UnexpectedEof ) ?;
450
442
451
443
match self . state {
452
444
Magic ( i, mut version) => if i < 6 {
@@ -456,7 +448,7 @@ impl StreamingDecoder {
456
448
self . version = match & version[ 3 ..] {
457
449
b"87a" => Version :: V87a ,
458
450
b"89a" => Version :: V89a ,
459
- _ => return Err ( DecodingError :: format ( "unsupported GIF version " ) )
451
+ _ => return Err ( DecodingError :: format ( "malformed GIF header " ) )
460
452
} ;
461
453
goto ! ( U16Byte1 ( U16Value :: ScreenWidth , b) )
462
454
} else {
@@ -515,7 +507,7 @@ impl StreamingDecoder {
515
507
let global_table = global_flags & 0x80 != 0 ;
516
508
let table_size = if global_table {
517
509
let table_size = PLTE_CHANNELS * ( 1 << ( ( global_flags & 0b111 ) + 1 ) as usize ) ;
518
- self . global_color_table . try_reserve_exact ( table_size) . map_err ( |_| io:: Error :: from ( io :: ErrorKind :: OutOfMemory ) ) ?;
510
+ self . global_color_table . try_reserve_exact ( table_size) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
519
511
table_size
520
512
} else {
521
513
0usize
@@ -569,7 +561,7 @@ impl StreamingDecoder {
569
561
if local_table {
570
562
let entries = PLTE_CHANNELS * ( 1 << ( table_size + 1 ) ) ;
571
563
let mut pal = Vec :: new ( ) ;
572
- pal. try_reserve_exact ( entries) . map_err ( |_| io:: Error :: from ( io :: ErrorKind :: OutOfMemory ) ) ?;
564
+ pal. try_reserve_exact ( entries) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
573
565
frame. palette = Some ( pal) ;
574
566
goto ! ( LocalPalette ( entries) )
575
567
} else {
@@ -646,9 +638,7 @@ impl StreamingDecoder {
646
638
}
647
639
}
648
640
} else {
649
- Err ( DecodingError :: format (
650
- "unknown extention block encountered"
651
- ) )
641
+ Err ( DecodingError :: format ( "unknown block type encountered" ) )
652
642
}
653
643
}
654
644
SkipBlock ( left) => {
@@ -699,7 +689,7 @@ impl StreamingDecoder {
699
689
( len, len)
700
690
} ,
701
691
OutputBuffer :: Vec ( vec) => {
702
- vec. try_reserve ( n) . map_err ( |_| io:: Error :: from ( io :: ErrorKind :: OutOfMemory ) ) ?;
692
+ vec. try_reserve ( n) . map_err ( |_| io:: ErrorKind :: OutOfMemory ) ?;
703
693
vec. extend_from_slice ( & buf[ ..n] ) ;
704
694
( n, n)
705
695
} ,
@@ -773,5 +763,5 @@ impl StreamingDecoder {
773
763
774
764
#[ test]
775
765
fn error_cast ( ) {
776
- let _ : Box < dyn error:: Error > = DecodingError :: Format ( DecodingFormatError :: new ( "testing" ) ) . into ( ) ;
766
+ let _ : Box < dyn error:: Error > = DecodingError :: format ( "testing" ) . into ( ) ;
777
767
}
0 commit comments