Skip to content

high-level tcp bindings for std #2409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 30 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
deba9eb
initial stab at API for std::net::tcp
olsonjeffery Apr 26, 2012
ad35511
std: pushing existing code in net.rs -> net_ip.rs and re-import/expor…
olsonjeffery Apr 30, 2012
4dc697a
std: misc cleanup for uv::ll
olsonjeffery Apr 30, 2012
0f071a8
std: export net::ip::format_addr
olsonjeffery Apr 30, 2012
c5cf2bc
std: impl for net::tcp::connect
olsonjeffery May 1, 2012
c6c870a
std: tweak uv::ll::write signature and make it generic/more flexible
olsonjeffery May 1, 2012
7cc0646
std: impl of net::tcp::write and make net::tcp::tcp_socket a resource
olsonjeffery May 1, 2012
703121e
std: impl for high-level tcp client/request workflow
olsonjeffery May 2, 2012
71b68ca
std: no longer return uv::ll::err_data records from net::tcp
olsonjeffery May 2, 2012
d0ca3f7
core: add result::unwrap() .. patch from @nmatsakis
olsonjeffery May 2, 2012
959362a
std: change tcp_*_result to use result::result.. flatter!
olsonjeffery May 2, 2012
b8f3f5d
std: makeing uv::ll::listen/accept use generic params for ptr args
olsonjeffery May 5, 2012
9706fba
std: FIXME stub net::ip::ip_addr::ipv6 variant...needs parse/format impl
olsonjeffery May 5, 2012
f0a9a67
std: first-pass at a tcp server API, with a basic (non-robust) test
olsonjeffery May 5, 2012
d99c7a6
std: change sig of uv::ll::accept, again.
olsonjeffery May 8, 2012
6ddf458
std: tightening up net::tcp, server/client test done, still has races..
olsonjeffery May 9, 2012
63f7ecd
rt: adding rust_uv_* binding for kernel malloc and free'ing :/
olsonjeffery May 14, 2012
d5ed0cb
std: reworking how some net and libuv modules are exported in the rc
olsonjeffery May 14, 2012
adffc3c
std: splitting out tcp server API WIP
olsonjeffery May 16, 2012
8a54ee9
std: splitting out tcp server API + tests
olsonjeffery May 17, 2012
33ec1cb
std: ignoring timer test that seems to be race-failing b/c valgrind
olsonjeffery May 18, 2012
a6ae810
std: more docs and some methods for types in net::tcp
olsonjeffery May 18, 2012
fadff9f
core: doc/err feedback tweeks for result::unwrap
olsonjeffery May 19, 2012
0cabb2f
std: add try_parse_addr and change an alt w/ ip_addr::ipv6 to avoid w…
olsonjeffery May 19, 2012
7ac7571
std: several minor cleanups wrt codereview.. see extended comments
olsonjeffery May 21, 2012
b86839e
std: adding tcp::write_future for non-block tcp writes, docs cleanup
olsonjeffery May 21, 2012
133f52b
std:: adding tcp::read fn as simple, blocking read operation, akin to…
olsonjeffery May 21, 2012
a97b9f6
std: more work on uv tests to endure valgrind's machinations against …
olsonjeffery May 22, 2012
b561469
std: high-level libuv-leverage APIs now take a hl_loop as arg (tcp/ti…
olsonjeffery May 22, 2012
f77892b
std: warning cleanup
olsonjeffery May 23, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/libcore/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,19 @@ fn iter2<S,T,U:copy>(ss: [S], ts: [T],
ret ok(());
}

#[doc="
Unwraps a result, assuming it is an `ok(T)`
"]
fn unwrap<T, U>(-res: result<T, U>) -> T unsafe {
let addr = alt res {
ok(x) { ptr::addr_of(x) }
err(_) { fail "error result" }
};
let liberated_value = unsafe::reinterpret_cast(*addr);
unsafe::forget(res);
ret liberated_value;
}

#[cfg(test)]
mod tests {
fn op1() -> result::result<int, str> { result::ok(666) }
Expand Down
54 changes: 7 additions & 47 deletions src/libstd/net.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,9 @@
import vec;
import uint;
#[doc="
Top-level module for network-related functionality
"];

#[doc = "An IP address"]
enum ip_addr {
/*
Variant: ipv4
import tcp = net_tcp;
export tcp;

An IPv4 address
*/
ipv4(u8, u8, u8, u8),
}

#[doc = "Convert an `ip_addr` to a str"]
fn format_addr(ip: ip_addr) -> str {
alt ip {
ipv4(a, b, c, d) {
#fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint]
}
}
}

#[doc = "
Convert a str to `ip_addr`

Converts a string of the format `x.x.x.x` into an ip_addr enum.

Fails if the string is not a valid IPv4 address
"]
fn parse_addr(ip: str) -> ip_addr {
let parts = vec::map(str::split_char(ip, '.'), {|s|
alt uint::from_str(s) {
some(n) if n <= 255u { n }
_ { fail "Invalid IP Address part." }
}
});
if vec::len(parts) != 4u { fail "Too many dots in IP address"; }
ipv4(parts[0] as u8, parts[1] as u8, parts[2] as u8, parts[3] as u8)
}

#[test]
fn test_format_ip() {
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));
}
import ip = net_ip;
export ip;
103 changes: 103 additions & 0 deletions src/libstd/net_ip.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#[doc="
Types/fns concerning Internet Protocol (IP), versions 4 & 6
"];

