Why are the ip parsers in ::std::net private?

The traits/structs/fns that parse strings into IPv4, v6 and socket addresses are all private.

It would be nice if these weren’t so developers could leverage them.

For instance, I would like to create a parser that can parse both IpAddrs and SocketAddrs (without the :port or with the :port). All the pieces to do this are there in std::net::parsers, but they are private.

I mean I can do something hacky like

pub fn parse_ip_and_port(addr: &str) -> Result<(IpAddr, u16), &str> {
    let sock_addr: Result<SocketAddr, AddrParseError> = addr.parse();
    let mut res = sock_addr.map_err(|err| "err.description()")
        .map(|sock| (sock.ip(), sock.port()));
    if res.is_err() {
        let ip_addr: Result<IpAddr, AddrParseError> = addr.parse();
        res = ip_addr.map_err(|err| "err.description()")
            .map(|ip| (ip, 0));

or some such, but it would be more efficient if I could get at some of the parser impls…

You can write your function like this:

pub fn parse_ip_and_port(addr: &str) -> Result<(IpAddr, u16), MyError> {
        .map(|sock| (sock.ip(), sock.port()))
        .or_else(|_| addr.parse().map(|ip| (ip, 0)))
        .map_err(|_| MyError)