Skip to content

Commit

Permalink
Comparison between IpAddr and Ipv[46]Addr.
Browse files Browse the repository at this point in the history
  • Loading branch information
clarfonthey committed Dec 23, 2016
1 parent 00b4019 commit 9301e2e
Showing 1 changed file with 100 additions and 4 deletions.
104 changes: 100 additions & 4 deletions src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,26 @@ impl PartialEq for Ipv4Addr {
}
}

#[stable(feature = "ip_cmp", since = "1.15.0")]
impl PartialEq<Ipv4Addr> for IpAddr {
fn eq(&self, other: &Ipv4Addr) -> bool {
match *self {
IpAddr::V4(ref v4) => v4 == other,
IpAddr::V6(_) => false,
}
}
}

#[stable(feature = "ip_cmp", since = "1.15.0")]
impl PartialEq<IpAddr> for Ipv4Addr {
fn eq(&self, other: &IpAddr) -> bool {
match *other {
IpAddr::V4(ref v4) => self == v4,
IpAddr::V6(_) => false,
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for Ipv4Addr {}

Expand All @@ -568,6 +588,26 @@ impl PartialOrd for Ipv4Addr {
}
}

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

#[stable(feature = "ip_cmp", since = "1.15.0")]
impl PartialOrd<IpAddr> for Ipv4Addr {
fn partial_cmp(&self, other: &IpAddr) -> Option<Ordering> {
match *other {
IpAddr::V4(ref v4) => self.partial_cmp(v4),
IpAddr::V6(_) => Some(Ordering::Less),
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for Ipv4Addr {
fn cmp(&self, other: &Ipv4Addr) -> Ordering {
Expand Down Expand Up @@ -1026,6 +1066,26 @@ impl PartialEq for Ipv6Addr {
}
}

#[stable(feature = "ip_cmp", since = "1.15.0")]
impl PartialEq<IpAddr> for Ipv6Addr {
fn eq(&self, other: &IpAddr) -> bool {
match *other {
IpAddr::V4(_) => false,
IpAddr::V6(ref v6) => self == v6,
}
}
}

#[stable(feature = "ip_cmp", since = "1.15.0")]
impl PartialEq<Ipv6Addr> for IpAddr {
fn eq(&self, other: &Ipv6Addr) -> bool {
match *self {
IpAddr::V4(_) => false,
IpAddr::V6(ref v6) => v6 == other,
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl Eq for Ipv6Addr {}

Expand All @@ -1043,6 +1103,26 @@ impl PartialOrd for Ipv6Addr {
}
}

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

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

#[stable(feature = "rust1", since = "1.0.0")]
impl Ord for Ipv6Addr {
fn cmp(&self, other: &Ipv6Addr) -> Ordering {
Expand Down Expand Up @@ -1444,10 +1524,26 @@ mod tests {
}

#[test]
fn ord() {
assert!(Ipv4Addr::new(100, 64, 3, 3) < Ipv4Addr::new(192, 0, 2, 2));
assert!("2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap() <
"2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap());
fn cmp() {
let v41 = Ipv4Addr::new(100, 64, 3, 3);
let v42 = Ipv4Addr::new(192, 0, 2, 2);
let v61 = "2001:db8:f00::1002".parse::<Ipv6Addr>().unwrap();
let v62 = "2001:db8:f00::2001".parse::<Ipv6Addr>().unwrap();
assert!(v41 < v42);
assert!(v61 < v62);

assert_eq!(v41, IpAddr::V4(v41));
assert_eq!(v61, IpAddr::V6(v61));
assert!(v41 != IpAddr::V4(v42));
assert!(v61 != IpAddr::V6(v62));

assert!(v41 < IpAddr::V4(v42));
assert!(v61 < IpAddr::V6(v62));
assert!(IpAddr::V4(v41) < v42);
assert!(IpAddr::V6(v61) < v62);

assert!(v41 < IpAddr::V6(v61));
assert!(IpAddr::V4(v41) < v61);
}

#[test]
Expand Down

0 comments on commit 9301e2e

Please sign in to comment.