@@ -44,8 +44,8 @@ pub fn ip4_as_uv_ip4<T>(addr: IpAddr, f: &fn(*sockaddr_in) -> T) -> T {
44
44
pub fn uv_ip4_to_ip4 ( addr : * sockaddr_in ) -> IpAddr {
45
45
let ip4_size = 16 ;
46
46
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) } ;
49
49
let ip_str = str:: from_bytes_slice ( buf) . trim_right_chars ( & '\x00' ) ;
50
50
let ip: ~[ u8 ] = ip_str. split_iter ( '.' )
51
51
. transform ( |s : & str | -> u8 {
@@ -71,22 +71,19 @@ impl StreamWatcher {
71
71
data. read_cb = Some ( cb) ;
72
72
}
73
73
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) ; }
76
75
77
76
extern fn alloc_cb ( stream : * uvll:: uv_stream_t , suggested_size : size_t ) -> Buf {
78
77
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 ( ) ;
81
79
return ( * alloc_cb) ( suggested_size as uint ) ;
82
80
}
83
81
84
82
extern fn read_cb ( stream : * uvll:: uv_stream_t , nread : ssize_t , buf : Buf ) {
85
83
rtdebug ! ( "buf addr: %x" , buf. base as uint) ;
86
84
rtdebug ! ( "buf len: %d" , buf. len as int) ;
87
85
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 ( ) ;
90
87
let status = status_to_maybe_uv_error ( stream, nread as c_int ) ;
91
88
( * cb) ( stream_watcher, nread as int , buf, status) ;
92
89
}
@@ -108,22 +105,15 @@ impl StreamWatcher {
108
105
}
109
106
110
107
let req = WriteRequest :: new ( ) ;
111
- let bufs = [ buf] ;
112
108
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) ) ;
116
110
}
117
111
118
112
extern fn write_cb ( req : * uvll:: uv_write_t , status : c_int ) {
119
113
let write_request: WriteRequest = NativeHandle :: from_native_handle ( req) ;
120
114
let mut stream_watcher = write_request. stream ( ) ;
121
115
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 ( ) ;
127
117
let status = status_to_maybe_uv_error ( stream_watcher. native_handle ( ) , status) ;
128
118
cb ( stream_watcher, status) ;
129
119
}
@@ -132,9 +122,7 @@ impl StreamWatcher {
132
122
pub fn accept ( & mut self , stream : StreamWatcher ) {
133
123
let self_handle = self . native_handle ( ) as * c_void ;
134
124
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) } ) ;
138
126
}
139
127
140
128
pub fn close ( self , cb : NullCallback ) {
@@ -149,19 +137,15 @@ impl StreamWatcher {
149
137
150
138
extern fn close_cb ( handle : * uvll:: uv_stream_t ) {
151
139
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 ( ) ( ) ;
156
141
stream_watcher. drop_watcher_data ( ) ;
157
142
unsafe { free_handle ( handle as * c_void ) }
158
143
}
159
144
}
160
145
}
161
146
162
147
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 {
165
149
StreamWatcher ( handle)
166
150
}
167
151
fn native_handle ( & self ) -> * uvll:: uv_stream_t {
@@ -188,9 +172,7 @@ impl TcpWatcher {
188
172
match address {
189
173
Ipv4 ( * ) => {
190
174
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) } ;
194
176
if result == 0 {
195
177
Ok ( ( ) )
196
178
} else {
@@ -212,9 +194,9 @@ impl TcpWatcher {
212
194
Ipv4 ( * ) => {
213
195
do ip4_as_uv_ip4 ( address) |addr| {
214
196
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) ) ;
218
200
}
219
201
}
220
202
_ => fail ! ( )
@@ -225,10 +207,7 @@ impl TcpWatcher {
225
207
let connect_request: ConnectRequest = NativeHandle :: from_native_handle ( req) ;
226
208
let mut stream_watcher = connect_request. stream ( ) ;
227
209
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 ( ) ;
232
211
let status = status_to_maybe_uv_error ( stream_watcher. native_handle ( ) , status) ;
233
212
cb ( stream_watcher, status) ;
234
213
}
@@ -245,15 +224,13 @@ impl TcpWatcher {
245
224
unsafe {
246
225
static BACKLOG : c_int = 128 ; // XXX should be configurable
247
226
// 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) ) ;
250
228
}
251
229
252
230
extern fn connection_cb ( handle : * uvll:: uv_stream_t , status : c_int ) {
253
231
rtdebug ! ( "connection_cb" ) ;
254
232
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 ( ) ;
257
234
let status = status_to_maybe_uv_error ( handle, status) ;
258
235
( * cb) ( stream_watcher, status) ;
259
236
}
@@ -314,8 +291,7 @@ impl UdpWatcher {
314
291
data. udp_recv_cb = Some ( cb) ;
315
292
}
316
293
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) ; }
319
295
320
296
extern fn alloc_cb ( handle : * uvll:: uv_udp_t , suggested_size : size_t ) -> Buf {
321
297
let mut udp_watcher: UdpWatcher = NativeHandle :: from_native_handle ( handle) ;
@@ -331,17 +307,14 @@ impl UdpWatcher {
331
307
rtdebug ! ( "buf addr: %x" , buf. base as uint) ;
332
308
rtdebug ! ( "buf len: %d" , buf. len as int) ;
333
309
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 ( ) ;
336
311
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) ;
339
313
}
340
314
}
341
315
342
316
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 ( ) ) ; }
345
318
}
346
319
347
320
pub fn send ( & self , buf : Buf , address : IpAddr , cb : UdpSendCallback ) {
@@ -357,7 +330,7 @@ impl UdpWatcher {
357
330
Ipv4 ( * ) => {
358
331
do ip4_as_uv_ip4 ( address) |addr| {
359
332
unsafe {
360
- assert ! ( 0 == uvll:: udp_send( req. native_handle( ) ,
333
+ assert_eq ! ( 0 , uvll:: udp_send( req. native_handle( ) ,
361
334
self . native_handle( ) ,
362
335
[ buf] , addr, send_cb) ) ;
363
336
}
@@ -411,12 +384,9 @@ impl Request for ConnectRequest { }
411
384
impl ConnectRequest {
412
385
413
386
fn new ( ) -> ConnectRequest {
414
- let connect_handle = unsafe {
415
- malloc_req ( UV_CONNECT )
416
- } ;
387
+ let connect_handle = unsafe { malloc_req ( UV_CONNECT ) } ;
417
388
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 )
420
390
}
421
391
422
392
fn stream ( & self ) -> StreamWatcher {
@@ -432,8 +402,7 @@ impl ConnectRequest {
432
402
}
433
403
434
404
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 {
437
406
ConnectRequest ( handle)
438
407
}
439
408
fn native_handle ( & self ) -> * uvll:: uv_connect_t {
@@ -447,12 +416,9 @@ impl Request for WriteRequest { }
447
416
448
417
impl WriteRequest {
449
418
pub fn new ( ) -> WriteRequest {
450
- let write_handle = unsafe {
451
- malloc_req ( UV_WRITE )
452
- } ;
419
+ let write_handle = unsafe { malloc_req ( UV_WRITE ) } ;
453
420
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 )
456
422
}
457
423
458
424
pub fn stream ( & self ) -> StreamWatcher {
@@ -483,16 +449,14 @@ impl UdpSendRequest {
483
449
pub fn new ( ) -> UdpSendRequest {
484
450
let send_handle = unsafe { malloc_req ( UV_UDP_SEND ) } ;
485
451
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 )
488
453
}
489
454
490
455
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)
496
460
}
497
461
498
462
pub fn delete ( self ) {
0 commit comments