Skip to content

Commit 67291cc

Browse files
committed
std::net: add Ipv4Addr::is_shared()
1 parent f87b967 commit 67291cc

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/libstd/net/ip.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,28 @@ impl Ipv4Addr {
532532
!self.is_broadcast() && !self.is_documentation() && !self.is_unspecified()
533533
}
534534

535+
/// Returns [`true`] if this address is part of the Shared Address Space defined in
536+
/// [IETF RFC 6598] (`100.64.0.0/10`).
537+
///
538+
/// [IETF RFC 6598]: https://tools.ietf.org/html/rfc6598
539+
/// [`true`]: ../../std/primitive.bool.html
540+
///
541+
/// # Examples
542+
///
543+
/// ```
544+
/// #![feature(ip)]
545+
/// use std::net::Ipv4Addr;
546+
///
547+
/// fn main() {
548+
/// assert_eq!(Ipv4Addr::new(100, 64, 0, 0).is_shared(), true);
549+
/// assert_eq!(Ipv4Addr::new(100, 127, 255, 255).is_shared(), true);
550+
/// assert_eq!(Ipv4Addr::new(100, 128, 0, 0).is_shared(), false);
551+
/// }
552+
/// ```
553+
pub fn is_shared(&self) -> bool {
554+
self.octets()[0] == 100 && (self.octets()[1] & 0b1100_0000 == 0b0100_0000)
555+
}
556+
535557
/// Returns [`true`] if this address is part of `192.0.0.0/24`, which is reserved to
536558
/// IANA for IETF protocol assignments, as documented in [IETF RFC 6890].
537559
///

0 commit comments

Comments
 (0)