diff options
| author | bors <bors@rust-lang.org> | 2016-10-10 17:18:01 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-10-10 17:18:01 -0700 |
| commit | ead9212c3304fe0ebb77a483c0b52b174e8e9526 (patch) | |
| tree | 6f200af9d02d1d2abd67f0d7288bb72e3456f0bf /src/libstd/net | |
| parent | a3bc191b5f41df5143cc65084b13999896411817 (diff) | |
| parent | 80a7a3cb0ba56f93b30da8ba2970aae2d6551a8d (diff) | |
| download | rust-ead9212c3304fe0ebb77a483c0b52b174e8e9526.tar.gz rust-ead9212c3304fe0ebb77a483c0b52b174e8e9526.zip | |
Auto merge of #36707 - achanda:ip_type, r=alexcrichton
Add two functions to check type of given address The is_v4 function returns true if the given IP is v4. The is_v6 function returns true if the IP is v6.
Diffstat (limited to 'src/libstd/net')
| -rw-r--r-- | src/libstd/net/ip.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index 2030a61f60f..49080680fac 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 { @@ -1022,4 +1040,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()); + } } |
