Skip to content

Reduce a large amount of uv boilerplate #10256

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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 8 additions & 8 deletions src/librustuv/addrinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ impl GetAddrInfoRequest {
self.get_req_data().getaddrinfo_cb = Some(wrapper_cb);

unsafe {
assert!(0 == uvll::getaddrinfo(loop_.native_handle(),
self.native_handle(),
getaddrinfo_cb,
c_node_ptr,
c_service_ptr,
hint_ptr));
assert!(0 == uvll::uv_getaddrinfo(loop_.native_handle(),
self.native_handle(),
getaddrinfo_cb,
c_node_ptr,
c_service_ptr,
hint_ptr));
}

extern "C" fn getaddrinfo_cb(req: *uvll::uv_getaddrinfo_t,
Expand All @@ -127,7 +127,7 @@ impl GetAddrInfoRequest {
let data = req.get_req_data();
(*data.getaddrinfo_cb.get_ref())(req, &addrinfo, err);
unsafe {
uvll::freeaddrinfo(res);
uvll::uv_freeaddrinfo(res);
}
}
}
Expand Down Expand Up @@ -235,7 +235,7 @@ pub fn accum_addrinfo(addr: &net::UvAddrInfo) -> ~[ai::Info] {
}
}

impl NativeHandle<*uvll::uv_getaddrinfo_t> for GetAddrInfoRequest {
impl NativeHandle<uvll::uv_getaddrinfo_t> for GetAddrInfoRequest {
fn from_native_handle(handle: *uvll::uv_getaddrinfo_t) -> GetAddrInfoRequest {
GetAddrInfoRequest(handle)
}
Expand Down
24 changes: 9 additions & 15 deletions src/librustuv/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ impl Watcher for AsyncWatcher { }

impl AsyncWatcher {
pub fn new(loop_: &mut Loop, cb: AsyncCallback) -> AsyncWatcher {
unsafe {
let handle = uvll::malloc_handle(uvll::UV_ASYNC);
assert!(handle.is_not_null());
let mut watcher: AsyncWatcher = NativeHandle::from_native_handle(handle);
watcher.install_watcher_data();
let data = watcher.get_watcher_data();
data.async_cb = Some(cb);
assert_eq!(0, uvll::async_init(loop_.native_handle(), handle, async_cb));
return watcher;
}
let mut watcher: AsyncWatcher = NativeHandle::alloc(uvll::UV_ASYNC);
assert_eq!(unsafe {
uvll::uv_async_init(loop_.native_handle(), *watcher, async_cb)
}, 0);
watcher.install_watcher_data();
watcher.get_watcher_data().async_cb = Some(cb);
return watcher;

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

pub fn send(&mut self) {
unsafe {
let handle = self.native_handle();
uvll::async_send(handle);
}
unsafe { uvll::uv_async_send(self.native_handle()) }
}
}

impl NativeHandle<*uvll::uv_async_t> for AsyncWatcher {
impl NativeHandle<uvll::uv_async_t> for AsyncWatcher {
fn from_native_handle(handle: *uvll::uv_async_t) -> AsyncWatcher {
AsyncWatcher(handle)
}
Expand Down
84 changes: 42 additions & 42 deletions src/librustuv/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
use std::ptr::null;
use std::c_str;
use std::c_str::CString;
use std::libc::c_void;
use std::cast::transmute;
use std::libc;
use std::libc::{c_int};
use std::libc::{c_void, c_int, c_uint};
use std::cast::transmute;

use super::{Request, NativeHandle, Loop, FsCallback, Buf,
status_to_maybe_uv_error, UvError};
Expand Down Expand Up @@ -43,8 +42,9 @@ impl FsRequest {
me.req_boilerplate(Some(cb))
};
let ret = path.with_ref(|p| unsafe {
uvll::fs_open(loop_.native_handle(),
self.native_handle(), p, flags, mode, complete_cb_ptr)
uvll::uv_fs_open(loop_.native_handle(),
self.native_handle(), p, flags as c_int,
mode as c_int, complete_cb_ptr)
});
assert_eq!(ret, 0);
}
Expand All @@ -56,8 +56,9 @@ impl FsRequest {
me.req_boilerplate(None)
};
let result = path.with_ref(|p| unsafe {
uvll::fs_open(loop_.native_handle(),
self.native_handle(), p, flags, mode, complete_cb_ptr)
uvll::uv_fs_open(loop_.native_handle(),
self.native_handle(), p, flags as c_int,
mode as c_int, complete_cb_ptr)
});
self.sync_cleanup(result)
}
Expand All @@ -68,8 +69,8 @@ impl FsRequest {
me.req_boilerplate(Some(cb))
};
let ret = path.with_ref(|p| unsafe {
uvll::fs_unlink(loop_.native_handle(),
self.native_handle(), p, complete_cb_ptr)
uvll::uv_fs_unlink(loop_.native_handle(),
self.native_handle(), p, complete_cb_ptr)
});
assert_eq!(ret, 0);
}
Expand All @@ -81,8 +82,8 @@ impl FsRequest {
me.req_boilerplate(None)
};
let result = path.with_ref(|p| unsafe {
uvll::fs_unlink(loop_.native_handle(),
self.native_handle(), p, complete_cb_ptr)
uvll::uv_fs_unlink(loop_.native_handle(),
self.native_handle(), p, complete_cb_ptr)
});
self.sync_cleanup(result)
}
Expand All @@ -93,8 +94,8 @@ impl FsRequest {
me.req_boilerplate(Some(cb))
};
let ret = path.with_ref(|p| unsafe {
uvll::fs_stat(loop_.native_handle(),
self.native_handle(), p, complete_cb_ptr)
uvll::uv_fs_stat(loop_.native_handle(),
self.native_handle(), p, complete_cb_ptr)
});
assert_eq!(ret, 0);
}
Expand All @@ -107,9 +108,9 @@ impl FsRequest {
let base_ptr = buf.base as *c_void;
let len = buf.len as uint;
let ret = unsafe {
uvll::fs_write(loop_.native_handle(), self.native_handle(),
fd, base_ptr,
len, offset, complete_cb_ptr)
uvll::uv_fs_write(loop_.native_handle(), self.native_handle(),
fd, base_ptr,
len as c_uint, offset, complete_cb_ptr)
};
assert_eq!(ret, 0);
}
Expand All @@ -122,9 +123,9 @@ impl FsRequest {
let base_ptr = buf.base as *c_void;
let len = buf.len as uint;
let result = unsafe {
uvll::fs_write(loop_.native_handle(), self.native_handle(),
fd, base_ptr,
len, offset, complete_cb_ptr)
uvll::uv_fs_write(loop_.native_handle(), self.native_handle(),
fd, base_ptr,
len as c_uint, offset, complete_cb_ptr)
};
self.sync_cleanup(result)
}
Expand All @@ -137,9 +138,9 @@ impl FsRequest {
let buf_ptr = buf.base as *c_void;
let len = buf.len as uint;
let ret = unsafe {
uvll::fs_read(loop_.native_handle(), self.native_handle(),
fd, buf_ptr,
len, offset, complete_cb_ptr)
uvll::uv_fs_read(loop_.native_handle(), self.native_handle(),
fd, buf_ptr,
len as c_uint, offset, complete_cb_ptr)
};
assert_eq!(ret, 0);
}
Expand All @@ -152,9 +153,9 @@ impl FsRequest {
let buf_ptr = buf.base as *c_void;
let len = buf.len as uint;
let result = unsafe {
uvll::fs_read(loop_.native_handle(), self.native_handle(),
fd, buf_ptr,
len, offset, complete_cb_ptr)
uvll::uv_fs_read(loop_.native_handle(), self.native_handle(),
fd, buf_ptr,
len as c_uint, offset, complete_cb_ptr)
};
self.sync_cleanup(result)
}
Expand All @@ -165,8 +166,8 @@ impl FsRequest {
me.req_boilerplate(Some(cb))
};
let ret = unsafe {
uvll::fs_close(loop_.native_handle(), self.native_handle(),
fd, complete_cb_ptr)
uvll::uv_fs_close(loop_.native_handle(), self.native_handle(),
fd, complete_cb_ptr)
};
assert_eq!(ret, 0);
}
Expand All @@ -176,8 +177,8 @@ impl FsRequest {
me.req_boilerplate(None)
};
let result = unsafe {
uvll::fs_close(loop_.native_handle(), self.native_handle(),
fd, complete_cb_ptr)
uvll::uv_fs_close(loop_.native_handle(), self.native_handle(),
fd, complete_cb_ptr)
};
self.sync_cleanup(result)
}
Expand All @@ -188,8 +189,9 @@ impl FsRequest {
me.req_boilerplate(Some(cb))
};
let ret = path.with_ref(|p| unsafe {
uvll::fs_mkdir(loop_.native_handle(),
self.native_handle(), p, mode, complete_cb_ptr)
uvll::uv_fs_mkdir(loop_.native_handle(),
self.native_handle(), p,
mode as c_int, complete_cb_ptr)
});
assert_eq!(ret, 0);
}
Expand All @@ -200,8 +202,8 @@ impl FsRequest {
me.req_boilerplate(Some(cb))
};
let ret = path.with_ref(|p| unsafe {
uvll::fs_rmdir(loop_.native_handle(),
self.native_handle(), p, complete_cb_ptr)
uvll::uv_fs_rmdir(loop_.native_handle(),
self.native_handle(), p, complete_cb_ptr)
});
assert_eq!(ret, 0);
}
Expand All @@ -213,8 +215,8 @@ impl FsRequest {
me.req_boilerplate(Some(cb))
};
let ret = path.with_ref(|p| unsafe {
uvll::fs_readdir(loop_.native_handle(),
self.native_handle(), p, flags, complete_cb_ptr)
uvll::uv_fs_readdir(loop_.native_handle(),
self.native_handle(), p, flags, complete_cb_ptr)
});
assert_eq!(ret, 0);
}
Expand All @@ -228,12 +230,10 @@ impl FsRequest {
None => Ok(result)
}
}
fn req_boilerplate(&mut self, cb: Option<FsCallback>) -> *u8 {
fn req_boilerplate(&mut self, cb: Option<FsCallback>) -> uvll::uv_fs_cb {
let result = match cb {
Some(_) => {
compl_cb as *u8
},
None => 0 as *u8
Some(_) => compl_cb,
None => 0 as uvll::uv_fs_cb
};
self.install_req_data(cb);
result
Expand Down Expand Up @@ -304,13 +304,13 @@ impl FsRequest {
let data = uvll::get_data_for_req(self.native_handle());
let _data = transmute::<*c_void, ~RequestData>(data);
uvll::set_data_for_req(self.native_handle(), null::<()>());
uvll::fs_req_cleanup(self.native_handle());
uvll::uv_fs_req_cleanup(self.native_handle());
free_req(self.native_handle() as *c_void)
}
}
}

