about summary refs log tree commit diff
path: root/src/libstd/net
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/net')
-rw-r--r--src/libstd/net/ip.rs32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs
index 05ef559422f..7c4985bd627 100644
--- a/src/libstd/net/ip.rs
+++ b/src/libstd/net/ip.rs
@@ -130,6 +130,24 @@ impl IpAddr {
             IpAddr::V6(ref a) => a.is_documentation(),
         }
     }
+
+    /// Returns true if this address is a valid IPv4 address, false if it's a valid IPv6 address.
+    #[unstable(feature = "ipaddr_checker", issue = "36949")]
+    pub fn is_ipv4(&self) -> bool {
+        match *self {
+            IpAddr::V4(_) => true,
+            IpAddr::V6(_) => false,
+        }
+    }
+
+    /// Returns true if this address is a valid IPv6 address, false if it's a valid IPv4 address.
+    #[unstable(feature = "ipaddr_checker", issue = "36949")]
+    pub fn is_ipv6(&self) -> bool {
+        match *self {
+            IpAddr::V4(_) => false,
+            IpAddr::V6(_) => true,
+        }
+    }
 }
 
 impl Ipv4Addr {
@@ -1023,4 +1041,18 @@ mod tests {
         assert!("2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap() <
                 "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap());
     }
+
+    #[test]
+    fn is_v4() {
+        let ip = IpAddr::V4(Ipv4Addr::new(100, 64, 3, 3));
+        assert!(ip.is_ipv4());
+        assert!(!ip.is_ipv6());
+    }
+
+    #[test]
+    fn is_v6() {
+        let ip = IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0xffff, 0x1234, 0x5678));
+        assert!(!ip.is_ipv4());
+        assert!(ip.is_ipv6());
+    }
 }