Skip to content

Commit cbaccc1

Browse files
committed
Add IpAddr::is_benchmarking
1 parent e2d6334 commit cbaccc1

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

library/std/src/net/ip.rs

+24
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,30 @@ impl IpAddr {
252252
}
253253
}
254254

255+
/// Returns [`true`] if this address is in a range designated for benchmarking.
256+
///
257+
/// See the documentation for [`Ipv4Addr::is_benchmarking()`] and
258+
/// [`Ipv6Addr::is_benchmarking()`] for more details.
259+
///
260+
/// # Examples
261+
///
262+
/// ```
263+
/// #![feature(ip)]
264+
///
265+
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
266+
///
267+
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(198, 19, 255, 255)).is_benchmarking(), true);
268+
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0x2, 0, 0, 0, 0, 0, 0)).is_benchmarking(), true);
269+
/// ```
270+
#[unstable(feature = "ip", issue = "27709")]
271+
#[inline]
272+
pub const fn is_benchmarking(&self) -> bool {
273+
match self {
274+
IpAddr::V4(ip) => ip.is_benchmarking(),
275+
IpAddr::V6(ip) => ip.is_benchmarking(),
276+
}
277+
}
278+
255279
/// Returns [`true`] if this address is an [`IPv4` address], and [`false`]
256280
/// otherwise.
257281
///

library/std/src/net/ip/tests.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ fn ip_properties() {
224224
let global: u8 = 1 << 2;
225225
let multicast: u8 = 1 << 3;
226226
let doc: u8 = 1 << 4;
227+
let benchmarking: u8 = 1 << 5;
227228

228229
if ($mask & unspec) == unspec {
229230
assert!(ip!($s).is_unspecified());
@@ -254,6 +255,12 @@ fn ip_properties() {
254255
} else {
255256
assert!(!ip!($s).is_documentation());
256257
}
258+
259+
if ($mask & benchmarking) == benchmarking {
260+
assert!(ip!($s).is_benchmarking());
261+
} else {
262+
assert!(!ip!($s).is_benchmarking());
263+
}
257264
}};
258265
}
259266

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

266274
check!("0.0.0.0", unspec);
267275
check!("0.0.0.1");
@@ -280,9 +288,9 @@ fn ip_properties() {
280288
check!("239.255.255.255", global | multicast);
281289
check!("255.255.255.255");
282290
// make sure benchmarking addresses are not global
283-
check!("198.18.0.0");
284-
check!("198.18.54.2");
285-
check!("198.19.255.255");
291+
check!("198.18.0.0", benchmarking);
292+
check!("198.18.54.2", benchmarking);
293+
check!("198.19.255.255", benchmarking);
286294
// make sure addresses reserved for protocol assignment are not global
287295
check!("192.0.0.0");
288296
check!("192.0.0.255");
@@ -313,6 +321,7 @@ fn ip_properties() {
313321
check!("ff08::", multicast);
314322
check!("ff0e::", global | multicast);
315323
check!("2001:db8:85a3::8a2e:370:7334", doc);
324+
check!("2001:2::ac32:23ff:21", global | benchmarking);
316325
check!("102:304:506:708:90a:b0c:d0e:f10", global);
317326
}
318327

0 commit comments

Comments
 (0)