Skip to content

Commit 8ed8679

Browse files
committed
Auto merge of rust-lang#29897 - alexcrichton:process-wait-with-output, r=brson
Previously this function used channels but this isn't necessary any more now that threads have return values. This also has the added bonus of appropriately waiting for the thread to exit to ensure that the function doesn't still have running threads once it returns.
2 parents 50b969d + 3335366 commit 8ed8679

File tree

1 file changed

+14
-20
lines changed

1 file changed

+14
-20
lines changed

src/libstd/process.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,10 @@ use ffi::OsStr;
2020
use fmt;
2121
use io::{self, Error, ErrorKind};
2222
use path;
23-
use sync::mpsc::{channel, Receiver};
2423
use sys::pipe::{self, AnonPipe};
2524
use sys::process as imp;
2625
use sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
27-
use thread;
26+
use thread::{self, JoinHandle};
2827

2928
/// Representation of a running or exited child process.
3029
///
@@ -542,29 +541,24 @@ impl Child {
542541
#[stable(feature = "process", since = "1.0.0")]
543542
pub fn wait_with_output(mut self) -> io::Result<Output> {
544543
drop(self.stdin.take());
545-
fn read<T: Read + Send + 'static>(stream: Option<T>) -> Receiver<io::Result<Vec<u8>>> {
546-
let (tx, rx) = channel();
547-
match stream {
548-
Some(stream) => {
549-
thread::spawn(move || {
550-
let mut stream = stream;
551-
let mut ret = Vec::new();
552-
let res = stream.read_to_end(&mut ret);
553-
tx.send(res.map(|_| ret)).unwrap();
554-
});
555-
}
556-
None => tx.send(Ok(Vec::new())).unwrap()
557-
}
558-
rx
544+
fn read<R>(mut input: R) -> JoinHandle<io::Result<Vec<u8>>>
545+
where R: Read + Send + 'static
546+
{
547+
thread::spawn(move || {
548+
let mut ret = Vec::new();
549+
input.read_to_end(&mut ret).map(|_| ret)
550+
})
559551
}
560-
let stdout = read(self.stdout.take());
561-
let stderr = read(self.stderr.take());
552+
let stdout = self.stdout.take().map(read);
553+
let stderr = self.stderr.take().map(read);
562554
let status = try!(self.wait());
555+
let stdout = stdout.and_then(|t| t.join().unwrap().ok());
556+
let stderr = stderr.and_then(|t| t.join().unwrap().ok());
563557

564558
Ok(Output {
565559
status: status,
566-
stdout: stdout.recv().unwrap().unwrap_or(Vec::new()),
567-
stderr: stderr.recv().unwrap().unwrap_or(Vec::new()),
560+
stdout: stdout.unwrap_or(Vec::new()),
561+
stderr: stderr.unwrap_or(Vec::new()),
568562
})
569563
}
570564
}

0 commit comments

Comments
 (0)