diff --git a/src/libcore/arc.rs b/src/libcore/arc.rs index ddbc4e8321f..7e8d69298bd 100644 --- a/src/libcore/arc.rs +++ b/src/libcore/arc.rs @@ -24,17 +24,19 @@ fn rust_atomic_decrement(p: &mut libc::intptr_t) data: T }; -resource arc_destruct(data: *libc::c_void) { - unsafe { - let data: ~arc_data = unsafe::reinterpret_cast(data); - let new_count = rustrt::rust_atomic_decrement(&mut data.count); - assert new_count >= 0; - if new_count == 0 { - // drop glue takes over. - } else { - unsafe::forget(data); - } - } +class arc_destruct { + let data: *libc::c_void; + new(data: *libc::c_void) { self.data = data; } + drop unsafe { + let data: ~arc_data = unsafe::reinterpret_cast(self.data); + let new_count = rustrt::rust_atomic_decrement(&mut data.count); + assert new_count >= 0; + if new_count == 0 { + // drop glue takes over. + } else { + unsafe::forget(data); + } + } } type arc = arc_destruct; @@ -52,7 +54,7 @@ fn arc(-data: T) -> arc { wrapper."] fn get(rc: &a.arc) -> &a.T { unsafe { - let ptr: ~arc_data = unsafe::reinterpret_cast(**rc); + let ptr: ~arc_data = unsafe::reinterpret_cast((*rc).data); // Cast us back into the correct region let r = unsafe::reinterpret_cast(&ptr.data); unsafe::forget(ptr); @@ -67,12 +69,12 @@ fn get(rc: &a.arc) -> &a.T { allowing them to share the underlying data."] fn clone(rc: &arc) -> arc { unsafe { - let ptr: ~arc_data = unsafe::reinterpret_cast(**rc); + let ptr: ~arc_data = unsafe::reinterpret_cast((*rc).data); let new_count = rustrt::rust_atomic_increment(&mut ptr.count); assert new_count >= 2; unsafe::forget(ptr); } - arc_destruct(**rc) + arc_destruct((*rc).data) } // An arc over mutable data that is protected by a lock. @@ -93,17 +95,19 @@ impl methods for exclusive { fn clone() -> exclusive { unsafe { // this makes me nervous... - let ptr: ~arc_data> = unsafe::reinterpret_cast(*self); + let ptr: ~arc_data> = + unsafe::reinterpret_cast(self.data); let new_count = rustrt::rust_atomic_increment(&mut ptr.count); assert new_count > 1; unsafe::forget(ptr); } - arc_destruct(*self) + arc_destruct(self.data) } fn with(f: fn(sys::condition, x: &T) -> U) -> U { unsafe { - let ptr: ~arc_data> = unsafe::reinterpret_cast(*self); + let ptr: ~arc_data> = + unsafe::reinterpret_cast(self.data); let r = { let rec: &ex_data = &(*ptr).data; rec.lock.lock_cond() {|c| @@ -123,8 +127,10 @@ fn with(f: fn(sys::condition, x: &T) -> U) -> U { // (terminate, get) type shared_arc = (shared_arc_res, get_chan); -resource shared_arc_res(c: comm::chan<()>) { - c.send(()); +class shared_arc_res { + let c: comm::chan<()>; + new(c: comm::chan<()>) { self.c = c; } + drop { self.c.send(()); } } fn shared_arc(-data: T) -> shared_arc { diff --git a/src/libcore/priv.rs b/src/libcore/priv.rs index fc1e547ebde..922d7c72819 100644 --- a/src/libcore/priv.rs +++ b/src/libcore/priv.rs @@ -181,8 +181,12 @@ unsafe fn weaken_task(f: fn(comm::port<()>)) unsafe { let _unweaken = unweaken(ch); f(po); - resource unweaken(ch: comm::chan<()>) unsafe { - rustrt::rust_task_unweaken(unsafe::reinterpret_cast(ch)); + class unweaken { + let ch: comm::chan<()>; + new(ch: comm::chan<()>) { self.ch = ch; } + drop unsafe { + rustrt::rust_task_unweaken(unsafe::reinterpret_cast(self.ch)); + } } } diff --git a/src/libcore/rand.rs b/src/libcore/rand.rs index f5e404712b9..99ac4addc78 100644 --- a/src/libcore/rand.rs +++ b/src/libcore/rand.rs @@ -230,10 +230,14 @@ fn shuffle_mut(&&values: [mut T]) { } -resource rand_res(c: *rctx) { rustrt::rand_free(c); } +class rand_res { + let c: *rctx; + new(c: *rctx) { self.c = c; } + drop { rustrt::rand_free(self.c); } +} impl of rng for @rand_res { - fn next() -> u32 { ret rustrt::rand_next(**self); } + fn next() -> u32 { ret rustrt::rand_next((*self).c); } } #[doc = "Create a new random seed for seeded_rng"] diff --git a/src/libcore/run.rs b/src/libcore/run.rs index 401454c40df..4183056b741 100644 --- a/src/libcore/run.rs +++ b/src/libcore/run.rs @@ -170,9 +170,9 @@ fn run_program(prog: str, args: [str]) -> int { #[doc =" Spawns a process and returns a program -The returned value is a boxed resource containing a object that can -be used for sending and recieving data over the standard file descriptors. -The resource will ensure that file descriptors are closed properly. +The returned value is a boxed class containing a object that can +be used for sending and receiving data over the standard file descriptors. +The class will ensure that file descriptors are closed properly. # Arguments @@ -181,7 +181,7 @@ fn run_program(prog: str, args: [str]) -> int { # Return value -A boxed resource of +A class with a field "] fn start_program(prog: str, args: [str]) -> program { let pipe_input = os::pipe(); @@ -221,16 +221,20 @@ fn destroy_repr(r: prog_repr) { libc::fclose(r.out_file); libc::fclose(r.err_file); } - resource prog_res(r: prog_repr) { destroy_repr(r); } + class prog_res { + let r: prog_repr; + new(-r: prog_repr) { self.r = r; } + drop { destroy_repr(self.r); } + } impl of program for prog_res { - fn get_id() -> pid_t { ret self.pid; } - fn input() -> io::writer { io::fd_writer(self.in_fd, false) } - fn output() -> io::reader { io::FILE_reader(self.out_file, false) } - fn err() -> io::reader { io::FILE_reader(self.err_file, false) } - fn close_input() { close_repr_input(*self); } - fn finish() -> int { finish_repr(*self) } - fn destroy() { destroy_repr(*self); } + fn get_id() -> pid_t { ret self.r.pid; } + fn input() -> io::writer { io::fd_writer(self.r.in_fd, false) } + fn output() -> io::reader { io::FILE_reader(self.r.out_file, false) } + fn err() -> io::reader { io::FILE_reader(self.r.err_file, false) } + fn close_input() { close_repr_input(self.r); } + fn finish() -> int { finish_repr(self.r) } + fn destroy() { destroy_repr(self.r); } } let repr = {pid: pid, mut in_fd: pipe_input.out,