Skip to content

Commit

Permalink
Add IpAddr::is_benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
CDirkx committed Jun 18, 2021
1 parent e2d6334 commit cbaccc1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
24 changes: 24 additions & 0 deletions library/std/src/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ impl IpAddr {
}
}

/// Returns [`true`] if this address is in a range designated for benchmarking.
///
/// See the documentation for [`Ipv4Addr::is_benchmarking()`] and
/// [`Ipv6Addr::is_benchmarking()`] for more details.
///
/// # Examples
///
/// ```
/// #![feature(ip)]
///
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(198, 19, 255, 255)).is_benchmarking(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0)).is_benchmarking(), true);
/// ```
#[unstable(feature = "ip", issue = "27709")]
#[inline]
pub const fn is_benchmarking(&self) -> bool {
match self {
IpAddr::V4(ip) => ip.is_benchmarking(),
IpAddr::V6(ip) => ip.is_benchmarking(),
}
}

/// Returns [`true`] if this address is an [`IPv4` address], and [`false`]
/// otherwise.
///
Expand Down
15 changes: 12 additions & 3 deletions library/std/src/net/ip/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ fn ip_properties() {
let global: u8 = 1 << 2;
let multicast: u8 = 1 << 3;
let doc: u8 = 1 << 4;
let benchmarking: u8 = 1 << 5;

if ($mask & unspec) == unspec {
assert!(ip!($s).is_unspecified());
Expand Down Expand Up @@ -254,6 +255,12 @@ fn ip_properties() {
} else {
assert!(!ip!($s).is_documentation());
}

if ($mask & benchmarking) == benchmarking {
assert!(ip!($s).is_benchmarking());
} else {
assert!(!ip!($s).is_benchmarking());
}
}};
}

Expand All @@ -262,6 +269,7 @@ fn ip_properties() {
let global: u8 = 1 << 2;
let multicast: u8 = 1 << 3;
let doc: u8 = 1 << 4;
let benchmarking: u8 = 1 << 5;

check!("0.0.0.0", unspec);
check!("0.0.0.1");
Expand All @@ -280,9 +288,9 @@ fn ip_properties() {
check!("239.255.255.255", global | multicast);
check!("255.255.255.255");
// make sure benchmarking addresses are not global
check!("198.18.0.0");
check!("198.18.54.2");
check!("198.19.255.255");
check!("198.18.0.0", benchmarking);
check!("198.18.54.2", benchmarking);
check!("198.19.255.255", benchmarking);
// make sure addresses reserved for protocol assignment are not global
check!("192.0.0.0");
check!("192.0.0.255");
Expand Down Expand Up @@ -313,6 +321,7 @@ fn ip_properties() {
check!("ff08::", multicast);
check!("ff0e::", global | multicast);
check!("2001:db8:85a3::8a2e:370:7334", doc);
check!("2001:2::ac32:23ff:21", global | benchmarking);
check!("102:304:506:708:90a:b0c:d0e:f10", global);
}

Expand Down

0 comments on commit cbaccc1

Please sign in to comment.