core::rt: Fix some copies in uv

This commit is contained in:
Brian Anderson 2013-04-27 17:26:35 -07:00
parent b771c99304
commit cfd183db15
3 changed files with 9 additions and 14 deletions

View File

@ -362,7 +362,6 @@ struct WatcherData {
connect_cb: Option<ConnectionCallback>,
close_cb: Option<NullCallback>,
alloc_cb: Option<AllocCallback>,
buf: Option<Buf>
}
pub fn install_watcher_data<H, W: Watcher + NativeHandle<*H>>(watcher: &mut W) {
@ -373,7 +372,6 @@ pub fn install_watcher_data<H, W: Watcher + NativeHandle<*H>>(watcher: &mut W) {
connect_cb: None,
close_cb: None,
alloc_cb: None,
buf: None
};
let data = transmute::<~WatcherData, *c_void>(data);
uvll::set_data_for_uv_handle(watcher.native_handle(), data);

View File

@ -11,12 +11,13 @@
use prelude::*;
use libc::{size_t, ssize_t, c_int, c_void};
use cast::transmute_mut_region;
use util::ignore;
use rt::uv::uvll;
use rt::uv::uvll::*;
use super::{Loop, Watcher, Request, UvError, Buf, Callback, NativeHandle, NullCallback,
loop_from_watcher, status_to_maybe_uv_error,
install_watcher_data, get_watcher_data, drop_watcher_data,
vec_to_uv_buf, vec_from_uv_buf};
vec_to_uv_buf, vec_from_uv_buf, slice_to_uv_buf};
use super::super::io::net::ip::{IpAddr, Ipv4, Ipv6};
use rt::uv::last_uv_error;
@ -99,17 +100,13 @@ fn read_stop(&mut self) {
unsafe { uvll::read_stop(handle); }
}
// XXX: Needs to take &[u8], not ~[u8]
fn write(&mut self, msg: ~[u8], cb: ConnectionCallback) {
fn write(&mut self, buf: Buf, cb: ConnectionCallback) {
// XXX: Borrowck
let data = get_watcher_data(unsafe { transmute_mut_region(self) });
assert!(data.write_cb.is_none());
data.write_cb = Some(cb);
let req = WriteRequest::new();
let buf = vec_to_uv_buf(msg);
assert!(data.buf.is_none());
data.buf = Some(buf);
let bufs = [buf];
unsafe {
assert!(0 == uvll::write(req.native_handle(),
@ -123,7 +120,6 @@ fn write(&mut self, msg: ~[u8], cb: ConnectionCallback) {
write_request.delete();
let cb = {
let data = get_watcher_data(&mut stream_watcher);
let _vec = vec_from_uv_buf(data.buf.swap_unwrap());
let cb = data.write_cb.swap_unwrap();
cb
};
@ -434,10 +430,13 @@ fn listen() {
assert!(status.is_none());
let mut stream_watcher = stream_watcher;
let msg = ~[0, 1, 2, 3, 4, 5, 6 ,7 ,8, 9];
do stream_watcher.write(msg) |stream_watcher, status| {
let buf = slice_to_uv_buf(msg);
let msg_cell = Cell(msg);
do stream_watcher.write(buf) |stream_watcher, status| {
rtdebug!("writing");
assert!(status.is_none());
stream_watcher.close(||());
let msg_cell = Cell(msg_cell.take());
stream_watcher.close(||ignore(msg_cell.take()));
}
}
loop_.run();

View File

@ -289,9 +289,7 @@ fn write(&mut self, buf: &[u8]) -> Result<(), IoError> {
do scheduler.deschedule_running_task_and_then |task| {
let mut watcher = watcher;
let task_cell = Cell(task);
let buf = unsafe { &*buf_ptr };
// XXX: OMGCOPIES
let buf = buf.to_vec();
let buf = unsafe { slice_to_uv_buf(*buf_ptr) };
do watcher.write(buf) |_watcher, status| {
let result = if status.is_none() {
Ok(())