Skip to content

Commit a784997

Browse files
committed
auto merge of #6044 : Dretch/rust/run-remove-progrepr, r=graydon
...which was a legacy from the time when destructors were implemented using resource types.
2 parents ee3789b + 345d5b8 commit a784997

File tree

1 file changed

+50
-67
lines changed

1 file changed

+50
-67
lines changed

src/libcore/run.rs

+50-67
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,48 @@ pub struct RunProgramResult {
4747
handle: *(),
4848
}
4949

50-
struct ProgRepr {
51-
pid: pid_t,
52-
handle: *(),
53-
in_fd: c_int,
54-
out_file: *libc::FILE,
55-
err_file: *libc::FILE,
56-
finished: bool,
50+
/// A value representing a child process
51+
pub struct Program {
52+
priv pid: pid_t,
53+
priv handle: *(),
54+
priv in_fd: c_int,
55+
priv out_file: *libc::FILE,
56+
priv err_file: *libc::FILE,
57+
priv finished: bool,
58+
}
59+
60+
impl Drop for Program {
61+
fn finalize(&self) {
62+
// FIXME #4943: transmute is bad.
63+
let mut_self: &mut Program = unsafe { cast::transmute(self) };
64+
65+
mut_self.finish();
66+
mut_self.close_outputs();
67+
free_handle(self.handle);
68+
}
5769
}
5870

59-
impl ProgRepr {
71+
pub impl Program {
72+
73+
/// Returns the process id of the program
74+
fn get_id(&mut self) -> pid_t { self.pid }
75+
76+
/// Returns an io::Writer that can be used to write to stdin
77+
fn input(&mut self) -> @io::Writer {
78+
io::fd_writer(self.in_fd, false)
79+
}
80+
81+
/// Returns an io::Reader that can be used to read from stdout
82+
fn output(&mut self) -> @io::Reader {
83+
io::FILE_reader(self.out_file, false)
84+
}
85+
86+
/// Returns an io::Reader that can be used to read from stderr
87+
fn err(&mut self) -> @io::Reader {
88+
io::FILE_reader(self.err_file, false)
89+
}
90+
91+
/// Closes the handle to the child processes standard input
6092
fn close_input(&mut self) {
6193
let invalid_fd = -1i32;
6294
if self.in_fd != invalid_fd {
@@ -67,21 +99,25 @@ impl ProgRepr {
6799
}
68100
}
69101

70-
fn close_outputs(&mut self) {
102+
priv fn close_outputs(&mut self) {
71103
unsafe {
72104
fclose_and_null(&mut self.out_file);
73105
fclose_and_null(&mut self.err_file);
74106
}
75107
}
76108

109+
/**
110+
* Waits for the child process to terminate. Closes the handle
111+
* to stdin if necessary.
112+
*/
77113
fn finish(&mut self) -> int {
78114
if self.finished { return 0; }
79115
self.finished = true;
80116
self.close_input();
81117
return waitpid(self.pid);
82118
}
83119

84-
fn destroy(&mut self, force: bool) {
120+
priv fn destroy_internal(&mut self, force: bool) {
85121
killpid(self.pid, force);
86122
self.finish();
87123
self.close_outputs();
@@ -107,57 +143,6 @@ impl ProgRepr {
107143
}
108144
}
109145
}
110-
}
111-
112-
/// A value representing a child process
113-
pub struct Program {
114-
priv r: ProgRepr,
115-
}
116-
117-
impl Drop for Program {
118-
fn finalize(&self) {
119-
// FIXME #4943: transmute is bad.
120-
let selfr: &mut ProgRepr = unsafe { cast::transmute(&self.r) };
121-
122-
selfr.finish();
123-
selfr.close_outputs();
124-
free_handle(self.r.handle);
125-
}
126-
}
127-
128-
pub impl Program {
129-
priv fn new(r: ProgRepr) -> Program {
130-
Program {
131-
r: r
132-
}
133-
}
134-
135-
/// Returns the process id of the program
136-
fn get_id(&mut self) -> pid_t { self.r.pid }
137-
138-
/// Returns an io::Writer that can be used to write to stdin
139-
fn input(&mut self) -> @io::Writer {
140-
io::fd_writer(self.r.in_fd, false)
141-
}
142-
143-
/// Returns an io::Reader that can be used to read from stdout
144-
fn output(&mut self) -> @io::Reader {
145-
io::FILE_reader(self.r.out_file, false)
146-
}
147-
148-
/// Returns an io::Reader that can be used to read from stderr
149-
fn err(&mut self) -> @io::Reader {
150-
io::FILE_reader(self.r.err_file, false)
151-
}
152-
153-
/// Closes the handle to the child processes standard input
154-
fn close_input(&mut self) { self.r.close_input(); }
155-
156-
/**
157-
* Waits for the child process to terminate. Closes the handle
158-
* to stdin if necessary.
159-
*/
160-
fn finish(&mut self) -> int { self.r.finish() }
161146

162147
/**
163148
* Terminate the program, giving it a chance to clean itself up if
@@ -166,7 +151,7 @@ pub impl Program {
166151
* On Posix OSs SIGTERM will be sent to the process. On Win32
167152
* TerminateProcess(..) will be called.
168153
*/
169-
fn destroy(&mut self) { self.r.destroy(false); }
154+
fn destroy(&mut self) { self.destroy_internal(false); }
170155

171156
/**
172157
* Terminate the program as soon as possible without giving it a
@@ -175,7 +160,7 @@ pub impl Program {
175160
* On Posix OSs SIGKILL will be sent to the process. On Win32
176161
* TerminateProcess(..) will be called.
177162
*/
178-
fn force_destroy(&mut self) { self.r.destroy(true); }
163+
fn force_destroy(&mut self) { self.destroy_internal(true); }
179164
}
180165

181166

@@ -366,16 +351,14 @@ pub fn start_program(prog: &str, args: &[~str]) -> Program {
366351
libc::close(pipe_err.out);
367352
}
368353

369-
let repr = ProgRepr {
354+
Program {
370355
pid: res.pid,
371356
handle: res.handle,
372357
in_fd: pipe_input.out,
373358
out_file: os::fdopen(pipe_output.in),
374359
err_file: os::fdopen(pipe_err.in),
375360
finished: false,
376-
};
377-
378-
Program::new(repr)
361+
}
379362
}
380363

381364
fn read_all(rd: @io::Reader) -> ~str {

0 commit comments

Comments
 (0)