Skip to content

Commit 6f649b0

Browse files
committed
Improve readability
1 parent 7d5b4bb commit 6f649b0

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/wire/ip.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,8 @@ pub mod checksum {
688688
// For each 32-byte chunk...
689689
const CHUNK_SIZE: usize = 32;
690690
const WORD_SIZE: usize = 2;
691-
for chunk in data.chunks_exact(CHUNK_SIZE) {
691+
let mut chunks = data.chunks_exact(CHUNK_SIZE);
692+
for chunk in &mut chunks {
692693
// ... take by 2 bytes and sum them.
693694
for pair in chunk.chunks_exact(WORD_SIZE) {
694695
accum += u16::from_be_bytes([pair[0], pair[1]]) as u32;
@@ -697,15 +698,15 @@ pub mod checksum {
697698

698699
// Sum the rest that does not fit the last 32-byte chunk,
699700
// taking by 2 bytes.
700-
let remainder = data.chunks_exact(CHUNK_SIZE).remainder();
701-
for pair in remainder.chunks_exact(WORD_SIZE) {
701+
let remainder = chunks.remainder();
702+
let mut word_pairs = remainder.chunks_exact(WORD_SIZE);
703+
for pair in &mut word_pairs {
702704
accum += u16::from_be_bytes([pair[0], pair[1]]) as u32;
703705
}
704706

705707
// Add the last remaining odd byte, if any.
706-
let last = remainder.chunks_exact(WORD_SIZE).remainder();
707-
if !last.is_empty() {
708-
accum += (last[0] as u32) << 8;
708+
if let Some(&byte) = word_pairs.remainder().first() {
709+
accum += (byte as u32) << 8;
709710
}
710711

711712
propagate_carries(accum)

0 commit comments

Comments
 (0)