Skip to content

Commit

Permalink
chore: rewrite bitmask() using match
Browse files Browse the repository at this point in the history
  • Loading branch information
WieeRd committed May 16, 2024
1 parent 8b216c9 commit 9b7cd35
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/scramble.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
array,
cmp::Ordering,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
ops::Range,
};
Expand All @@ -20,14 +21,10 @@ where

/// Creates a bitmask with specified amount of leading zeros and the rest set to 1.
fn bitmask<const N: usize>(zero_bits: usize) -> [u8; N] {
array::from_fn(|i| {
if i < zero_bits / 8 {
0b00000000
} else if i == zero_bits / 8 {
0b11111111 >> (zero_bits % 8)
} else {
0b11111111
}
array::from_fn(|index| match index.cmp(&(zero_bits / 8)) {
Ordering::Less => 0b00000000,
Ordering::Equal => 0b11111111 >> (zero_bits % 8),
Ordering::Greater => 0b11111111,
})
}

Expand Down

0 comments on commit 9b7cd35

Please sign in to comment.