1616//!
1717
1818use std:: fmt;
19- use std:: str:: Chars ;
19+ use std:: str;
2020use { Error , Hash } ;
2121
2222/// Trait for objects that can be serialized as hex strings
@@ -64,9 +64,9 @@ impl<T: Hash> FromHex for T {
6464
6565/// Iterator over a hex-encoded string slice which decodes hex and yields bytes.
6666pub struct HexIterator < ' a > {
67- /// The `Chars ` iterator whose first two characters will be decoded to yield
67+ /// The `Bytes ` iterator whose next two bytes will be decoded to yield
6868 /// the next byte.
69- chars : Chars < ' a > ,
69+ iter : str :: Bytes < ' a > ,
7070}
7171
7272impl < ' a > HexIterator < ' a > {
@@ -76,18 +76,18 @@ impl<'a> HexIterator<'a> {
7676 if s. len ( ) % 2 != 0 {
7777 Err ( Error :: OddLengthString ( s. len ( ) ) )
7878 } else {
79- Ok ( HexIterator {
80- chars : s. chars ( )
81- } )
79+ Ok ( HexIterator { iter : s. bytes ( ) } )
8280 }
8381 }
8482}
8583
86- fn chars_to_hex ( hi : char , lo : char ) -> Result < u8 , Error > {
87- let hih = hi. to_digit ( 16 )
88- . ok_or ( Error :: InvalidChar ( hi) ) ?;
89- let loh = lo. to_digit ( 16 )
90- . ok_or ( Error :: InvalidChar ( lo) ) ?;
84+ fn chars_to_hex ( hi : u8 , lo : u8 ) -> Result < u8 , Error > {
85+ let hih = ( hi as char )
86+ . to_digit ( 16 )
87+ . ok_or ( Error :: InvalidChar ( hi) ) ?;
88+ let loh = ( lo as char )
89+ . to_digit ( 16 )
90+ . ok_or ( Error :: InvalidChar ( lo) ) ?;
9191
9292 let ret = ( hih << 4 ) + loh;
9393 Ok ( ret as u8 )
@@ -97,21 +97,21 @@ impl<'a> Iterator for HexIterator<'a> {
9797 type Item = Result < u8 , Error > ;
9898
9999 fn next ( & mut self ) -> Option < Result < u8 , Error > > {
100- let hi = self . chars . next ( ) ?;
101- let lo = self . chars . next ( ) . unwrap ( ) ;
100+ let hi = self . iter . next ( ) ?;
101+ let lo = self . iter . next ( ) . unwrap ( ) ;
102102 Some ( chars_to_hex ( hi, lo) )
103103 }
104104
105105 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
106- let len = self . chars . as_str ( ) . len ( ) / 2 ;
107- ( len , Some ( len ) )
106+ let ( min , max ) = self . iter . size_hint ( ) ;
107+ ( min / 2 , max . map ( |x| x / 2 ) )
108108 }
109109}
110110
111111impl < ' a > DoubleEndedIterator for HexIterator < ' a > {
112112 fn next_back ( & mut self ) -> Option < Result < u8 , Error > > {
113- let lo = self . chars . next_back ( ) ?;
114- let hi = self . chars . next_back ( ) . unwrap ( ) ;
113+ let lo = self . iter . next_back ( ) ?;
114+ let hi = self . iter . next_back ( ) . unwrap ( ) ;
115115 Some ( chars_to_hex ( hi, lo) )
116116 }
117117}
@@ -330,15 +330,15 @@ mod tests {
330330 ) ;
331331 assert_eq ! (
332332 Vec :: <u8 >:: from_hex( badchar1) ,
333- Err ( Error :: InvalidChar ( 'Z' ) )
333+ Err ( Error :: InvalidChar ( b 'Z') )
334334 ) ;
335335 assert_eq ! (
336336 Vec :: <u8 >:: from_hex( badchar2) ,
337- Err ( Error :: InvalidChar ( 'Y' ) )
337+ Err ( Error :: InvalidChar ( b 'Y') )
338338 ) ;
339339 assert_eq ! (
340340 Vec :: <u8 >:: from_hex( badchar3) ,
341- Err ( Error :: InvalidChar ( '«' ) )
341+ Err ( Error :: InvalidChar ( 194 ) )
342342 ) ;
343343 }
344344}
0 commit comments