Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ exclude = ["book/", ".github/"]
criterion = { version = "0.8" }
proptest = "1"

[lib]
bench = false

[profile.bench]
opt-level = 3
lto = true
Expand Down
28 changes: 14 additions & 14 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -614,7 +614,7 @@ impl Ipv4Network {
/// ```
#[inline]
pub const fn new(addr: Ipv4Addr, mask: Ipv4Addr) -> Self {
let addr = u32::from_be_bytes(addr.octets()) & u32::from_be_bytes(mask.octets());
let addr = addr.to_bits() & mask.to_bits();

Self(Ipv4Addr::from_bits(addr), mask)
}
Expand Down Expand Up @@ -723,7 +723,7 @@ impl Ipv4Network {
#[inline]
pub const fn to_bits(&self) -> (u32, u32) {
match self {
Self(addr, mask) => (u32::from_be_bytes(addr.octets()), u32::from_be_bytes(mask.octets())),
Self(addr, mask) => (addr.to_bits(), mask.to_bits()),
}
}

Expand Down Expand Up @@ -902,7 +902,7 @@ impl Ipv4Network {
/// ```
#[inline]
pub const fn is_contiguous(&self) -> bool {
let mask = u32::from_be_bytes(self.mask().octets());
let mask = self.mask().to_bits();
mask | mask.wrapping_sub(1) == u32::MAX
}

Expand Down Expand Up @@ -940,7 +940,7 @@ impl Ipv4Network {
/// ```
#[inline]
pub const fn prefix(&self) -> Option<u8> {
let mask = u32::from_be_bytes(self.mask().octets());
let mask = self.mask().to_bits();
let ones = mask.leading_ones();

if mask.count_ones() == ones {
Expand Down Expand Up @@ -1611,7 +1611,7 @@ impl Ipv6Network {
/// During construction, the address is normalized using mask.
#[inline]
pub const fn new(addr: Ipv6Addr, mask: Ipv6Addr) -> Self {
let addr = u128::from_be_bytes(addr.octets()) & u128::from_be_bytes(mask.octets());
let addr = addr.to_bits() & mask.to_bits();

Self(Ipv6Addr::from_bits(addr), mask)
}
Expand Down Expand Up @@ -1772,7 +1772,7 @@ impl Ipv6Network {
#[inline]
pub const fn to_bits(&self) -> (u128, u128) {
match self {
Self(addr, mask) => (u128::from_be_bytes(addr.octets()), u128::from_be_bytes(mask.octets())),
Self(addr, mask) => (addr.to_bits(), mask.to_bits()),
}
}

Expand Down Expand Up @@ -1962,7 +1962,7 @@ impl Ipv6Network {
/// ```
#[inline]
pub const fn is_contiguous(&self) -> bool {
let mask = u128::from_be_bytes(self.mask().octets());
let mask = self.mask().to_bits();
mask | mask.wrapping_sub(1) == u128::MAX
}

Expand Down Expand Up @@ -2103,7 +2103,7 @@ impl Ipv6Network {
/// ```
#[inline]
pub const fn prefix(&self) -> Option<u8> {
let mask = u128::from_be_bytes(self.mask().octets());
let mask = self.mask().to_bits();
let ones = mask.leading_ones();

if mask.count_ones() == ones {
Expand Down Expand Up @@ -3255,7 +3255,7 @@ impl Contiguous<Ipv4Network> {
pub const fn prefix(&self) -> u8 {
match self {
Self(net) => {
let mask = u32::from_be_bytes(net.mask().octets());
let mask = net.mask().to_bits();
let ones = mask.leading_ones();
ones as u8
}
Expand Down Expand Up @@ -3289,7 +3289,7 @@ impl Contiguous<Ipv6Network> {
pub const fn prefix(&self) -> u8 {
match self {
Self(net) => {
let mask = u128::from_be_bytes(net.mask().octets());
let mask = net.mask().to_bits();
let ones = mask.leading_ones();
ones as u8
}
Expand Down Expand Up @@ -5980,7 +5980,7 @@ mod test {
let mut before_addrs: Vec<u32> = Vec::new();
for net in &nets {
for addr in (*net).addrs() {
before_addrs.push(u32::from_be_bytes(addr.octets()));
before_addrs.push(addr.to_bits());
}
}
before_addrs.sort_unstable();
Expand All @@ -5991,7 +5991,7 @@ mod test {
let mut after_addrs: Vec<u32> = Vec::new();
for net in result.iter() {
for addr in (*net).addrs() {
after_addrs.push(u32::from_be_bytes(addr.octets()));
after_addrs.push(addr.to_bits());
}
}
after_addrs.sort_unstable();
Expand Down Expand Up @@ -6061,7 +6061,7 @@ mod test {
let mut before_addrs: Vec<u32> = Vec::new();
for net in &nets {
for addr in (*net).addrs() {
before_addrs.push(u32::from_be_bytes(addr.octets()));
before_addrs.push(addr.to_bits());
}
}
before_addrs.sort_unstable();
Expand All @@ -6072,7 +6072,7 @@ mod test {
let mut after_addrs: Vec<u32> = Vec::new();
for net in result.iter() {
for addr in (*net).addrs() {
after_addrs.push(u32::from_be_bytes(addr.octets()));
after_addrs.push(addr.to_bits());
}
}
after_addrs.sort_unstable();
Expand Down
Loading