import vec;
import uint;

export ip_addr, parse_addr_err;
export format_addr;
export v4;

#[doc = "An IP address"]
enum ip_addr {
#[doc="An IPv4 address"]
ipv4(u8, u8, u8, u8),
ipv6(u16,u16,u16,u16,u16,u16,u16,u16)
}

#[doc="
Human-friendly feedback on why a parse_addr attempt failed
"]
type parse_addr_err = {
err_msg: str
};

#[doc="
Convert a `ip_addr` to a str

# Arguments

* ip - a `std::net::ip::ip_addr`
"]
fn format_addr(ip: ip_addr) -> str {
alt ip {
ipv4(a, b, c, d) {
#fmt["%u.%u.%u.%u", a as uint, b as uint, c as uint, d as uint]
}
ipv6(_, _, _, _, _, _, _, _) {
fail "FIXME impl parsing of ipv6 addr";
}
}
}

mod v4 {
#[doc = "
Convert a str to `ip_addr`

# Failure

j Fails if the string is not a valid IPv4 address

# Arguments

* ip - a string of the format `x.x.x.x`

# Returns

* an `ip_addr` of the `ipv4` variant
"]
fn parse_addr(ip: str) -> ip_addr {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to have a result version of this too.

alt try_parse_addr(ip) {
result::ok(addr) { addr }
result::err(err_data) {
fail err_data.err_msg
}
}
}
fn try_parse_addr(ip: str) -> result::result<ip_addr,parse_addr_err> {
let parts = vec::map(str::split_char(ip, '.'), {|s|
alt uint::from_str(s) {
some(n) if n <= 255u { n }
_ { 256u }
}
});
if vec::len(parts) != 4u {
result::err({err_msg: #fmt("'%s' doesn't have 4 parts",
ip)})
}
else if vec::contains(parts, 256u) {
result::err({err_msg: #fmt("invalid octal in provided addr '%s'",
ip)})
}
else {
result::ok(ipv4(parts[0] as u8, parts[1] as u8,
parts[2] as u8, parts[3] as u8))
}
}
}

#[cfg(test)]
mod test {
#[test]
fn test_format_ip() {
assert (format_addr(ipv4(127u8, 0u8, 0u8, 1u8))
== "127.0.0.1")
}

#[test]
fn test_parse_ip() {
assert (v4::parse_addr("127.0.0.1") ==
ipv4(127u8, 0u8, 0u8, 1u8));
}
}
Loading