Convert std::net to istrs. Issue #855

This commit is contained in:
Brian Anderson 2011-08-27 23:35:16 -07:00 committed by Brian Anderson
parent 0abec867c3
commit c94d4cff03
7 changed files with 21 additions and 17 deletions

View File

@ -45,7 +45,7 @@ tag request {
type ctx = chan<request>;
fn ip_to_sbuf(ip: net::ip_addr) -> *u8 {
vec::unsafe::to_ptr(str::bytes(net::format_addr(ip)))
vec::to_ptr(istr::bytes(net::format_addr(ip)))
}
fn connect_task(ip: net::ip_addr, portnum: int, evt: chan<socket_event>) {

View File

@ -4,19 +4,20 @@ import uint;
tag ip_addr { ipv4(u8, u8, u8, u8); }
fn format_addr(ip: ip_addr) -> str {
fn format_addr(ip: ip_addr) -> istr {
alt ip {
ipv4(a, b, c, d) {
#fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint]
istr::from_estr(
#fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint])
}
_ { fail "Unsupported address type"; }
}
}
fn parse_addr(ip: str) -> ip_addr {
fn parse_addr(ip: &istr) -> ip_addr {
let parts = vec::map(
{ |&s| uint::from_str(istr::from_estr(s)) },
str::split(ip, "."[0]));
{ |&s| uint::from_str(s) },
istr::split(ip, ~"."[0]));
if vec::len(parts) != 4u { fail "Too many dots in IP address"; }
for i in parts { if i > 255u { fail "Invalid IP Address part."; } }
ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8)

View File

@ -14,10 +14,10 @@ fn connectTask(cx: sio::ctx, ip: net::ip_addr, portnum: int) {
fn main() {
let cx: sio::ctx = sio::new();
let srv: sio::server = sio::create_server(cx,
net::parse_addr("0.0.0.0"), 9090);
let srv: sio::server = sio::create_server(
cx, net::parse_addr(~"0.0.0.0"), 9090);
let child = task::_spawn(bind connectTask(cx,
net::parse_addr("127.0.0.1"),
net::parse_addr(~"127.0.0.1"),
9090));
let client: sio::client = sio::accept_from(srv);
task::join_id(child);

View File

@ -16,10 +16,10 @@ fn connectTask(cx: sio::ctx, ip: net::ip_addr, portnum: int) {
fn main() {
let cx: sio::ctx = sio::new();
let srv: sio::server = sio::create_server(cx,
net::parse_addr("0.0.0.0"), 9090);
let srv: sio::server = sio::create_server(
cx, net::parse_addr("~0.0.0.0"), 9090);
let child = task::_spawn(bind connectTask(cx,
net::parse_addr("127.0.0.1"),
net::parse_addr(~"127.0.0.1"),
9090));
let client: sio::client = sio::accept_from(srv);
sio::write_data(client, str::bytes("hello, world\n"));

View File

@ -7,7 +7,8 @@ import std::net;
fn main() {
let cx: sio::ctx = sio::new();
let srv: sio::server = sio::create_server(cx, net::parse_addr("127.0.0.1"),
let srv: sio::server = sio::create_server(cx,
net::parse_addr(~"127.0.0.1"),
9090);
sio::close_server(srv);
sio::destroy(cx);

View File

@ -15,9 +15,9 @@ fn connectTask(cx: sio::ctx, ip: net::ip_addr, portnum: int) {
fn main() {
let cx: sio::ctx = sio::new();
let srv: sio::server = sio::create_server(cx, net::parse_addr("0.0.0.0"),
let srv: sio::server = sio::create_server(cx, net::parse_addr(~"0.0.0.0"),
9090);
let child = task::_spawn(bind connectTask(cx, net::parse_addr("127.0.0.1"),
let child = task::_spawn(bind connectTask(cx, net::parse_addr(~"127.0.0.1"),
9090));
let client: sio::client = sio::accept_from(srv);
sio::write_data(client, str::bytes("hello, world\n"));

View File

@ -3,10 +3,12 @@ import std::net;
#[test]
fn test_format_ip() {
assert (net::format_addr(net::ipv4(127u8, 0u8, 0u8, 1u8)) == "127.0.0.1")
assert (net::format_addr(net::ipv4(
127u8, 0u8, 0u8, 1u8)) == ~"127.0.0.1")
}
#[test]
fn test_parse_ip() {
assert (net::parse_addr("127.0.0.1") == net::ipv4(127u8, 0u8, 0u8, 1u8));
assert (net::parse_addr(~"127.0.0.1")
== net::ipv4(127u8, 0u8, 0u8, 1u8));
}