Skip to content

Commit 9d5965a

Browse files
authored
Auto merge of #34694 - mathphreak:master, r=alexcrichton
Add IpAddr common methods Per rust-lang/rfcs#1668 (comment) no RFC is needed here. The generated documentation for these methods is being weird. It shows a deprecation message referencing #27709 for each of them even though two of the referenced methods were stabilized as part of that issue. I don't know how best to address that.
2 parents a63e3fa + 58da5dd commit 9d5965a

File tree

1 file changed

+121
-0
lines changed

1 file changed

+121
-0
lines changed

src/libstd/net/ip.rs

+121
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,66 @@ pub enum Ipv6MulticastScope {
5959
Global
6060
}
6161

62+
impl IpAddr {
63+
/// Returns true for the special 'unspecified' address ([IPv4], [IPv6]).
64+
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_unspecified
65+
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_unspecified
66+
#[unstable(feature="ip", issue="27709",
67+
reason="recently added and depends on unstable Ipv4Addr.is_unspecified()")]
68+
pub fn is_unspecified(&self) -> bool {
69+
match *self {
70+
IpAddr::V4(ref a) => a.is_unspecified(),
71+
IpAddr::V6(ref a) => a.is_unspecified(),
72+
}
73+
}
74+
75+
/// Returns true if this is a loopback address ([IPv4], [IPv6]).
76+
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_loopback
77+
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_loopback
78+
#[unstable(feature="ip", reason="recently added", issue="27709")]
79+
pub fn is_loopback(&self) -> bool {
80+
match *self {
81+
IpAddr::V4(ref a) => a.is_loopback(),
82+
IpAddr::V6(ref a) => a.is_loopback(),
83+
}
84+
}
85+
86+
/// Returns true if the address appears to be globally routable ([IPv4], [IPv6]).
87+
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_global
88+
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_global
89+
#[unstable(feature="ip", issue="27709",
90+
reason="recently added and depends on unstable Ip{v4,v6}Addr.is_global()")]
91+
pub fn is_global(&self) -> bool {
92+
match *self {
93+
IpAddr::V4(ref a) => a.is_global(),
94+
IpAddr::V6(ref a) => a.is_global(),
95+
}
96+
}
97+
98+
/// Returns true if this is a multicast address ([IPv4], [IPv6]).
99+
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_multicast
100+
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_multicast
101+
#[unstable(feature="ip", reason="recently added", issue="27709")]
102+
pub fn is_multicast(&self) -> bool {
103+
match *self {
104+
IpAddr::V4(ref a) => a.is_multicast(),
105+
IpAddr::V6(ref a) => a.is_multicast(),
106+
}
107+
}
108+
109+
/// Returns true if this address is in a range designated for documentation ([IPv4], [IPv6]).
110+
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_documentation
111+
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_documentation
112+
#[unstable(feature="ip", issue="27709",
113+
reason="recently added and depends on unstable Ipv6Addr.is_documentation()")]
114+
pub fn is_documentation(&self) -> bool {
115+
match *self {
116+
IpAddr::V4(ref a) => a.is_documentation(),
117+
IpAddr::V6(ref a) => a.is_documentation(),
118+
}
119+
}
120+
}
121+
62122
impl Ipv4Addr {
63123
/// Creates a new IPv4 address from four eight-bit octets.
64124
///
@@ -760,6 +820,67 @@ mod tests {
760820
None);
761821
}
762822

823+
#[test]
824+
fn ip_properties() {
825+
fn check4(octets: &[u8; 4], unspec: bool, loopback: bool,
826+
global: bool, multicast: bool, documentation: bool) {
827+
let ip = IpAddr::V4(Ipv4Addr::new(octets[0], octets[1], octets[2], octets[3]));
828+
assert_eq!(ip.is_unspecified(), unspec);
829+
assert_eq!(ip.is_loopback(), loopback);
830+
assert_eq!(ip.is_global(), global);
831+
assert_eq!(ip.is_multicast(), multicast);
832+
assert_eq!(ip.is_documentation(), documentation);
833+
}
834+
835+
fn check6(str_addr: &str, unspec: bool, loopback: bool,
836+
global: bool, u_doc: bool, mcast: bool) {
837+
let ip = IpAddr::V6(str_addr.parse().unwrap());
838+
assert_eq!(ip.is_unspecified(), unspec);
839+
assert_eq!(ip.is_loopback(), loopback);
840+
assert_eq!(ip.is_global(), global);
841+
assert_eq!(ip.is_documentation(), u_doc);
842+
assert_eq!(ip.is_multicast(), mcast);
843+
}
844+
845+
// address unspec loopbk global multicast doc
846+
check4(&[0, 0, 0, 0], true, false, false, false, false);
847+
check4(&[0, 0, 0, 1], false, false, true, false, false);
848+
check4(&[0, 1, 0, 0], false, false, true, false, false);
849+
check4(&[10, 9, 8, 7], false, false, false, false, false);
850+
check4(&[127, 1, 2, 3], false, true, false, false, false);
851+
check4(&[172, 31, 254, 253], false, false, false, false, false);
852+
check4(&[169, 254, 253, 242], false, false, false, false, false);
853+
check4(&[192, 0, 2, 183], false, false, false, false, true);
854+
check4(&[192, 1, 2, 183], false, false, true, false, false);
855+
check4(&[192, 168, 254, 253], false, false, false, false, false);
856+
check4(&[198, 51, 100, 0], false, false, false, false, true);
857+
check4(&[203, 0, 113, 0], false, false, false, false, true);
858+
check4(&[203, 2, 113, 0], false, false, true, false, false);
859+
check4(&[224, 0, 0, 0], false, false, true, true, false);
860+
check4(&[239, 255, 255, 255], false, false, true, true, false);
861+
check4(&[255, 255, 255, 255], false, false, false, false, false);
862+
863+
// address unspec loopbk global doc mcast
864+
check6("::", true, false, false, false, false);
865+
check6("::1", false, true, false, false, false);
866+
check6("::0.0.0.2", false, false, true, false, false);
867+
check6("1::", false, false, true, false, false);
868+
check6("fc00::", false, false, false, false, false);
869+
check6("fdff:ffff::", false, false, false, false, false);
870+
check6("fe80:ffff::", false, false, false, false, false);
871+
check6("febf:ffff::", false, false, false, false, false);
872+
check6("fec0::", false, false, false, false, false);
873+
check6("ff01::", false, false, false, false, true);
874+
check6("ff02::", false, false, false, false, true);
875+
check6("ff03::", false, false, false, false, true);
876+
check6("ff04::", false, false, false, false, true);
877+
check6("ff05::", false, false, false, false, true);
878+
check6("ff08::", false, false, false, false, true);
879+
check6("ff0e::", false, false, true, false, true);
880+
check6("2001:db8:85a3::8a2e:370:7334", false, false, false, true, false);
881+
check6("102:304:506:708:90a:b0c:d0e:f10", false, false, true, false, false);
882+
}
883+
763884
#[test]
764885
fn ipv4_properties() {
765886
fn check(octets: &[u8; 4], unspec: bool, loopback: bool,

0 commit comments

Comments
 (0)