summaryrefslogtreecommitdiff
path: root/src/vrrpv2.rs
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@nimmagadda.net>2024-02-01 16:51:45 +0530
committerSunil Nimmagadda <sunil@nimmagadda.net>2024-02-01 16:51:45 +0530
commitccf44b4028345ae520e68c05e56ad9d167742c2b (patch)
tree9cd01ed3782c6dc2779a4a346aa6a49c3c457d2d /src/vrrpv2.rs
parent141fe942b909f48aeaf50668c321336975be0ad7 (diff)
Move to as_chunks.
Fuzz testing needs a nightly, so let's move to nightly everywhere.
Diffstat (limited to 'src/vrrpv2.rs')
-rw-r--r--src/vrrpv2.rs15
1 files changed, 6 insertions, 9 deletions
diff --git a/src/vrrpv2.rs b/src/vrrpv2.rs
index aabe60a..cae0f31 100644
--- a/src/vrrpv2.rs
+++ b/src/vrrpv2.rs
@@ -163,16 +163,13 @@ pub fn from_bytes(bytes: &[u8]) -> Result<VRRPv2, VRRPv2Error> {
Ok(vrrpv2)
}
-/// TODO: as_chunks is nicer but not stabilised yet.
fn checksum(bytes: &[u8]) -> u16 {
- let mut sum: u32 = 0;
- for chunk in bytes.chunks(2) {
- // Left over byte if any
- if chunk.len() == 1 {
- sum += u32::from(chunk[0]);
- } else {
- sum += u32::from(u16::from_be_bytes(chunk.try_into().unwrap()));
- }
+ let (chunks, remainder) = bytes.as_chunks::<2>();
+ let mut sum = chunks
+ .iter()
+ .fold(0, |acc, x| acc + u32::from(u16::from_be_bytes(*x)));
+ if !remainder.is_empty() {
+ sum += u32::from(remainder[0]);
}
while (sum >> 16) > 0 {
sum = (sum & 0xffff) + (sum >> 16);