impl NativeHandle<*uvll::uv_fs_t> for FsRequest {
impl NativeHandle<uvll::uv_fs_t> for FsRequest {
fn from_native_handle(handle: *uvll:: uv_fs_t) -> FsRequest {
FsRequest(handle)
}
Expand Down
22 changes: 10 additions & 12 deletions src/librustuv/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ impl Watcher for IdleWatcher { }

impl IdleWatcher {
pub fn new(loop_: &mut Loop) -> IdleWatcher {
unsafe {
let handle = uvll::malloc_handle(uvll::UV_IDLE);
assert!(handle.is_not_null());
assert_eq!(uvll::idle_init(loop_.native_handle(), handle), 0);
let mut watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
watcher.install_watcher_data();
return watcher
}
let mut watcher: IdleWatcher = NativeHandle::alloc(uvll::UV_IDLE);
assert_eq!(unsafe {
uvll::uv_idle_init(loop_.native_handle(), *watcher)
}, 0);
watcher.install_watcher_data();
return watcher;
}

pub fn start(&mut self, cb: IdleCallback) {
Expand All @@ -35,14 +33,14 @@ impl IdleWatcher {
}

unsafe {
assert_eq!(uvll::idle_start(self.native_handle(), idle_cb), 0)
assert_eq!(uvll::uv_idle_start(self.native_handle(), idle_cb), 0)
}
}

pub fn restart(&mut self) {
unsafe {
assert!(self.get_watcher_data().idle_cb.is_some());
assert_eq!(uvll::idle_start(self.native_handle(), idle_cb), 0)
assert_eq!(uvll::uv_idle_start(self.native_handle(), idle_cb), 0)
}
}

Expand All @@ -52,12 +50,12 @@ impl IdleWatcher {
// free

unsafe {
assert_eq!(uvll::idle_stop(self.native_handle()), 0);
assert_eq!(uvll::uv_idle_stop(self.native_handle()), 0);
}
}
}

impl NativeHandle<*uvll::uv_idle_t> for IdleWatcher {
impl NativeHandle<uvll::uv_idle_t> for IdleWatcher {
fn from_native_handle(handle: *uvll::uv_idle_t) -> IdleWatcher {
IdleWatcher(handle)
}
Expand Down
Loading