Skip to content

Commit

Permalink
Do not call new for iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
JakubOnderka committed Jul 11, 2021
1 parent c4845a3 commit 47814c1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/ipv4_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::collections::btree_map::Entry;
/// IPv4 Network.
#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord)]
pub struct Ipv4Network {
network_address: Ipv4Addr,
netmask: u8,
pub(crate) network_address: Ipv4Addr,
pub(crate) netmask: u8,
}

impl Ipv4Network {
Expand Down
4 changes: 2 additions & 2 deletions src/ipv6_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub enum Ipv6MulticastScope {
/// IPv6 Network.
#[derive(Clone, Copy, Debug, Eq, PartialOrd, Ord)]
pub struct Ipv6Network {
network_address: Ipv6Addr,
netmask: u8,
pub(crate) network_address: Ipv6Addr,
pub(crate) netmask: u8,
}

impl Ipv6Network {
Expand Down
14 changes: 10 additions & 4 deletions src/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,10 @@ impl Iterator for Ipv4NetworkIterator {
None => self.is_done = true,
};

Some(Self::Item::new(Ipv4Addr::from(output), self.new_netmask).unwrap())
Some(Self::Item {
network_address: Ipv4Addr::from(output),
netmask: self.new_netmask,
})
} else {
None
}
Expand Down Expand Up @@ -244,7 +247,10 @@ impl Iterator for Ipv6NetworkIterator {
None => self.is_done = true,
};

Some(Self::Item::new(Ipv6Addr::from(output), self.new_netmask).unwrap())
Some(Self::Item {
network_address: Ipv6Addr::from(output),
netmask: self.new_netmask,
})
} else {
None
}
Expand All @@ -254,7 +260,7 @@ impl Iterator for Ipv6NetworkIterator {
let remaining = self.real_len();

if 128 - remaining.leading_zeros() > POINTER_WIDTH {
(::std::usize::MAX, None)
(usize::MAX, None)
} else {
let remaining_u64 = remaining as u64;
(remaining_u64 as usize, Some(remaining_u64 as usize))
Expand Down Expand Up @@ -368,7 +374,7 @@ mod tests {
let network = Ipv6Network::new(ip, 0).unwrap();
let iterator = Ipv6NetworkIterator::new(network, 128);

assert_eq!(iterator.real_len(), u128::max_value());
assert_eq!(iterator.real_len(), u128::MAX);
}

#[test]
Expand Down

0 comments on commit 47814c1

Please sign in to comment.