From d283ca6240b8004d0e74b088d79d1944492ac41d Mon Sep 17 00:00:00 2001 From: Abhishek Chanda Date: Mon, 19 Dec 2016 17:07:03 +0000 Subject: [PATCH] Implement mask for IpNetwork --- src/lib.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 3174885..6da2917 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,6 @@ #![cfg_attr(feature = "dev", feature(plugin))] #![cfg_attr(feature = "dev", plugin(clippy))] #![crate_type = "lib"] -#[allow(dead_code)] use std::net::IpAddr; @@ -65,6 +64,22 @@ impl IpNetwork { IpNetwork::V6(ref a) => a.prefix(), } } + + /// Returns the mask of the given `IpNetwork` + /// + /// # Example + /// ``` + /// use ipnetwork::IpNetwork; + /// + /// assert_eq!(IpNetwork::V4("10.9.0.32/16".parse().unwrap()).mask(), 16u8); + /// assert_eq!(IpNetwork::V6("ff01::0/32".parse().unwrap()).mask(), 32u8); + ///``` + pub fn mask(&self) -> IpAddr { + match *self { + IpNetwork::V4(ref a) => IpAddr::V4(a.mask()), + IpNetwork::V6(ref a) => IpAddr::V6(a.mask()), + } + } } /// Converts a `IpAddr` network mask into a prefix.