Skip to content

Commit

Permalink
Change implementation of Ipv6Addr::is_unspecified and is_loopback
Browse files Browse the repository at this point in the history
… from `matches!` to `u128` comparison

Done because `matches!` doesn't optimize well with array comparisons
  • Loading branch information
CDirkx committed Sep 1, 2020
1 parent cd08def commit a43dd4f
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions library/std/src/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ impl Ipv6Addr {
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
#[stable(since = "1.7.0", feature = "ip_17")]
pub const fn is_unspecified(&self) -> bool {
matches!(self.segments(), [0, 0, 0, 0, 0, 0, 0, 0])
u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::UNSPECIFIED.octets())
}

/// Returns [`true`] if this is a loopback address (::1).
Expand All @@ -1160,7 +1160,7 @@ impl Ipv6Addr {
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
#[stable(since = "1.7.0", feature = "ip_17")]
pub const fn is_loopback(&self) -> bool {
matches!(self.segments(), [0, 0, 0, 0, 0, 0, 0, 1])
u128::from_be_bytes(self.octets()) == u128::from_be_bytes(Ipv6Addr::LOCALHOST.octets())
}

/// Returns [`true`] if the address appears to be globally routable.
Expand Down

0 comments on commit a43dd4f

Please sign in to comment.