Skip to content
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

Remove unnecessary mutability in WatcherInterop #9688

Closed
wants to merge 1 commit 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
13 changes: 6 additions & 7 deletions src/libstd/rt/uv/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl AsyncWatcher {
unsafe {
let handle = uvll::malloc_handle(UV_ASYNC);
assert!(handle.is_not_null());
let mut watcher: AsyncWatcher = NativeHandle::from_native_handle(handle);
let watcher: AsyncWatcher = NativeHandle::from_native_handle(handle);
watcher.install_watcher_data();
let data = watcher.get_watcher_data();
data.async_cb = Some(cb);
Expand All @@ -33,24 +33,23 @@ impl AsyncWatcher {
}

extern fn async_cb(handle: *uvll::uv_async_t, status: c_int) {
let mut watcher: AsyncWatcher = NativeHandle::from_native_handle(handle);
let watcher: AsyncWatcher = NativeHandle::from_native_handle(handle);
let status = status_to_maybe_uv_error(status);
let data = watcher.get_watcher_data();
let cb = data.async_cb.get_ref();
(*cb)(watcher, status);
}
}

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

pub fn close(self, cb: NullCallback) {
let mut this = self;
let data = this.get_watcher_data();
let data = self.get_watcher_data();
assert!(data.close_cb.is_none());
data.close_cb = Some(cb);

Expand All @@ -59,7 +58,7 @@ impl AsyncWatcher {
}

extern fn close_cb(handle: *uvll::uv_stream_t) {
let mut watcher: AsyncWatcher = NativeHandle::from_native_handle(handle);
let watcher: AsyncWatcher = NativeHandle::from_native_handle(handle);
{
let data = watcher.get_watcher_data();
data.close_cb.take_unwrap()();
Expand Down Expand Up @@ -95,7 +94,7 @@ mod test {
let watcher = AsyncWatcher::new(&mut loop_, |w, _| w.close(||()) );
let watcher_cell = Cell::new(watcher);
let thread = do Thread::start {
let mut watcher = watcher_cell.take();
let watcher = watcher_cell.take();
watcher.send();
};
loop_.run();
Expand Down
26 changes: 12 additions & 14 deletions src/libstd/rt/uv/idle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ impl IdleWatcher {
let handle = uvll::idle_new();
assert!(handle.is_not_null());
assert!(0 == uvll::idle_init(loop_.native_handle(), handle));
let mut watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
let watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
watcher.install_watcher_data();
return watcher
}
}

pub fn start(&mut self, cb: IdleCallback) {
pub fn start(&self, cb: IdleCallback) {
{
let data = self.get_watcher_data();
data.idle_cb = Some(cb);
Expand All @@ -40,29 +40,29 @@ impl IdleWatcher {
};

extern fn idle_cb(handle: *uvll::uv_idle_t, status: c_int) {
let mut idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
let idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
let data = idle_watcher.get_watcher_data();
let cb: &IdleCallback = data.idle_cb.get_ref();
let status = status_to_maybe_uv_error(status);
(*cb)(idle_watcher, status);
}
}

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

extern fn idle_cb(handle: *uvll::uv_idle_t, status: c_int) {
let mut idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
let idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
let data = idle_watcher.get_watcher_data();
let cb: &IdleCallback = data.idle_cb.get_ref();
let status = status_to_maybe_uv_error(status);
(*cb)(idle_watcher, status);
}
}

pub fn stop(&mut self) {
pub fn stop(&self) {
// NB: Not resetting the Rust idle_cb to None here because `stop` is
// likely called from *within* the idle callback, causing a use after
// free
Expand All @@ -74,8 +74,7 @@ impl IdleWatcher {

pub fn close(self, cb: NullCallback) {
{
let mut this = self;
let data = this.get_watcher_data();
let data = self.get_watcher_data();
assert!(data.close_cb.is_none());
data.close_cb = Some(cb);
}
Expand All @@ -84,7 +83,7 @@ impl IdleWatcher {

extern fn close_cb(handle: *uvll::uv_idle_t) {
unsafe {
let mut idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
let idle_watcher: IdleWatcher = NativeHandle::from_native_handle(handle);
{
let data = idle_watcher.get_watcher_data();
data.close_cb.take_unwrap()();
Expand Down Expand Up @@ -126,11 +125,10 @@ mod test {
fn idle_smoke_test() {
do run_in_bare_thread {
let mut loop_ = Loop::new();
let mut idle_watcher = { IdleWatcher::new(&mut loop_) };
let idle_watcher = { IdleWatcher::new(&mut loop_) };
let mut count = 10;
let count_ptr: *mut int = &mut count;
do idle_watcher.start |idle_watcher, status| {
let mut idle_watcher = idle_watcher;
assert!(status.is_none());
if unsafe { *count_ptr == 10 } {
idle_watcher.stop();
Expand All @@ -149,14 +147,14 @@ mod test {
fn idle_start_stop_start() {
do run_in_bare_thread {
let mut loop_ = Loop::new();
let mut idle_watcher = { IdleWatcher::new(&mut loop_) };
let idle_watcher = { IdleWatcher::new(&mut loop_) };
do idle_watcher.start |idle_watcher, status| {
let mut idle_watcher = idle_watcher;
let idle_watcher = idle_watcher;
assert!(status.is_none());
idle_watcher.stop();
do idle_watcher.start |idle_watcher, status| {
assert!(status.is_none());
let mut idle_watcher = idle_watcher;
let idle_watcher = idle_watcher;
idle_watcher.stop();
idle_watcher.close(||());
}
Expand Down
12 changes: 6 additions & 6 deletions src/libstd/rt/uv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ struct WatcherData {

pub trait WatcherInterop {
fn event_loop(&self) -> Loop;
fn install_watcher_data(&mut self);
fn get_watcher_data<'r>(&'r mut self) -> &'r mut WatcherData;
fn drop_watcher_data(&mut self);
fn install_watcher_data(&self);
fn get_watcher_data<'r>(&'r self) -> &'r mut WatcherData;
fn drop_watcher_data(&self);
}

impl<H, W: Watcher + NativeHandle<*H>> WatcherInterop for W {
Expand All @@ -172,7 +172,7 @@ impl<H, W: Watcher + NativeHandle<*H>> WatcherInterop for W {
}
}

fn install_watcher_data(&mut self) {
fn install_watcher_data(&self) {
unsafe {
let data = ~WatcherData {
read_cb: None,
Expand All @@ -192,15 +192,15 @@ impl<H, W: Watcher + NativeHandle<*H>> WatcherInterop for W {
}
}

fn get_watcher_data<'r>(&'r mut self) -> &'r mut WatcherData {
fn get_watcher_data<'r>(&'r self) -> &'r mut WatcherData {
unsafe {
let data = uvll::get_data_for_uv_handle(self.native_handle());
let data = transmute::<&*c_void, &mut ~WatcherData>(&data);
return &mut **data;
}
}

fn drop_watcher_data(&mut self) {
fn drop_watcher_data(&self) {
unsafe {
let data = uvll::get_data_for_uv_handle(self.native_handle());
let _data = transmute::<*c_void, ~WatcherData>(data);
Expand Down
Loading