diff options
author | Sunil Nimmagadda <sunil@nimmagadda.net> | 2023-01-05 16:03:05 +0530 |
---|---|---|
committer | Sunil Nimmagadda <sunil@nimmagadda.net> | 2023-01-05 16:03:05 +0530 |
commit | 427835d242aa1807d420ed05598cc3e655d6b2af (patch) | |
tree | dd0a8ac3a9d87b2aa898ffeab9ca7d6961791631 | |
parent | 70e972a8bebfd5cb6ca9d7c7517a8fa007e12267 (diff) |
Note about avoiding mutability.
-rw-r--r-- | src/vrrpv2.rs | 6 |
1 files changed, 6 insertions, 0 deletions
diff --git a/src/vrrpv2.rs b/src/vrrpv2.rs index 0db2d7d..16bd74e 100644 --- a/src/vrrpv2.rs +++ b/src/vrrpv2.rs @@ -84,6 +84,12 @@ fn parse_auth_type(input: &[u8]) -> IResult<&[u8], VRRPv2AuthType> { // if let Some([b]) = chunks.remainder() { // sum += *b as u32 // } + +// Shadowing can be used to avoid `mut`... +// let sum =...; +// let sum = (sum & 0xffff) + (sum >> 16); +// let sum = (sum & 0xffff) + (sum >> 16); +// manually un-rolling while loop since it's needed atmost twice for an u32. fn validate_checksum(bytes: &[u8]) -> bool { let mut sum: u32 = bytes.chunks(2).fold(0, |acc: u32, x| { acc + u32::from(u16::from_ne_bytes(x.try_into().unwrap())) |