This repository has been archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 255
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
347 additions
and
538 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,61 @@ | ||
use std::io::{Read, Write}; | ||
use std::io; | ||
use std::pin::Pin; | ||
use std::process::{Command, Stdio}; | ||
use std::rc::Rc; | ||
use std::task::{Context, Poll}; | ||
|
||
use futures::Poll; | ||
use tokio::io::{AsyncRead, AsyncWrite}; | ||
use tokio_process::{Child, CommandExt}; | ||
|
||
pub struct ChildProcess { | ||
stdin: tokio_process::ChildStdin, | ||
stdout: tokio_process::ChildStdout, | ||
child: Rc<tokio_process::Child>, | ||
stdin: tokio::process::ChildStdin, | ||
stdout: tokio::process::ChildStdout, | ||
child: Rc<tokio::process::Child>, | ||
} | ||
|
||
impl Read for ChildProcess { | ||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
Read::read(&mut self.stdout, buf) | ||
impl AsyncRead for ChildProcess { | ||
fn poll_read( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &mut [u8], | ||
) -> Poll<io::Result<usize>> { | ||
Pin::new(&mut self.stdout).poll_read(cx, buf) | ||
} | ||
} | ||
|
||
impl Write for ChildProcess { | ||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { | ||
Write::write(&mut self.stdin, buf) | ||
impl AsyncWrite for ChildProcess { | ||
fn poll_write( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut Context<'_>, | ||
buf: &[u8], | ||
) -> Poll<io::Result<usize>> { | ||
Pin::new(&mut self.stdin).poll_write(cx, buf) | ||
} | ||
fn flush(&mut self) -> std::io::Result<()> { | ||
Write::flush(&mut self.stdin) | ||
|
||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
Pin::new(&mut self.stdin).poll_flush(cx) | ||
} | ||
} | ||
|
||
impl AsyncRead for ChildProcess {} | ||
impl AsyncWrite for ChildProcess { | ||
fn shutdown(&mut self) -> Poll<(), std::io::Error> { | ||
AsyncWrite::shutdown(&mut self.stdin) | ||
fn poll_shutdown(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> { | ||
Pin::new(&mut self.stdin).poll_shutdown(cx) | ||
} | ||
} | ||
|
||
impl ChildProcess { | ||
pub fn spawn_from_command(mut cmd: Command) -> Result<ChildProcess, std::io::Error> { | ||
cmd.stdin(Stdio::piped()); | ||
cmd.stdout(Stdio::piped()); | ||
let mut child = cmd.spawn_async()?; | ||
let mut child = tokio::process::Command::from(cmd).spawn().expect("to async spawn process"); | ||
|
||
Ok(ChildProcess { | ||
stdout: child.stdout().take().unwrap(), | ||
stdin: child.stdin().take().unwrap(), | ||
stdout: child.stdout.take().unwrap(), | ||
stdin: child.stdin.take().unwrap(), | ||
child: Rc::new(child), | ||
}) | ||
} | ||
|
||
/// Returns a handle to the underlying `Child` process. | ||
/// Useful when waiting until child process exits. | ||
pub fn child(&self) -> Rc<Child> { | ||
pub fn child(&self) -> Rc<tokio::process::Child> { | ||
Rc::clone(&self.child) | ||
} | ||
} |
Oops, something went wrong.