Skip to content

Commit ce97bd4

Browse files
author
Eric Reed
committed
cleaned up uv/net
1 parent 87ecfb7 commit ce97bd4

File tree

1 file changed

+32
-68
lines changed

1 file changed

+32
-68
lines changed

src/libstd/rt/uv/net.rs

Lines changed: 32 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub fn ip4_as_uv_ip4<T>(addr: IpAddr, f: &fn(*sockaddr_in) -> T) -> T {
4444
pub fn uv_ip4_to_ip4(addr: *sockaddr_in) -> IpAddr {
4545
let ip4_size = 16;
4646
let buf = vec::from_elem(ip4_size + 1 /*null terminated*/, 0u8);
47-
unsafe { ip4_name(addr, vec::raw::to_ptr(buf), ip4_size as u64) };
48-
let port = unsafe { ip4_port(addr) };
47+
unsafe { uvll::ip4_name(addr, vec::raw::to_ptr(buf), ip4_size as u64) };
48+
let port = unsafe { uvll::ip4_port(addr) };
4949
let ip_str = str::from_bytes_slice(buf).trim_right_chars(&'\x00');
5050
let ip: ~[u8] = ip_str.split_iter('.')
5151
.transform(|s: &str| -> u8 {
@@ -71,22 +71,19 @@ impl StreamWatcher {
7171
data.read_cb = Some(cb);
7272
}
7373

74-
let handle = self.native_handle();
75-
unsafe { uvll::read_start(handle, alloc_cb, read_cb); }
74+
unsafe { uvll::read_start(self.native_handle(), alloc_cb, read_cb); }
7675

7776
extern fn alloc_cb(stream: *uvll::uv_stream_t, suggested_size: size_t) -> Buf {
7877
let mut stream_watcher: StreamWatcher = NativeHandle::from_native_handle(stream);
79-
let data = stream_watcher.get_watcher_data();
80-
let alloc_cb = data.alloc_cb.get_ref();
78+
let alloc_cb = stream_watcher.get_watcher_data().alloc_cb.get_ref();
8179
return (*alloc_cb)(suggested_size as uint);
8280
}
8381

8482
extern fn read_cb(stream: *uvll::uv_stream_t, nread: ssize_t, buf: Buf) {
8583
rtdebug!("buf addr: %x", buf.base as uint);
8684
rtdebug!("buf len: %d", buf.len as int);
8785
let mut stream_watcher: StreamWatcher = NativeHandle::from_native_handle(stream);
88-
let data = stream_watcher.get_watcher_data();
89-
let cb = data.read_cb.get_ref();
86+
let cb = stream_watcher.get_watcher_data().read_cb.get_ref();
9087
let status = status_to_maybe_uv_error(stream, nread as c_int);
9188
(*cb)(stream_watcher, nread as int, buf, status);
9289
}
@@ -108,22 +105,15 @@ impl StreamWatcher {
108105
}
109106

110107
let req = WriteRequest::new();
111-
let bufs = [buf];
112108
unsafe {
113-
assert!(0 == uvll::write(req.native_handle(),
114-
self.native_handle(),
115-
bufs, write_cb));
109+
assert_eq!(0, uvll::write(req.native_handle(), self.native_handle(), [buf], write_cb));
116110
}
117111

118112
extern fn write_cb(req: *uvll::uv_write_t, status: c_int) {
119113
let write_request: WriteRequest = NativeHandle::from_native_handle(req);
120114
let mut stream_watcher = write_request.stream();
121115
write_request.delete();
122-
let cb = {
123-
let data = stream_watcher.get_watcher_data();
124-
let cb = data.write_cb.swap_unwrap();
125-
cb
126-
};
116+
let cb = stream_watcher.get_watcher_data().write_cb.swap_unwrap();
127117
let status = status_to_maybe_uv_error(stream_watcher.native_handle(), status);
128118
cb(stream_watcher, status);
129119
}
@@ -132,9 +122,7 @@ impl StreamWatcher {
132122
pub fn accept(&mut self, stream: StreamWatcher) {
133123
let self_handle = self.native_handle() as *c_void;
134124
let stream_handle = stream.native_handle() as *c_void;
135-
unsafe {
136-
assert_eq!(0, uvll::accept(self_handle, stream_handle));
137-
}
125+
assert_eq!(0, unsafe { uvll::accept(self_handle, stream_handle) } );
138126
}
139127

140128
pub fn close(self, cb: NullCallback) {
@@ -149,19 +137,15 @@ impl StreamWatcher {
149137

150138
extern fn close_cb(handle: *uvll::uv_stream_t) {
151139
let mut stream_watcher: StreamWatcher = NativeHandle::from_native_handle(handle);
152-
{
153-
let data = stream_watcher.get_watcher_data();
154-
data.close_cb.swap_unwrap()();
155-
}
140+
stream_watcher.get_watcher_data().close_cb.swap_unwrap()();
156141
stream_watcher.drop_watcher_data();
157142
unsafe { free_handle(handle as *c_void) }
158143
}
159144
}
160145
}
161146

162147
impl NativeHandle<*uvll::uv_stream_t> for StreamWatcher {
163-
fn from_native_handle(
164-
handle: *uvll::uv_stream_t) -> StreamWatcher {
148+
fn from_native_handle(handle: *uvll::uv_stream_t) -> StreamWatcher {
165149
StreamWatcher(handle)
166150
}
167151
fn native_handle(&self) -> *uvll::uv_stream_t {
@@ -188,9 +172,7 @@ impl TcpWatcher {
188172
match address {
189173
Ipv4(*) => {
190174
do ip4_as_uv_ip4(address) |addr| {
191-
let result = unsafe {
192-
uvll::tcp_bind(self.native_handle(), addr)
193-
};
175+
let result = unsafe { uvll::tcp_bind(self.native_handle(), addr) };
194176
if result == 0 {
195177
Ok(())
196178
} else {
@@ -212,9 +194,9 @@ impl TcpWatcher {
212194
Ipv4(*) => {
213195
do ip4_as_uv_ip4(address) |addr| {
214196
rtdebug!("connect_t: %x", connect_handle as uint);
215-
assert!(0 == uvll::tcp_connect(connect_handle,
216-
self.native_handle(),
217-
addr, connect_cb));
197+
assert_eq!(0,
198+
uvll::tcp_connect(connect_handle, self.native_handle(),
199+
addr, connect_cb));
218200
}
219201
}
220202
_ => fail!()
@@ -225,10 +207,7 @@ impl TcpWatcher {
225207
let connect_request: ConnectRequest = NativeHandle::from_native_handle(req);
226208
let mut stream_watcher = connect_request.stream();
227209
connect_request.delete();
228-
let cb: ConnectionCallback = {
229-
let data = stream_watcher.get_watcher_data();
230-
data.connect_cb.swap_unwrap()
231-
};
210+
let cb = stream_watcher.get_watcher_data().connect_cb.swap_unwrap();
232211
let status = status_to_maybe_uv_error(stream_watcher.native_handle(), status);
233212
cb(stream_watcher, status);
234213
}
@@ -245,15 +224,13 @@ impl TcpWatcher {
245224
unsafe {
246225
static BACKLOG: c_int = 128; // XXX should be configurable
247226
// XXX: This can probably fail
248-
assert!(0 == uvll::listen(self.native_handle(),
249-
BACKLOG, connection_cb));
227+
assert_eq!(0, uvll::listen(self.native_handle(), BACKLOG, connection_cb));
250228
}
251229

252230
extern fn connection_cb(handle: *uvll::uv_stream_t, status: c_int) {
253231
rtdebug!("connection_cb");
254232
let mut stream_watcher: StreamWatcher = NativeHandle::from_native_handle(handle);
255-
let data = stream_watcher.get_watcher_data();
256-
let cb = data.connect_cb.get_ref();
233+
let cb = stream_watcher.get_watcher_data().connect_cb.get_ref();
257234
let status = status_to_maybe_uv_error(handle, status);
258235
(*cb)(stream_watcher, status);
259236
}
@@ -314,8 +291,7 @@ impl UdpWatcher {
314291
data.udp_recv_cb = Some(cb);
315292
}
316293

317-
let handle = self.native_handle();
318-
unsafe { uvll::udp_recv_start(handle, alloc_cb, recv_cb); }
294+
unsafe { uvll::udp_recv_start(self.native_handle(), alloc_cb, recv_cb); }
319295

320296
extern fn alloc_cb(handle: *uvll::uv_udp_t, suggested_size: size_t) -> Buf {
321297
let mut udp_watcher: UdpWatcher = NativeHandle::from_native_handle(handle);
@@ -331,17 +307,14 @@ impl UdpWatcher {
331307
rtdebug!("buf addr: %x", buf.base as uint);
332308
rtdebug!("buf len: %d", buf.len as int);
333309
let mut udp_watcher: UdpWatcher = NativeHandle::from_native_handle(handle);
334-
let data = udp_watcher.get_watcher_data();
335-
let cb = data.udp_recv_cb.get_ref();
310+
let cb = udp_watcher.get_watcher_data().udp_recv_cb.get_ref();
336311
let status = status_to_maybe_uv_error(handle, nread as c_int);
337-
let address = uv_ip4_to_ip4(addr);
338-
(*cb)(udp_watcher, nread as int, buf, address, flags as uint, status);
312+
(*cb)(udp_watcher, nread as int, buf, uv_ip4_to_ip4(addr), flags as uint, status);
339313
}
340314
}
341315

342316
pub fn recv_stop(&self) {
343-
let handle = self.native_handle();
344-
unsafe { uvll::udp_recv_stop(handle); }
317+
unsafe { uvll::udp_recv_stop(self.native_handle()); }
345318
}
346319

347320
pub fn send(&self, buf: Buf, address: IpAddr, cb: UdpSendCallback) {
@@ -357,7 +330,7 @@ impl UdpWatcher {
357330
Ipv4(*) => {
358331
do ip4_as_uv_ip4(address) |addr| {
359332
unsafe {
360-
assert!(0 == uvll::udp_send(req.native_handle(),
333+
assert_eq!(0, uvll::udp_send(req.native_handle(),
361334
self.native_handle(),
362335
[buf], addr, send_cb));
363336
}
@@ -411,12 +384,9 @@ impl Request for ConnectRequest { }
411384
impl ConnectRequest {
412385

413386
fn new() -> ConnectRequest {
414-
let connect_handle = unsafe {
415-
malloc_req(UV_CONNECT)
416-
};
387+
let connect_handle = unsafe { malloc_req(UV_CONNECT) };
417388
assert!(connect_handle.is_not_null());
418-
let connect_handle = connect_handle as *uvll::uv_connect_t;
419-
ConnectRequest(connect_handle)
389+
ConnectRequest(connect_handle as *uvll::uv_connect_t)
420390
}
421391

422392
fn stream(&self) -> StreamWatcher {
@@ -432,8 +402,7 @@ impl ConnectRequest {
432402
}
433403

434404
impl NativeHandle<*uvll::uv_connect_t> for ConnectRequest {
435-
fn from_native_handle(
436-
handle: *uvll:: uv_connect_t) -> ConnectRequest {
405+
fn from_native_handle(handle: *uvll:: uv_connect_t) -> ConnectRequest {
437406
ConnectRequest(handle)
438407
}
439408
fn native_handle(&self) -> *uvll::uv_connect_t {
@@ -447,12 +416,9 @@ impl Request for WriteRequest { }
447416

448417
impl WriteRequest {
449418
pub fn new() -> WriteRequest {
450-
let write_handle = unsafe {
451-
malloc_req(UV_WRITE)
452-
};
419+
let write_handle = unsafe { malloc_req(UV_WRITE) };
453420
assert!(write_handle.is_not_null());
454-
let write_handle = write_handle as *uvll::uv_write_t;
455-
WriteRequest(write_handle)
421+
WriteRequest(write_handle as *uvll::uv_write_t)
456422
}
457423

458424
pub fn stream(&self) -> StreamWatcher {
@@ -483,16 +449,14 @@ impl UdpSendRequest {
483449
pub fn new() -> UdpSendRequest {
484450
let send_handle = unsafe { malloc_req(UV_UDP_SEND) };
485451
assert!(send_handle.is_not_null());
486-
let send_handle = send_handle as *uvll::uv_udp_send_t;
487-
UdpSendRequest(send_handle)
452+
UdpSendRequest(send_handle as *uvll::uv_udp_send_t)
488453
}
489454

490455
pub fn handle(&self) -> UdpWatcher {
491-
unsafe {
492-
NativeHandle::from_native_handle(
493-
uvll::get_udp_handle_from_send_req(
494-
self.native_handle()))
495-
}
456+
let send_request_handle = unsafe {
457+
uvll::get_udp_handle_from_send_req(self.native_handle())
458+
};
459+
NativeHandle::from_native_handle(send_request_handle)
496460
}
497461

498462
pub fn delete(self) {

0 commit comments

Comments
 (0)