Skip to content

Commit

Permalink
Rollup merge of rust-lang#53257 - faern:ip-method-idiomatic-improveme…
Browse files Browse the repository at this point in the history
…nt, r=TimNN

Idiomatic improvements to IP method

Since match ergonomics and slice patterns are stable this might be more idiomatic modern Rust implementations of these methods? Or well, slice patterns with `..` are not stabilized yet, so maybe we want to specify all fields but with `_`?
  • Loading branch information
kennytm committed Aug 14, 2018
2 parents e77d995 + cbe80a9 commit 11484c6
Showing 1 changed file with 50 additions and 47 deletions.
97 changes: 50 additions & 47 deletions src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ impl IpAddr {
/// ```
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_unspecified(&self) -> bool {
match *self {
IpAddr::V4(ref a) => a.is_unspecified(),
IpAddr::V6(ref a) => a.is_unspecified(),
match self {
IpAddr::V4(ip) => ip.is_unspecified(),
IpAddr::V6(ip) => ip.is_unspecified(),
}
}

Expand All @@ -185,9 +185,9 @@ impl IpAddr {
/// ```
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_loopback(&self) -> bool {
match *self {
IpAddr::V4(ref a) => a.is_loopback(),
IpAddr::V6(ref a) => a.is_loopback(),
match self {
IpAddr::V4(ip) => ip.is_loopback(),
IpAddr::V6(ip) => ip.is_loopback(),
}
}

Expand All @@ -214,9 +214,9 @@ impl IpAddr {
/// }
/// ```
pub fn is_global(&self) -> bool {
match *self {
IpAddr::V4(ref a) => a.is_global(),
IpAddr::V6(ref a) => a.is_global(),
match self {
IpAddr::V4(ip) => ip.is_global(),
IpAddr::V6(ip) => ip.is_global(),
}
}

Expand All @@ -239,9 +239,9 @@ impl IpAddr {
/// ```
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_multicast(&self) -> bool {
match *self {
IpAddr::V4(ref a) => a.is_multicast(),
IpAddr::V6(ref a) => a.is_multicast(),
match self {
IpAddr::V4(ip) => ip.is_multicast(),
IpAddr::V6(ip) => ip.is_multicast(),
}
}

Expand All @@ -268,9 +268,9 @@ impl IpAddr {
/// }
/// ```
pub fn is_documentation(&self) -> bool {
match *self {
IpAddr::V4(ref a) => a.is_documentation(),
IpAddr::V6(ref a) => a.is_documentation(),
match self {
IpAddr::V4(ip) => ip.is_documentation(),
IpAddr::V6(ip) => ip.is_documentation(),
}
}

Expand All @@ -293,7 +293,7 @@ impl IpAddr {
/// ```
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
pub fn is_ipv4(&self) -> bool {
match *self {
match self {
IpAddr::V4(_) => true,
IpAddr::V6(_) => false,
}
Expand All @@ -318,7 +318,7 @@ impl IpAddr {
/// ```
#[stable(feature = "ipaddr_checker", since = "1.16.0")]
pub fn is_ipv6(&self) -> bool {
match *self {
match self {
IpAddr::V4(_) => false,
IpAddr::V6(_) => true,
}
Expand Down Expand Up @@ -483,11 +483,11 @@ impl Ipv4Addr {
/// ```
#[stable(since = "1.7.0", feature = "ip_17")]
pub fn is_private(&self) -> bool {
match (self.octets()[0], self.octets()[1]) {
(10, _) => true,
(172, b) if b >= 16 && b <= 31 => true,
(192, 168) => true,
_ => false
match self.octets() {
[10, ..] => true,
[172, b, ..] if b >= 16 && b <= 31 => true,
[192, 168, ..] => true,
_ => false,
}
}

Expand All @@ -509,7 +509,10 @@ impl Ipv4Addr {
/// ```
#[stable(since = "1.7.0", feature = "ip_17")]
pub fn is_link_local(&self) -> bool {
self.octets()[0] == 169 && self.octets()[1] == 254
match self.octets() {
[169, 254, ..] => true,
_ => false,
}
}

/// Returns [`true`] if the address appears to be globally routable.
Expand Down Expand Up @@ -612,11 +615,11 @@ impl Ipv4Addr {
/// ```
#[stable(since = "1.7.0", feature = "ip_17")]
pub fn is_documentation(&self) -> bool {
match(self.octets()[0], self.octets()[1], self.octets()[2], self.octets()[3]) {
(192, 0, 2, _) => true,
(198, 51, 100, _) => true,
(203, 0, 113, _) => true,
_ => false
match self.octets() {
[192, 0, 2, _] => true,
[198, 51, 100, _] => true,
[203, 0, 113, _] => true,
_ => false,
}
}

Expand Down Expand Up @@ -666,9 +669,9 @@ impl Ipv4Addr {
#[stable(feature = "ip_addr", since = "1.7.0")]
impl fmt::Display for IpAddr {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
IpAddr::V4(ref a) => a.fmt(fmt),
IpAddr::V6(ref a) => a.fmt(fmt),
match self {
IpAddr::V4(ip) => ip.fmt(fmt),
IpAddr::V6(ip) => ip.fmt(fmt),
}
}
}
Expand Down Expand Up @@ -717,8 +720,8 @@ impl PartialEq for Ipv4Addr {
#[stable(feature = "ip_cmp", since = "1.16.0")]
impl PartialEq<Ipv4Addr> for IpAddr {
fn eq(&self, other: &Ipv4Addr) -> bool {
match *self {
IpAddr::V4(ref v4) => v4 == other,
match self {
IpAddr::V4(v4) => v4 == other,
IpAddr::V6(_) => false,
}
}
Expand All @@ -727,8 +730,8 @@ impl PartialEq<Ipv4Addr> for IpAddr {
#[stable(feature = "ip_cmp", since = "1.16.0")]
impl PartialEq<IpAddr> for Ipv4Addr {
fn eq(&self, other: &IpAddr) -> bool {
match *other {
IpAddr::V4(ref v4) => self == v4,
match other {
IpAddr::V4(v4) => self == v4,
IpAddr::V6(_) => false,
}
}
Expand All @@ -755,8 +758,8 @@ impl PartialOrd for Ipv4Addr {
#[stable(feature = "ip_cmp", since = "1.16.0")]
impl PartialOrd<Ipv4Addr> for IpAddr {
fn partial_cmp(&self, other: &Ipv4Addr) -> Option<Ordering> {
match *self {
IpAddr::V4(ref v4) => v4.partial_cmp(other),
match self {
IpAddr::V4(v4) => v4.partial_cmp(other),
IpAddr::V6(_) => Some(Ordering::Greater),
}
}
Expand All @@ -765,8 +768,8 @@ impl PartialOrd<Ipv4Addr> for IpAddr {
#[stable(feature = "ip_cmp", since = "1.16.0")]
impl PartialOrd<IpAddr> for Ipv4Addr {
fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
match *other {
IpAddr::V4(ref v4) => self.partial_cmp(v4),
match other {
IpAddr::V4(v4) => self.partial_cmp(v4),
IpAddr::V6(_) => Some(Ordering::Less),
}
}
Expand Down Expand Up @@ -1335,19 +1338,19 @@ impl PartialEq for Ipv6Addr {
#[stable(feature = "ip_cmp", since = "1.16.0")]
impl PartialEq<IpAddr> for Ipv6Addr {
fn eq(&self, other: &IpAddr) -> bool {
match *other {
match other {
IpAddr::V4(_) => false,
IpAddr::V6(ref v6) => self == v6,
IpAddr::V6(v6) => self == v6,
}
}
}

#[stable(feature = "ip_cmp", since = "1.16.0")]
impl PartialEq<Ipv6Addr> for IpAddr {
fn eq(&self, other: &Ipv6Addr) -> bool {
match *self {
match self {
IpAddr::V4(_) => false,
IpAddr::V6(ref v6) => v6 == other,
IpAddr::V6(v6) => v6 == other,
}
}
}
Expand All @@ -1372,19 +1375,19 @@ impl PartialOrd for Ipv6Addr {
#[stable(feature = "ip_cmp", since = "1.16.0")]
impl PartialOrd<Ipv6Addr> for IpAddr {
fn partial_cmp(&self, other: &Ipv6Addr) -> Option<Ordering> {
match *self {
match self {
IpAddr::V4(_) => Some(Ordering::Less),
IpAddr::V6(ref v6) => v6.partial_cmp(other),
IpAddr::V6(v6) => v6.partial_cmp(other),
}
}
}

#[stable(feature = "ip_cmp", since = "1.16.0")]
impl PartialOrd<IpAddr> for Ipv6Addr {
fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
match *other {
match other {
IpAddr::V4(_) => Some(Ordering::Greater),
IpAddr::V6(ref v6) => self.partial_cmp(v6),
IpAddr::V6(v6) => self.partial_cmp(v6),
}
}
}
Expand Down

0 comments on commit 11484c6

Please sign in to comment.