Skip to content

Commit b7261a1

Browse files
olsonjefferybrson
authored andcommitted
---
yaml --- r: 15968 b: refs/heads/try c: d99b7bc h: refs/heads/master v: v3
1 parent 7770521 commit b7261a1

File tree

4 files changed

+81
-48
lines changed

4 files changed

+81
-48
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
refs/heads/master: 61b1875c16de39c166b0f4d54bba19f9c6777d1a
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 4a81779abd786ff22d71434c6d9a5917ea4cdfff
5-
refs/heads/try: ffdaf14dd90995273fd697b9b87018d5dba84379
5+
refs/heads/try: d99b7bcb2f0c8163645429bb6ba40ab6761a6ce2
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105

branches/try/src/libstd/net.rs

Lines changed: 5 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,7 @@
1-
import vec;
2-
import uint;
1+
#[doc="
2+
Top-level module for network-related functionality
3+
"];
34

4-
#[doc = "An IP address"]
5-
enum ip_addr {
6-
/*
7-
Variant: ipv4
85

9-
An IPv4 address
10-
*/
11-
ipv4(u8, u8, u8, u8),
12-
}
13-
14-
#[doc = "Convert an `ip_addr` to a str"]
15-
fn format_addr(ip: ip_addr) -> str {
16-
alt ip {
17-
ipv4(a, b, c, d) {
18-
#fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint]
19-
}
20-
}
21-
}
22-
23-
#[doc = "
24-
Convert a str to `ip_addr`
25-
26-
Converts a string of the format `x.x.x.x` into an ip_addr enum.
27-
28-
Fails if the string is not a valid IPv4 address
29-
"]
30-
fn parse_addr(ip: str) -> ip_addr {
31-
let parts = vec::map(str::split_char(ip, '.'), {|s|
32-
alt uint::from_str(s) {
33-
some(n) if n <= 255u { n }
34-
_ { fail "Invalid IP Address part." }
35-
}
36-
});
37-
if vec::len(parts) != 4u { fail "Too many dots in IP address"; }
38-
ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8)
39-
}
40-
41-
#[test]
42-
fn test_format_ip() {
43-
assert (net::format_addr(net::ipv4(127u8, 0u8, 0u8, 1u8)) == "127.0.0.1")
44-
}
45-
46-
#[test]
47-
fn test_parse_ip() {
48-
assert (net::parse_addr("127.0.0.1") == net::ipv4(127u8, 0u8, 0u8, 1u8));
49-
}
6+
import ip = net_ip;
7+
export ip;

branches/try/src/libstd/net_ip.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#[doc="
2+
Types/fns concerning Internet Protocol (IP), versions 4 & 6
3+
"];
4+
5+
import vec;
6+
import uint;
7+
8+
export ip_addr;
9+
export v4;
10+
//format_addr, parse_addr;
11+
12+
#[doc = "An IP address"]
13+
enum ip_addr {
14+
#[doc="An IPv4 address"]
15+
ipv4(u8, u8, u8, u8),
16+
}
17+
18+
#[doc="
19+
Convert a `ip_addr` to a str
20+
21+
# Arguments
22+
23+
* ip - a `std::net::ip::ip_addr`
24+
"]
25+
fn format_addr(ip: ip_addr) -> str {
26+
alt ip {
27+
ipv4(a, b, c, d) {
28+
#fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint]
29+
}
30+
}
31+
}
32+
33+
mod v4 {
34+
#[doc = "
35+
Convert a str to `ip_addr`
36+
37+
# Failure
38+
39+
j Fails if the string is not a valid IPv4 address
40+
41+
# Arguments
42+
43+
* ip - a string of the format `x.x.x.x`
44+
45+
# Returns
46+
47+
* an `ip_addr` of the `ipv4` variant
48+
"]
49+
fn parse_addr(ip: str) -> ip_addr {
50+
let parts = vec::map(str::split_char(ip, '.'), {|s|
51+
alt uint::from_str(s) {
52+
some(n) if n <= 255u { n }
53+
_ { fail "Invalid IP Address part." }
54+
}
55+
});
56+
if vec::len(parts) != 4u { fail "Too many dots in IP address"; }
57+
ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8)
58+
}
59+
}
60+
61+
#[cfg(test)]
62+
mod test {
63+
#[test]
64+
fn test_format_ip() {
65+
assert (format_addr(ipv4(127u8, 0u8, 0u8, 1u8))
66+
== "127.0.0.1")
67+
}
68+
69+
#[test]
70+
fn test_parse_ip() {
71+
assert (v4::parse_addr("127.0.0.1") ==
72+
ipv4(127u8, 0u8, 0u8, 1u8));
73+
}
74+
}

branches/try/src/libstd/std.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export test, tempfile, serialization;
2323
// General io and system-services modules
2424

2525
mod net;
26+
mod net_ip;
2627
mod net_tcp;
2728

2829
// libuv modules

0 commit comments

Comments
 (0)