Skip to content

Commit 3bc8221

Browse files
authored
Rollup merge of #85791 - CDirkx:is_unicast, r=joshtriplett
Add `Ipv6Addr::is_unicast` Adds an unstable utility method `Ipv6Addr::is_unicast` under the feature flag `ip` (tracking issue: #27709). Added for completeness with the other unicast methods (see also #85604 (comment)) and opposite of `is_multicast`.
2 parents 58f4c0f + 187b415 commit 3bc8221

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

library/std/src/net/ip.rs

+29-1
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,34 @@ impl Ipv6Addr {
12671267
(self.segments()[0] & 0xfe00) == 0xfc00
12681268
}
12691269

1270+
/// Returns [`true`] if this is a unicast address, as defined by [IETF RFC 4291].
1271+
/// Any address that is not a [multicast address] (`ff00::/8`) is unicast.
1272+
///
1273+
/// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291
1274+
/// [multicast address]: Ipv6Addr::is_multicast
1275+
///
1276+
/// # Examples
1277+
///
1278+
/// ```
1279+
/// #![feature(ip)]
1280+
///
1281+
/// use std::net::Ipv6Addr;
1282+
///
1283+
/// // The unspecified and loopback addresses are unicast.
1284+
/// assert_eq!(Ipv6Addr::UNSPECIFIED.is_unicast(), true);
1285+
/// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast(), true);
1286+
///
1287+
/// // Any address that is not a multicast address (`ff00::/8`) is unicast.
1288+
/// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast(), true);
1289+
/// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_unicast(), false);
1290+
/// ```
1291+
#[rustc_const_unstable(feature = "const_ipv6", issue = "76205")]
1292+
#[unstable(feature = "ip", issue = "27709")]
1293+
#[inline]
1294+
pub const fn is_unicast(&self) -> bool {
1295+
!self.is_multicast()
1296+
}
1297+
12701298
/// Returns `true` if the address is a unicast address with link-local scope,
12711299
/// as defined in [RFC 4291].
12721300
///
@@ -1417,7 +1445,7 @@ impl Ipv6Addr {
14171445
#[unstable(feature = "ip", issue = "27709")]
14181446
#[inline]
14191447
pub const fn is_unicast_global(&self) -> bool {
1420-
!self.is_multicast()
1448+
self.is_unicast()
14211449
&& !self.is_loopback()
14221450
&& !self.is_unicast_link_local()
14231451
&& !self.is_unique_local()

0 commit comments

Comments
 (0)