Skip to content

Commit 25aa360

Browse files
committed
[NEEDS SNAPSHOT] Port remainder of resources to classes in libcore
1 parent a141f58 commit 25aa360

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

src/libcore/arc.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ type arc_data<T> = {
2424
data: T
2525
};
2626

27-
resource arc_destruct<T>(data: *libc::c_void) {
28-
unsafe {
29-
let data: ~arc_data<T> = unsafe::reinterpret_cast(data);
30-
let new_count = rustrt::rust_atomic_decrement(&mut data.count);
31-
assert new_count >= 0;
32-
if new_count == 0 {
33-
// drop glue takes over.
34-
} else {
35-
unsafe::forget(data);
36-
}
37-
}
27+
class arc_destruct<T> {
28+
let data: *libc::c_void;
29+
new(data: *libc::c_void) { self.data = data; }
30+
drop unsafe {
31+
let data: ~arc_data<T> = unsafe::reinterpret_cast(self.data);
32+
let new_count = rustrt::rust_atomic_decrement(&mut data.count);
33+
assert new_count >= 0;
34+
if new_count == 0 {
35+
// drop glue takes over.
36+
} else {
37+
unsafe::forget(data);
38+
}
39+
}
3840
}
3941

4042
type arc<T: const> = arc_destruct<T>;
@@ -52,7 +54,7 @@ fn arc<T: const>(-data: T) -> arc<T> {
5254
wrapper."]
5355
fn get<T: const>(rc: &a.arc<T>) -> &a.T {
5456
unsafe {
55-
let ptr: ~arc_data<T> = unsafe::reinterpret_cast(**rc);
57+
let ptr: ~arc_data<T> = unsafe::reinterpret_cast((*rc).data);
5658
// Cast us back into the correct region
5759
let r = unsafe::reinterpret_cast(&ptr.data);
5860
unsafe::forget(ptr);
@@ -67,12 +69,12 @@ object. However, one of the `arc` objects can be sent to another task,
6769
allowing them to share the underlying data."]
6870
fn clone<T: const>(rc: &arc<T>) -> arc<T> {
6971
unsafe {
70-
let ptr: ~arc_data<T> = unsafe::reinterpret_cast(**rc);
72+
let ptr: ~arc_data<T> = unsafe::reinterpret_cast((*rc).data);
7173
let new_count = rustrt::rust_atomic_increment(&mut ptr.count);
7274
assert new_count >= 2;
7375
unsafe::forget(ptr);
7476
}
75-
arc_destruct(**rc)
77+
arc_destruct((*rc).data)
7678
}
7779

7880
// An arc over mutable data that is protected by a lock.
@@ -93,17 +95,19 @@ impl methods<T: send> for exclusive<T> {
9395
fn clone() -> exclusive<T> {
9496
unsafe {
9597
// this makes me nervous...
96-
let ptr: ~arc_data<ex_data<T>> = unsafe::reinterpret_cast(*self);
98+
let ptr: ~arc_data<ex_data<T>> =
99+
unsafe::reinterpret_cast(self.data);
97100
let new_count = rustrt::rust_atomic_increment(&mut ptr.count);
98101
assert new_count > 1;
99102
unsafe::forget(ptr);
100103
}
101-
arc_destruct(*self)
104+
arc_destruct(self.data)
102105
}
103106

104107
fn with<U>(f: fn(sys::condition, x: &T) -> U) -> U {
105108
unsafe {
106-
let ptr: ~arc_data<ex_data<T>> = unsafe::reinterpret_cast(*self);
109+
let ptr: ~arc_data<ex_data<T>> =
110+
unsafe::reinterpret_cast(self.data);
107111
let r = {
108112
let rec: &ex_data<T> = &(*ptr).data;
109113
rec.lock.lock_cond() {|c|
@@ -123,8 +127,10 @@ type get_chan<T: const send> = chan<chan<arc<T>>>;
123127
// (terminate, get)
124128
type shared_arc<T: const send> = (shared_arc_res, get_chan<T>);
125129

126-
resource shared_arc_res(c: comm::chan<()>) {
127-
c.send(());
130+
class shared_arc_res {
131+
let c: comm::chan<()>;
132+
new(c: comm::chan<()>) { self.c = c; }
133+
drop { self.c.send(()); }
128134
}
129135

130136
fn shared_arc<T: send const>(-data: T) -> shared_arc<T> {

src/libcore/priv.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,12 @@ unsafe fn weaken_task(f: fn(comm::port<()>)) unsafe {
181181
let _unweaken = unweaken(ch);
182182
f(po);
183183

184-
resource unweaken(ch: comm::chan<()>) unsafe {
185-
rustrt::rust_task_unweaken(unsafe::reinterpret_cast(ch));
184+
class unweaken {
185+
let ch: comm::chan<()>;
186+
new(ch: comm::chan<()>) { self.ch = ch; }
187+
drop unsafe {
188+
rustrt::rust_task_unweaken(unsafe::reinterpret_cast(self.ch));
189+
}
186190
}
187191
}
188192

src/libcore/rand.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,14 @@ impl extensions for rng {
230230

231231
}
232232

233-
resource rand_res(c: *rctx) { rustrt::rand_free(c); }
233+
class rand_res {
234+
let c: *rctx;
235+
new(c: *rctx) { self.c = c; }
236+
drop { rustrt::rand_free(self.c); }
237+
}
234238

235239
impl of rng for @rand_res {
236-
fn next() -> u32 { ret rustrt::rand_next(**self); }
240+
fn next() -> u32 { ret rustrt::rand_next((*self).c); }
237241
}
238242

239243
#[doc = "Create a new random seed for seeded_rng"]

src/libcore/run.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,9 @@ fn run_program(prog: str, args: [str]) -> int {
170170
#[doc ="
171171
Spawns a process and returns a program
172172
173-
The returned value is a boxed resource containing a <program> object that can
174-
be used for sending and recieving data over the standard file descriptors.
175-
The resource will ensure that file descriptors are closed properly.
173+
The returned value is a boxed class containing a <program> object that can
174+
be used for sending and receiving data over the standard file descriptors.
175+
The class will ensure that file descriptors are closed properly.
176176
177177
# Arguments
178178
@@ -181,7 +181,7 @@ The resource will ensure that file descriptors are closed properly.
181181
182182
# Return value
183183
184-
A boxed resource of <program>
184+
A class with a <program> field
185185
"]
186186
fn start_program(prog: str, args: [str]) -> program {
187187
let pipe_input = os::pipe();
@@ -221,16 +221,20 @@ fn start_program(prog: str, args: [str]) -> program {
221221
libc::fclose(r.out_file);
222222
libc::fclose(r.err_file);
223223
}
224-
resource prog_res(r: prog_repr) { destroy_repr(r); }
224+
class prog_res {
225+
let r: prog_repr;
226+
new(-r: prog_repr) { self.r = r; }
227+
drop { destroy_repr(self.r); }
228+
}
225229

226230
impl of program for prog_res {
227-
fn get_id() -> pid_t { ret self.pid; }
228-
fn input() -> io::writer { io::fd_writer(self.in_fd, false) }
229-
fn output() -> io::reader { io::FILE_reader(self.out_file, false) }
230-
fn err() -> io::reader { io::FILE_reader(self.err_file, false) }
231-
fn close_input() { close_repr_input(*self); }
232-
fn finish() -> int { finish_repr(*self) }
233-
fn destroy() { destroy_repr(*self); }
231+
fn get_id() -> pid_t { ret self.r.pid; }
232+
fn input() -> io::writer { io::fd_writer(self.r.in_fd, false) }
233+
fn output() -> io::reader { io::FILE_reader(self.r.out_file, false) }
234+
fn err() -> io::reader { io::FILE_reader(self.r.err_file, false) }
235+
fn close_input() { close_repr_input(self.r); }
236+
fn finish() -> int { finish_repr(self.r) }
237+
fn destroy() { destroy_repr(self.r); }
234238
}
235239
let repr = {pid: pid,
236240
mut in_fd: pipe_input.out,

0 commit comments

Comments
 (0)