Skip to content

Commit 97e95d1

Browse files
committed
Provide cleaner initialization of uv handles
1 parent 1850d26 commit 97e95d1

File tree

12 files changed

+82
-99
lines changed

12 files changed

+82
-99
lines changed

src/librustuv/addrinfo.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub fn accum_addrinfo(addr: &net::UvAddrInfo) -> ~[ai::Info] {
235235
}
236236
}
237237

238-
impl NativeHandle<*uvll::uv_getaddrinfo_t> for GetAddrInfoRequest {
238+
impl NativeHandle<uvll::uv_getaddrinfo_t> for GetAddrInfoRequest {
239239
fn from_native_handle(handle: *uvll::uv_getaddrinfo_t) -> GetAddrInfoRequest {
240240
GetAddrInfoRequest(handle)
241241
}

src/librustuv/async.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ impl Watcher for AsyncWatcher { }
1919

2020
impl AsyncWatcher {
2121
pub fn new(loop_: &mut Loop, cb: AsyncCallback) -> AsyncWatcher {
22-
unsafe {
23-
let handle = uvll::malloc_handle(uvll::UV_ASYNC);
24-
assert!(handle.is_not_null());
25-
let mut watcher: AsyncWatcher = NativeHandle::from_native_handle(handle);
26-
watcher.install_watcher_data();
27-
let data = watcher.get_watcher_data();
28-
data.async_cb = Some(cb);
29-
assert_eq!(0, uvll::uv_async_init(loop_.native_handle(), handle, async_cb));
30-
return watcher;
31-
}
22+
let mut watcher: AsyncWatcher = NativeHandle::alloc(uvll::UV_ASYNC);
23+
assert_eq!(unsafe {
24+
uvll::uv_async_init(loop_.native_handle(), *watcher, async_cb)
25+
}, 0);
26+
watcher.install_watcher_data();
27+
watcher.get_watcher_data().async_cb = Some(cb);
28+
return watcher;
3229

3330
extern fn async_cb(handle: *uvll::uv_async_t, status: c_int) {
3431
let mut watcher: AsyncWatcher = NativeHandle::from_native_handle(handle);
@@ -40,14 +37,11 @@ impl AsyncWatcher {
4037
}
4138

4239
pub fn send(&mut self) {
43-
unsafe {
44-
let handle = self.native_handle();
45-
uvll::uv_async_send(handle);
46-
}
40+
unsafe { uvll::uv_async_send(self.native_handle()) }
4741
}
4842
}
4943

50-
impl NativeHandle<*uvll::uv_async_t> for AsyncWatcher {
44+
impl NativeHandle<uvll::uv_async_t> for AsyncWatcher {
5145
fn from_native_handle(handle: *uvll::uv_async_t) -> AsyncWatcher {
5246
AsyncWatcher(handle)
5347
}

src/librustuv/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl FsRequest {
310310
}
311311
}
312312

313-
impl NativeHandle<*uvll::uv_fs_t> for FsRequest {
313+
impl NativeHandle<uvll::uv_fs_t> for FsRequest {
314314
fn from_native_handle(handle: *uvll:: uv_fs_t) -> FsRequest {
315315
FsRequest(handle)
316316
}

src/librustuv/idle.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ impl Watcher for IdleWatcher { }
1818

1919
impl IdleWatcher {
2020
pub fn new(loop_: &mut Loop) -> IdleWatcher {
21-
unsafe {
22-
let handle = uvll::malloc_handle(uvll::UV_IDLE);
23-
assert!(handle.is_not_null());
24-
assert_eq!(uvll::uv_idle_init(loop_.native_handle(), handle), 0);
25-
let mut watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
26-
watcher.install_watcher_data();
27-
return watcher
28-
}
21+
let mut watcher: IdleWatcher = NativeHandle::alloc(uvll::UV_IDLE);
22+
assert_eq!(unsafe {
23+
uvll::uv_idle_init(loop_.native_handle(), *watcher)
24+
}, 0);
25+
watcher.install_watcher_data();
26+
return watcher;
2927
}
3028

3129
pub fn start(&mut self, cb: IdleCallback) {
@@ -57,7 +55,7 @@ impl IdleWatcher {
5755
}
5856
}
5957

60-
impl NativeHandle<*uvll::uv_idle_t> for IdleWatcher {
58+
impl NativeHandle<uvll::uv_idle_t> for IdleWatcher {
6159
fn from_native_handle(handle: *uvll::uv_idle_t) -> IdleWatcher {
6260
IdleWatcher(handle)
6361
}

src/librustuv/lib.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub struct Loop {
9797
pub struct Handle(*uvll::uv_handle_t);
9898

9999
impl Watcher for Handle {}
100-
impl NativeHandle<*uvll::uv_handle_t> for Handle {
100+
impl NativeHandle<uvll::uv_handle_t> for Handle {
101101
fn from_native_handle(h: *uvll::uv_handle_t) -> Handle { Handle(h) }
102102
fn native_handle(&self) -> *uvll::uv_handle_t { **self }
103103
}
@@ -114,8 +114,15 @@ pub trait Request { }
114114

115115
/// A type that wraps a native handle
116116
pub trait NativeHandle<T> {
117-
fn from_native_handle(T) -> Self;
118-
fn native_handle(&self) -> T;
117+
fn from_native_handle(h: *T) -> Self;
118+
fn native_handle(&self) -> *T;
119+
120+
// FIXME(#8888) dummy self
121+
fn alloc(ty: uvll::uv_handle_type) -> Self {
122+
unsafe {
123+
NativeHandle::from_native_handle(uvll::malloc_handle(ty) as *T)
124+
}
125+
}
119126
}
120127

121128
impl Loop {
@@ -134,7 +141,7 @@ impl Loop {
134141
}
135142
}
136143

137-
impl NativeHandle<*uvll::uv_loop_t> for Loop {
144+
impl NativeHandle<uvll::uv_loop_t> for Loop {
138145
fn from_native_handle(handle: *uvll::uv_loop_t) -> Loop {
139146
Loop { handle: handle }
140147
}
@@ -185,7 +192,7 @@ pub trait WatcherInterop {
185192
fn close_async(self);
186193
}
187194

188-
impl<H, W: Watcher + NativeHandle<*H>> WatcherInterop for W {
195+
impl<H, W: Watcher + NativeHandle<H>> WatcherInterop for W {
189196
/// Get the uv event loop from a Watcher
190197
fn event_loop(&self) -> Loop {
191198
unsafe {

src/librustuv/net.rs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ impl StreamWatcher {
216216
}
217217
}
218218

219-
impl NativeHandle<*uvll::uv_stream_t> for StreamWatcher {
219+
impl NativeHandle<uvll::uv_stream_t> for StreamWatcher {
220220
fn from_native_handle(handle: *uvll::uv_stream_t) -> StreamWatcher {
221221
StreamWatcher(handle)
222222
}
@@ -230,14 +230,12 @@ impl Watcher for TcpWatcher { }
230230

231231
impl TcpWatcher {
232232
pub fn new(loop_: &Loop) -> TcpWatcher {
233-
unsafe {
234-
let handle = malloc_handle(UV_TCP);
235-
assert!(handle.is_not_null());
236-
assert_eq!(0, uvll::uv_tcp_init(loop_.native_handle(), handle));
237-
let mut watcher: TcpWatcher = NativeHandle::from_native_handle(handle);
238-
watcher.install_watcher_data();
239-
return watcher;
240-
}
233+
let mut watcher: TcpWatcher = NativeHandle::alloc(uvll::UV_TCP);
234+
assert_eq!(unsafe {
235+
uvll::uv_tcp_init(loop_.native_handle(), *watcher)
236+
}, 0);
237+
watcher.install_watcher_data();
238+
return watcher;
241239
}
242240

243241
pub fn bind(&mut self, address: SocketAddr) -> Result<(), UvError> {
@@ -289,7 +287,7 @@ impl TcpWatcher {
289287
}
290288
}
291289

292-
impl NativeHandle<*uvll::uv_tcp_t> for TcpWatcher {
290+
impl NativeHandle<uvll::uv_tcp_t> for TcpWatcher {
293291
fn from_native_handle(handle: *uvll::uv_tcp_t) -> TcpWatcher {
294292
TcpWatcher(handle)
295293
}
@@ -303,14 +301,12 @@ impl Watcher for UdpWatcher { }
303301

304302
impl UdpWatcher {
305303
pub fn new(loop_: &Loop) -> UdpWatcher {
306-
unsafe {
307-
let handle = malloc_handle(UV_UDP);
308-
assert!(handle.is_not_null());
309-
assert_eq!(0, uvll::uv_udp_init(loop_.native_handle(), handle));
310-
let mut watcher: UdpWatcher = NativeHandle::from_native_handle(handle);
311-
watcher.install_watcher_data();
312-
return watcher;
313-
}
304+
let mut watcher: UdpWatcher = NativeHandle::alloc(uvll::UV_UDP);
305+
assert_eq!(unsafe {
306+
uvll::uv_udp_init(loop_.native_handle(), *watcher)
307+
}, 0);
308+
watcher.install_watcher_data();
309+
return watcher;
314310
}
315311

316312
pub fn bind(&mut self, address: SocketAddr) -> Result<(), UvError> {
@@ -397,7 +393,7 @@ impl UdpWatcher {
397393
}
398394
}
399395

400-
impl NativeHandle<*uvll::uv_udp_t> for UdpWatcher {
396+
impl NativeHandle<uvll::uv_udp_t> for UdpWatcher {
401397
fn from_native_handle(handle: *uvll::uv_udp_t) -> UdpWatcher {
402398
UdpWatcher(handle)
403399
}
@@ -430,7 +426,7 @@ impl ConnectRequest {
430426
}
431427
}
432428

433-
impl NativeHandle<*uvll::uv_connect_t> for ConnectRequest {
429+
impl NativeHandle<uvll::uv_connect_t> for ConnectRequest {
434430
fn from_native_handle(handle: *uvll:: uv_connect_t) -> ConnectRequest {
435431
ConnectRequest(handle)
436432
}
@@ -462,7 +458,7 @@ impl WriteRequest {
462458
}
463459
}
464460

465-
impl NativeHandle<*uvll::uv_write_t> for WriteRequest {
461+
impl NativeHandle<uvll::uv_write_t> for WriteRequest {
466462
fn from_native_handle(handle: *uvll:: uv_write_t) -> WriteRequest {
467463
WriteRequest(handle)
468464
}
@@ -493,7 +489,7 @@ impl UdpSendRequest {
493489
}
494490
}
495491

496-
impl NativeHandle<*uvll::uv_udp_send_t> for UdpSendRequest {
492+
impl NativeHandle<uvll::uv_udp_send_t> for UdpSendRequest {
497493
fn from_native_handle(handle: *uvll::uv_udp_send_t) -> UdpSendRequest {
498494
UdpSendRequest(handle)
499495
}

src/librustuv/pipe.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
use std::libc;
12+
use std::libc::c_int;
1213
use std::c_str::CString;
1314

1415
use super::{Loop, UvError, Watcher, NativeHandle, status_to_maybe_uv_error};
@@ -22,16 +23,12 @@ impl Watcher for Pipe {}
2223

2324
impl Pipe {
2425
pub fn new(loop_: &Loop, ipc: bool) -> Pipe {
25-
unsafe {
26-
let handle = uvll::malloc_handle(uvll::UV_NAMED_PIPE);
27-
assert!(handle.is_not_null());
28-
let ipc = ipc as libc::c_int;
29-
assert_eq!(uvll::uv_pipe_init(loop_.native_handle(), handle, ipc), 0);
30-
let mut ret: Pipe =
31-
NativeHandle::from_native_handle(handle);
32-
ret.install_watcher_data();
33-
ret
34-
}
26+
let mut watcher: Pipe = NativeHandle::alloc(uvll::UV_NAMED_PIPE);
27+
assert_eq!(unsafe {
28+
uvll::uv_pipe_init(loop_.native_handle(), *watcher, ipc as c_int)
29+
}, 0);
30+
watcher.install_watcher_data();
31+
return watcher;
3532
}
3633

3734
pub fn as_stream(&self) -> net::StreamWatcher {
@@ -88,7 +85,7 @@ impl Pipe {
8885

8986
}
9087

91-
impl NativeHandle<*uvll::uv_pipe_t> for Pipe {
88+
impl NativeHandle<uvll::uv_pipe_t> for Pipe {
9289
fn from_native_handle(handle: *uvll::uv_pipe_t) -> Pipe {
9390
Pipe(handle)
9491
}

src/librustuv/process.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,9 @@ impl Watcher for Process {}
2727
impl Process {
2828
/// Creates a new process, ready to spawn inside an event loop
2929
pub fn new() -> Process {
30-
let handle = unsafe { uvll::malloc_handle(uvll::UV_PROCESS) };
31-
assert!(handle.is_not_null());
32-
let mut ret: Process = NativeHandle::from_native_handle(handle);
33-
ret.install_watcher_data();
34-
return ret;
30+
let mut watcher: Process = NativeHandle::alloc(uvll::UV_PROCESS);
31+
watcher.install_watcher_data();
32+
return watcher;
3533
}
3634

3735
/// Spawn a new process inside the specified event loop.
@@ -192,7 +190,7 @@ fn with_env<T>(env: Option<&[(~str, ~str)]>, f: &fn(**libc::c_char) -> T) -> T {
192190
c_envp.as_imm_buf(|buf, _| f(buf))
193191
}
194192

195-
impl NativeHandle<*uvll::uv_process_t> for Process {
193+
impl NativeHandle<uvll::uv_process_t> for Process {
196194
fn from_native_handle(handle: *uvll::uv_process_t) -> Process {
197195
Process(handle)
198196
}

src/librustuv/signal.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@ impl Watcher for SignalWatcher { }
2121

2222
impl SignalWatcher {
2323
pub fn new(loop_: &mut Loop) -> SignalWatcher {
24-
unsafe {
25-
let handle = uvll::malloc_handle(uvll::UV_SIGNAL);
26-
assert!(handle.is_not_null());
27-
assert!(0 == uvll::uv_signal_init(loop_.native_handle(), handle));
28-
let mut watcher: SignalWatcher = NativeHandle::from_native_handle(handle);
29-
watcher.install_watcher_data();
30-
return watcher;
31-
}
24+
let mut watcher: SignalWatcher = NativeHandle::alloc(uvll::UV_SIGNAL);
25+
assert_eq!(unsafe {
26+
uvll::uv_signal_init(loop_.native_handle(), *watcher)
27+
}, 0);
28+
watcher.install_watcher_data();
29+
return watcher;
3230
}
3331

3432
pub fn start(&mut self, signum: Signum, callback: SignalCallback)
@@ -61,7 +59,7 @@ impl SignalWatcher {
6159
}
6260
}
6361

64-
impl NativeHandle<*uvll::uv_signal_t> for SignalWatcher {
62+
impl NativeHandle<uvll::uv_signal_t> for SignalWatcher {
6563
fn from_native_handle(handle: *uvll::uv_signal_t) -> SignalWatcher {
6664
SignalWatcher(handle)
6765
}

src/librustuv/timer.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ impl Watcher for TimerWatcher { }
1818

1919
impl TimerWatcher {
2020
pub fn new(loop_: &mut Loop) -> TimerWatcher {
21-
unsafe {
22-
let handle = uvll::malloc_handle(uvll::UV_TIMER);
23-
assert!(handle.is_not_null());
24-
assert!(0 == uvll::uv_timer_init(loop_.native_handle(), handle));
25-
let mut watcher: TimerWatcher = NativeHandle::from_native_handle(handle);
26-
watcher.install_watcher_data();
27-
return watcher;
28-
}
21+
let mut watcher: TimerWatcher = NativeHandle::alloc(uvll::UV_TIMER);
22+
assert_eq!(unsafe {
23+
uvll::uv_timer_init(loop_.native_handle(), *watcher)
24+
}, 0);
25+
watcher.install_watcher_data();
26+
return watcher;
2927
}
3028

3129
pub fn start(&mut self, timeout: u64, repeat: u64, cb: TimerCallback) {
@@ -54,7 +52,7 @@ impl TimerWatcher {
5452
}
5553
}
5654

57-
impl NativeHandle<*uvll::uv_timer_t> for TimerWatcher {
55+
impl NativeHandle<uvll::uv_timer_t> for TimerWatcher {
5856
fn from_native_handle(handle: *uvll::uv_timer_t) -> TimerWatcher {
5957
TimerWatcher(handle)
6058
}

src/librustuv/tty.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,18 @@ impl TTY {
2424
pub fn new(loop_: &Loop, fd: libc::c_int, readable: bool) ->
2525
Result<TTY, UvError>
2626
{
27-
let handle = unsafe { uvll::malloc_handle(uvll::UV_TTY) };
28-
assert!(handle.is_not_null());
29-
27+
let mut watcher: TTY = NativeHandle::alloc(uvll::UV_TTY);
3028
let ret = unsafe {
31-
uvll::uv_tty_init(loop_.native_handle(), handle, fd as libc::c_int,
29+
uvll::uv_tty_init(loop_.native_handle(), *watcher, fd as libc::c_int,
3230
readable as libc::c_int)
3331
};
3432
match ret {
3533
0 => {
36-
let mut ret: TTY = NativeHandle::from_native_handle(handle);
37-
ret.install_watcher_data();
38-
Ok(ret)
34+
watcher.install_watcher_data();
35+
Ok(watcher)
3936
}
4037
n => {
41-
unsafe { uvll::free_handle(handle); }
38+
unsafe { uvll::free_handle(*watcher); }
4239
Err(UvError(n))
4340
}
4441
}
@@ -72,7 +69,7 @@ impl TTY {
7269
}
7370
}
7471

75-
impl NativeHandle<*uvll::uv_tty_t> for TTY {
72+
impl NativeHandle<uvll::uv_tty_t> for TTY {
7673
fn from_native_handle(handle: *uvll::uv_tty_t) -> TTY {
7774
TTY(handle)
7875
}

0 commit comments

Comments
 (0)