1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//! Networking primitives
//!
use std::net::AddrParseError;
use std::str::FromStr;

pub mod tcp;
pub mod udp;

#[cfg(unix)]
pub mod unix;

/// An IP address, either a IPv4 or IPv6 address.
///
/// Once `std::net::IpAddr` is stable, this will go away.
pub enum IpAddr {
    V4(Ipv4Addr),
    V6(Ipv6Addr),
}

pub use std::net::Ipv4Addr;
pub use std::net::Ipv6Addr;

impl FromStr for IpAddr {
    type Err = AddrParseError;

    fn from_str(s: &str) -> Result<IpAddr, AddrParseError> {
        s.parse()
            .map(|ip: Ipv4Addr| IpAddr::V4(ip))
            .or_else(|_| {
                s.parse().map(|ip: Ipv6Addr| IpAddr::V6(ip))
            })
    }
}