Skip to content

Commit

Permalink
impl FromStr for IpAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
murarth committed Mar 26, 2015
1 parent 1501f33 commit 1c43e53
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/libstd/net/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@
use prelude::v1::*;

use str::FromStr;
use net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};
use net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6};

struct Parser<'a> {
// parsing as ASCII, so can use byte array
s: &'a [u8],
pos: usize,
}

enum IpAddr {
V4(Ipv4Addr),
V6(Ipv6Addr),
}

impl<'a> Parser<'a> {
fn new(s: &'a str) -> Parser<'a> {
Parser {
Expand Down Expand Up @@ -296,6 +291,17 @@ impl<'a> Parser<'a> {
}
}

#[unstable(feature = "ip_addr", reason = "recent addition")]
impl FromStr for IpAddr {
type Err = AddrParseError;
fn from_str(s: &str) -> Result<IpAddr, AddrParseError> {
match Parser::new(s).read_till_eof(|p| p.read_ip_addr()) {
Some(s) => Ok(s),
None => Err(AddrParseError(()))
}
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl FromStr for Ipv4Addr {
type Err = AddrParseError;
Expand Down

0 comments on commit 1c43e53

Please sign in to comment.