diff --git a/src/lib.rs b/src/lib.rs index 16efa49d4..eced66ad6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -164,7 +164,7 @@ extern crate tokio_reactor; extern crate mio; use std::io::{self, Read, Write}; -use std::process::{self, ExitStatus, Output, Stdio}; +use std::process::{Command, ExitStatus, Output, Stdio}; use futures::{Future, Poll, IntoFuture}; use futures::future::{Either, ok}; @@ -320,25 +320,16 @@ pub trait CommandExt { } -impl CommandExt for process::Command { +impl CommandExt for Command { fn spawn_async_with_handle(&mut self, handle: &Handle) -> io::Result { - let mut child = Child { - child: imp::Child::new(try!(self.spawn()), handle), - stdin: None, - stdout: None, - stderr: None, - kill_on_drop: true, - }; - child.stdin = try!(child.child.register_stdin(handle)).map(|io| { - ChildStdin { inner: io } - }); - child.stdout = try!(child.child.register_stdout(handle)).map(|io| { - ChildStdout { inner: io } - }); - child.stderr = try!(child.child.register_stderr(handle)).map(|io| { - ChildStderr { inner: io } - }); - Ok(child) + imp::Child::new(self.spawn()?, handle) + .map(|(child, stdin, stdout, stderr)| Child { + child, + stdin: stdin.map(|inner| ChildStdin { inner }), + stdout: stdout.map(|inner| ChildStdout { inner }), + stderr: stderr.map(|inner| ChildStderr { inner }), + kill_on_drop: true, + }) } fn status_async_with_handle(&mut self, handle: &Handle) -> io::Result { diff --git a/src/unix/mod.rs b/src/unix/mod.rs index 1df1dd161..61980a531 100644 --- a/src/unix/mod.rs +++ b/src/unix/mod.rs @@ -58,27 +58,20 @@ impl fmt::Debug for Child { } impl Child { - pub fn new(inner: process::Child, handle: &Handle) -> Child { - Child { + pub fn new(mut inner: process::Child, handle: &Handle) + -> io::Result<(Child, Option, Option, Option)> + { + let stdin = stdio(inner.stdin.take(), handle)?; + let stdout = stdio(inner.stdout.take(), handle)?; + let stderr = stdio(inner.stderr.take(), handle)?; + + let child = Child { inner: inner, reaped: false, sigchld: Signal::with_handle(libc::SIGCHLD, handle).flatten_stream(), - } - } + }; - pub fn register_stdin(&mut self, handle: &Handle) - -> io::Result> { - stdio(self.inner.stdin.take(), handle) - } - - pub fn register_stdout(&mut self, handle: &Handle) - -> io::Result> { - stdio(self.inner.stdout.take(), handle) - } - - pub fn register_stderr(&mut self, handle: &Handle) - -> io::Result> { - stdio(self.inner.stderr.take(), handle) + Ok((child, stdin, stdout, stderr)) } pub fn id(&self) -> u32 { diff --git a/src/windows.rs b/src/windows.rs index 83e57f457..9fa5bdfe1 100644 --- a/src/windows.rs +++ b/src/windows.rs @@ -64,26 +64,19 @@ unsafe impl Sync for Waiting {} unsafe impl Send for Waiting {} impl Child { - pub fn new(child: process::Child, _handle: &Handle) -> Child { - Child { - child: child, + pub fn new(mut inner: process::Child, handle: &Handle) + -> io::Result<(Child, Option, Option, Option)> + { + let stdin = stdio(inner.stdin.take(), handle)?; + let stdout = stdio(inner.stdout.take(), handle)?; + let stderr = stdio(inner.stderr.take(), handle)?; + + let child = Child { + child: inner, waiting: None, - } - } + }; - pub fn register_stdin(&mut self, handle: &Handle) - -> io::Result> { - stdio(self.child.stdin.take(), handle) - } - - pub fn register_stdout(&mut self, handle: &Handle) - -> io::Result> { - stdio(self.child.stdout.take(), handle) - } - - pub fn register_stderr(&mut self, handle: &Handle) - -> io::Result> { - stdio(self.child.stderr.take(), handle) + Ok((child, stdin, stdout, stderr)) } pub fn id(&self) -> u32 {