Skip to content

Commit

Permalink
Add missing examples for IpAddr enum
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 30, 2016
1 parent f50dbd5 commit 336e5dd
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/libstd/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,18 @@ pub enum Ipv6MulticastScope {

impl IpAddr {
/// Returns true for the special 'unspecified' address ([IPv4], [IPv6]).
///
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_unspecified
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_unspecified
///
/// # Examples
///
/// ```
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)).is_unspecified(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0)).is_unspecified(), true);
/// ```
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_unspecified(&self) -> bool {
match *self {
Expand All @@ -90,8 +100,18 @@ impl IpAddr {
}

/// Returns true if this is a loopback address ([IPv4], [IPv6]).
///
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_loopback
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_loopback
///
/// # Examples
///
/// ```
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)).is_loopback(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 0x1)).is_loopback(), true);
/// ```
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_loopback(&self) -> bool {
match *self {
Expand All @@ -101,8 +121,23 @@ impl IpAddr {
}

/// Returns true if the address appears to be globally routable ([IPv4], [IPv6]).
///
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_global
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_global
///
/// # Examples
///
/// ```
/// #![feature(ip)]
///
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// fn main() {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(80, 9, 12, 3)).is_global(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0, 0, 0x1c9, 0, 0, 0xafc8, 0, 0x1)).is_global(),
/// true);
/// }
/// ```
pub fn is_global(&self) -> bool {
match *self {
IpAddr::V4(ref a) => a.is_global(),
Expand All @@ -111,8 +146,18 @@ impl IpAddr {
}

/// Returns true if this is a multicast address ([IPv4], [IPv6]).
///
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_multicast
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_multicast
///
/// # Examples
///
/// ```
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(224, 254, 0, 0)).is_multicast(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0xff00, 0, 0, 0, 0, 0, 0, 0)).is_multicast(), true);
/// ```
#[stable(feature = "ip_shared", since = "1.12.0")]
pub fn is_multicast(&self) -> bool {
match *self {
Expand All @@ -122,8 +167,23 @@ impl IpAddr {
}

/// Returns true if this address is in a range designated for documentation ([IPv4], [IPv6]).
///
/// [IPv4]: ../../std/net/struct.Ipv4Addr.html#method.is_documentation
/// [IPv6]: ../../std/net/struct.Ipv6Addr.html#method.is_documentation
///
/// # Examples
///
/// ```
/// #![feature(ip)]
///
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// fn main() {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_documentation(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0))
/// .is_documentation(), true);
/// }
/// ```
pub fn is_documentation(&self) -> bool {
match *self {
IpAddr::V4(ref a) => a.is_documentation(),
Expand All @@ -132,6 +192,20 @@ impl IpAddr {
}

/// Returns true if this address is a valid IPv4 address, false if it's a valid IPv6 address.
///
/// # Examples
///
/// ```
/// #![feature(ipaddr_checker)]
///
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// fn main() {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv4(), true);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv4(),
/// false);
/// }
/// ```
#[unstable(feature = "ipaddr_checker", issue = "36949")]
pub fn is_ipv4(&self) -> bool {
match *self {
Expand All @@ -141,6 +215,20 @@ impl IpAddr {
}

/// Returns true if this address is a valid IPv6 address, false if it's a valid IPv4 address.
///
/// # Examples
///
/// ```
/// #![feature(ipaddr_checker)]
///
/// use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
///
/// fn main() {
/// assert_eq!(IpAddr::V4(Ipv4Addr::new(203, 0, 113, 6)).is_ipv6(), false);
/// assert_eq!(IpAddr::V6(Ipv6Addr::new(0x2001, 0xdb8, 0, 0, 0, 0, 0, 0)).is_ipv6(),
/// true);
/// }
/// ```
#[unstable(feature = "ipaddr_checker", issue = "36949")]
pub fn is_ipv6(&self) -> bool {
match *self {
Expand Down

0 comments on commit 336e5dd

Please sign in to comment.