diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2021-06-09 12:04:01 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-09 12:04:01 +0900 |
| commit | 3bc822155807e3915a5455f8d08a06d0a8698086 (patch) | |
| tree | 2eff9bc33e25d6d97258351a74d0db2e9b37cd01 | |
| parent | 58f4c0f949b0109bed9fb79149db8344daa37724 (diff) | |
| parent | 187b4154505456d6d30224259d80cf224df21556 (diff) | |
| download | rust-3bc822155807e3915a5455f8d08a06d0a8698086.tar.gz rust-3bc822155807e3915a5455f8d08a06d0a8698086.zip | |
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 https://github.com/rust-lang/rust/issues/85604#issuecomment-848220455) and opposite of `is_multicast`.
| -rw-r--r-- | library/std/src/net/ip.rs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/library/std/src/net/ip.rs b/library/std/src/net/ip.rs index 80b6fb3d2a0..2b6d0d7d5da 100644 --- a/library/std/src/net/ip.rs +++ b/library/std/src/net/ip.rs @@ -1267,6 +1267,34 @@ impl Ipv6Addr { (self.segments()[0] & 0xfe00) == 0xfc00 } + /// Returns [`true`] if this is a unicast address, as defined by [IETF RFC 4291]. + /// Any address that is not a [multicast address] (`ff00::/8`) is unicast. + /// + /// [IETF RFC 4291]: https://tools.ietf.org/html/rfc4291 + /// [multicast address]: Ipv6Addr::is_multicast + /// + /// # Examples + /// + /// ``` + /// #![feature(ip)] + /// + /// use std::net::Ipv6Addr; + /// + /// // The unspecified and loopback addresses are unicast. + /// assert_eq!(Ipv6Addr::UNSPECIFIED.is_unicast(), true); + /// assert_eq!(Ipv6Addr::LOCALHOST.is_unicast(), true); + /// + /// // Any address that is not a multicast address (`ff00::/8`) is unicast. + /// assert_eq!(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0).is_unicast(), true); + /// assert_eq!(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0).is_unicast(), false); + /// ``` + #[rustc_const_unstable(feature = "const_ipv6", issue = "76205")] + #[unstable(feature = "ip", issue = "27709")] + #[inline] + pub const fn is_unicast(&self) -> bool { + !self.is_multicast() + } + /// Returns `true` if the address is a unicast address with link-local scope, /// as defined in [RFC 4291]. /// @@ -1417,7 +1445,7 @@ impl Ipv6Addr { #[unstable(feature = "ip", issue = "27709")] #[inline] pub const fn is_unicast_global(&self) -> bool { - !self.is_multicast() + self.is_unicast() && !self.is_loopback() && !self.is_unicast_link_local() && !self.is_unique_local() |
