Make /32 net have size 1

This commit is contained in:
Linus Färnstrand
2016-06-18 07:50:01 +02:00
parent b1837c10fa
commit 8d57734a7d

View File

@ -94,16 +94,12 @@ impl Ipv4Network {
/// assert_eq!(net.size(), 65536);
///
/// let tinynet = Ipv4Network::from_cidr("0.0.0.0/32").unwrap();
/// assert_eq!(tinynet.size(), 0);
/// assert_eq!(tinynet.size(), 1);
/// ```
pub fn size(&self) -> u64 {
let host_bits = (IPV4_BITS - self.prefix) as u32;
if host_bits == 0 {
0
} else {
(2 as u64).pow(host_bits)
}
}
/// Returns the `n`:th address within this network.
/// The adresses are indexed from 0 and `n` must be smaller than the size of the network.
@ -305,7 +301,7 @@ mod test {
#[test]
fn size_v4_min() {
let net = Ipv4Network::from_cidr("0/32").unwrap();
assert_eq!(net.size(), 0);
assert_eq!(net.size(), 1);
}
#[test]
@ -320,7 +316,7 @@ mod test {
#[test]
fn nth_v4_fail() {
let cidr = Ipv4Network::new(Ipv4Addr::new(10, 0, 0, 0), 32);
assert!(cidr.nth(0).is_none());
assert!(cidr.nth(1).is_none());
}
#[test]