diff options
| author | Corentin Henry <corentinhenry@gmail.com> | 2018-11-19 09:36:23 -0800 |
|---|---|---|
| committer | Corentin Henry <corentinhenry@gmail.com> | 2019-04-22 16:03:32 +0200 |
| commit | 8f679977e00dba29819aedbcfaf14927a4136430 (patch) | |
| tree | 2f2c70417f5c1bfe29960bb8dd1771cae02e649a /src/libstd | |
| parent | aea687c3148c7ae48833a0faef7e6ada24fce05c (diff) | |
| download | rust-8f679977e00dba29819aedbcfaf14927a4136430.tar.gz rust-8f679977e00dba29819aedbcfaf14927a4136430.zip | |
std::net: add Ipv4Addr::is_reserved()
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/net/ip.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/libstd/net/ip.rs b/src/libstd/net/ip.rs index 0fea70c6ed7..f94923d28d2 100644 --- a/src/libstd/net/ip.rs +++ b/src/libstd/net/ip.rs @@ -532,6 +532,34 @@ impl Ipv4Addr { !self.is_broadcast() && !self.is_documentation() && !self.is_unspecified() } + /// Returns [`true`] if this address is reserved by IANA for future use. [IETF RFC 1112] + /// defines the block of reserved addresses as `240.0.0.0/4`. This range normally includes the + /// broadcast address `255.255.255.255`, but this implementation explicitely excludes it, since + /// it is obviously not reserved for future use. + /// + /// [IETF RFC 1112]: https://tools.ietf.org/html/rfc1112 + /// [`true`]: ../../std/primitive.bool.html + /// + /// # Examples + /// + /// ``` + /// #![feature(ip)] + /// use std::net::Ipv4Addr; + /// + /// fn main() { + /// assert_eq!(Ipv4Addr::new(240, 0, 0, 0).is_reserved(), true); + /// assert_eq!(Ipv4Addr::new(255, 255, 255, 254).is_reserved(), true); + /// + /// assert_eq!(Ipv4Addr::new(239, 255, 255, 255).is_reserved(), false); + /// // The broadcast address is not considered as reserved for future use by this + /// // implementation + /// assert_eq!(Ipv4Addr::new(255, 255, 255, 255).is_reserved(), false); + /// } + /// ``` + pub fn is_reserved(&self) -> bool { + self.octets()[0] & 240 == 240 && !self.is_broadcast() + } + /// Returns [`true`] if this is a multicast address (224.0.0.0/4). /// /// Multicast addresses have a most significant octet between 224 and 239, |
