From c22db3db6d18141e21ea79cd6e0118177850b1b9 Mon Sep 17 00:00:00 2001 From: "J. Cliff Dyer" Date: Thu, 7 Sep 2017 14:08:58 -0400 Subject: [PATCH 1/4] IP address convenience constructors --- src/libstd/net/ip.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index 0abf8179cc971..d6387fdd659a8 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -342,6 +342,32 @@ impl Ipv4Addr { } } + /// Creates a new IPv4 address with the address pointing to localhost: 127.0.0.1. + /// + /// # Examples + /// + /// ``` + /// use std::net::Ipv4Addr; + /// + /// let addr = Ipv4Addr::localhost(); + /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1)); + pub fn localhost() -> Ipv4Addr { + Ipv4Addr::new(127, 0, 0, 1) + } + + /// Creates a new IPv4 address representing an unspecified address: 0.0.0.0 + /// + /// # Examples + /// + /// ``` + /// use std::net::Ipv4Addr; + /// + /// let addr = Ipv4Addr::unspecified(); + /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0)); + pub fn unspecified() -> Ipv4Addr { + Ipv4Addr::new(0, 0, 0, 0) + } + /// Returns the four eight-bit integers that make up this address. /// /// # Examples @@ -788,6 +814,32 @@ impl Ipv6Addr { Ipv6Addr { inner: addr } } + /// Creates a new IPv6 address representing localhost: `::1`. + /// + /// # Examples + /// + /// ``` + /// use std::net::Ipv6Addr; + /// + /// let addr = Ipv6Addr::localhost(); + /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); + pub fn localhost() -> Ipv6Addr { + Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1) + } + + /// Creates a new IPv6 address representing the unspecified address: `::` + /// + /// # Examples + /// + /// ``` + /// use std::net::Ipv6Addr; + /// + /// let addr = Ipv6Addr::unspecified(); + /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)); + pub fn unspecified() -> Ipv6Addr { + Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0) + } + /// Returns the eight 16-bit segments that make up this address. /// /// # Examples @@ -1681,6 +1733,22 @@ mod tests { assert_eq!(Ipv6Addr::from(0x112233445566778899aabbccddeeff11u128), a); } + #[test] + fn ipv4_from_constructors() { + assert_eq!(Ipv4Addr::localhost(), Ipv4Addr::new(127, 0, 0, 1)); + assert!(Ipv4Addr::localhost().is_loopback()); + assert_eq!(Ipv4Addr::unspecified(), Ipv4Addr::new(0, 0, 0, 0)); + assert!(Ipv4Addr::unspecified().is_unspecified()); + } + + #[test] + fn ipv6_from_contructors() { + assert_eq!(Ipv6Addr::localhost(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); + assert!(Ipv6Addr::localhost().is_loopback()); + assert_eq!(Ipv6Addr::unspecified(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)); + assert!(Ipv6Addr::unspecified().is_unspecified()); + } + #[test] fn ipv4_from_octets() { assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1)) From ad170f23397f9da8d2180c4a48981808bf5535c9 Mon Sep 17 00:00:00 2001 From: "J. Cliff Dyer" Date: Thu, 7 Sep 2017 19:06:57 -0400 Subject: [PATCH 2/4] Close doc examples and trim whitespace. --- src/libstd/net/ip.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index d6387fdd659a8..a3775d30aa8b2 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -351,6 +351,7 @@ impl Ipv4Addr { /// /// let addr = Ipv4Addr::localhost(); /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1)); + /// ``` pub fn localhost() -> Ipv4Addr { Ipv4Addr::new(127, 0, 0, 1) } @@ -364,6 +365,7 @@ impl Ipv4Addr { /// /// let addr = Ipv4Addr::unspecified(); /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0)); + /// ``` pub fn unspecified() -> Ipv4Addr { Ipv4Addr::new(0, 0, 0, 0) } @@ -823,6 +825,7 @@ impl Ipv6Addr { /// /// let addr = Ipv6Addr::localhost(); /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); + /// ``` pub fn localhost() -> Ipv6Addr { Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1) } @@ -836,6 +839,7 @@ impl Ipv6Addr { /// /// let addr = Ipv6Addr::unspecified(); /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)); + /// ``` pub fn unspecified() -> Ipv6Addr { Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0) } @@ -1748,7 +1752,7 @@ mod tests { assert_eq!(Ipv6Addr::unspecified(), Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)); assert!(Ipv6Addr::unspecified().is_unspecified()); } - + #[test] fn ipv4_from_octets() { assert_eq!(Ipv4Addr::from([127, 0, 0, 1]), Ipv4Addr::new(127, 0, 0, 1)) From 8b6122f8b8001de80fce59a5896566b31c6a69ff Mon Sep 17 00:00:00 2001 From: "J. Cliff Dyer" Date: Fri, 8 Sep 2017 17:20:31 -0400 Subject: [PATCH 3/4] Add feature gate to doctests. --- src/libstd/net/ip.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index a3775d30aa8b2..d3ea3845f1207 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -347,6 +347,7 @@ impl Ipv4Addr { /// # Examples /// /// ``` + /// #![feature(ip)] /// use std::net::Ipv4Addr; /// /// let addr = Ipv4Addr::localhost(); @@ -361,6 +362,7 @@ impl Ipv4Addr { /// # Examples /// /// ``` + /// #![feature(ip)] /// use std::net::Ipv4Addr; /// /// let addr = Ipv4Addr::unspecified(); @@ -821,6 +823,7 @@ impl Ipv6Addr { /// # Examples /// /// ``` + /// #![feature(ip)] /// use std::net::Ipv6Addr; /// /// let addr = Ipv6Addr::localhost(); @@ -835,6 +838,7 @@ impl Ipv6Addr { /// # Examples /// /// ``` + /// #![feature(ip)] /// use std::net::Ipv6Addr; /// /// let addr = Ipv6Addr::unspecified(); From 9d5b0e1ff54198543233f418fbc43921b5f7201b Mon Sep 17 00:00:00 2001 From: "J. Cliff Dyer" Date: Thu, 14 Sep 2017 21:30:36 -0400 Subject: [PATCH 4/4] Add unstable attributes for Ipv?Addr constructors. --- src/libstd/net/ip.rs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index d3ea3845f1207..eea604943af8b 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -10,7 +10,7 @@ #![unstable(feature = "ip", reason = "extra functionality has not been \ scrutinized to the level that it should \ - be stable", + be to be stable", issue = "27709")] use cmp::Ordering; @@ -347,12 +347,15 @@ impl Ipv4Addr { /// # Examples /// /// ``` - /// #![feature(ip)] + /// #![feature(ip_constructors)] /// use std::net::Ipv4Addr; /// /// let addr = Ipv4Addr::localhost(); /// assert_eq!(addr, Ipv4Addr::new(127, 0, 0, 1)); /// ``` + #[unstable(feature = "ip_constructors", + reason = "requires greater scrutiny before stabilization", + issue = "44582")] pub fn localhost() -> Ipv4Addr { Ipv4Addr::new(127, 0, 0, 1) } @@ -362,12 +365,15 @@ impl Ipv4Addr { /// # Examples /// /// ``` - /// #![feature(ip)] + /// #![feature(ip_constructors)] /// use std::net::Ipv4Addr; /// /// let addr = Ipv4Addr::unspecified(); /// assert_eq!(addr, Ipv4Addr::new(0, 0, 0, 0)); /// ``` + #[unstable(feature = "ip_constructors", + reason = "requires greater scrutiny before stabilization", + issue = "44582")] pub fn unspecified() -> Ipv4Addr { Ipv4Addr::new(0, 0, 0, 0) } @@ -823,12 +829,15 @@ impl Ipv6Addr { /// # Examples /// /// ``` - /// #![feature(ip)] + /// #![feature(ip_constructors)] /// use std::net::Ipv6Addr; /// /// let addr = Ipv6Addr::localhost(); /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)); /// ``` + #[unstable(feature = "ip_constructors", + reason = "requires greater scrutiny before stabilization", + issue = "44582")] pub fn localhost() -> Ipv6Addr { Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1) } @@ -838,12 +847,15 @@ impl Ipv6Addr { /// # Examples /// /// ``` - /// #![feature(ip)] + /// #![feature(ip_constructors)] /// use std::net::Ipv6Addr; /// /// let addr = Ipv6Addr::unspecified(); /// assert_eq!(addr, Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)); /// ``` + #[unstable(feature = "ip_constructors", + reason = "requires greater scrutiny before stabilization", + issue = "44582")] pub fn unspecified() -> Ipv6Addr { Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0) }