@@ -682,34 +682,30 @@ pub mod checksum {
682682 }
683683
684684 /// Compute an RFC 1071 compliant checksum (without the final complement).
685- pub fn data ( mut data : & [ u8 ] ) -> u16 {
685+ pub fn data ( data : & [ u8 ] ) -> u16 {
686686 let mut accum = 0 ;
687687
688688 // For each 32-byte chunk...
689689 const CHUNK_SIZE : usize = 32 ;
690- while data. len ( ) >= CHUNK_SIZE {
691- let chunk = & data[ ..CHUNK_SIZE ] ;
692- let mut i = 0 ;
690+ const WORD_SIZE : usize = 2 ;
691+ for chunk in data. chunks_exact ( CHUNK_SIZE ) {
693692 // ... take by 2 bytes and sum them.
694- while i + 1 < CHUNK_SIZE {
695- accum += u16:: from_be_bytes ( [ chunk[ i] , chunk[ i + 1 ] ] ) as u32 ;
696- i += 2 ;
693+ for pair in chunk. chunks_exact ( WORD_SIZE ) {
694+ accum += u16:: from_be_bytes ( [ pair[ 0 ] , pair[ 1 ] ] ) as u32 ;
697695 }
698-
699- data = & data[ CHUNK_SIZE ..] ;
700696 }
701697
702698 // Sum the rest that does not fit the last 32-byte chunk,
703699 // taking by 2 bytes.
704- let mut i = 0 ;
705- while i + 1 < data. len ( ) {
706- accum += u16:: from_be_bytes ( [ data[ i] , data[ i + 1 ] ] ) as u32 ;
707- i += 2 ;
700+ let remainder = data. chunks_exact ( CHUNK_SIZE ) . remainder ( ) ;
701+ for pair in remainder. chunks_exact ( WORD_SIZE ) {
702+ accum += u16:: from_be_bytes ( [ pair[ 0 ] , pair[ 1 ] ] ) as u32 ;
708703 }
709704
710705 // Add the last remaining odd byte, if any.
711- if i < data. len ( ) {
712- accum += ( data[ i] as u32 ) << 8 ;
706+ let last = remainder. chunks_exact ( WORD_SIZE ) . remainder ( ) ;
707+ if !last. is_empty ( ) {
708+ accum += ( last[ 0 ] as u32 ) << 8 ;
713709 }
714710
715711 propagate_carries ( accum)
0 commit comments