chromiumoxide/
async_process.rs

1//! Internal module providing an async child process abstraction for `async-std` or `tokio`.
2
3use ::tokio::process;
4use std::ffi::OsStr;
5use std::pin::Pin;
6pub use std::process::{ExitStatus, Stdio};
7use std::task::{Context, Poll};
8
9#[derive(Debug)]
10pub struct Command {
11    inner: process::Command,
12}
13
14impl Command {
15    pub fn new<S: AsRef<OsStr>>(program: S) -> Self {
16        let mut inner = process::Command::new(program);
17        // Since the kill and/or wait methods are async, we can't call
18        // explicitely in the Drop implementation. We MUST rely on the
19        // runtime implemetation which is already designed to deal with
20        // this case where the user didn't explicitely kill the child
21        // process before dropping the handle.
22        inner.kill_on_drop(true);
23        Self { inner }
24    }
25
26    pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
27        self.inner.arg(arg);
28        self
29    }
30
31    pub fn args<I, S>(&mut self, args: I) -> &mut Self
32    where
33        I: IntoIterator<Item = S>,
34        S: AsRef<OsStr>,
35    {
36        self.inner.args(args);
37        self
38    }
39
40    pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
41    where
42        I: IntoIterator<Item = (K, V)>,
43        K: AsRef<OsStr>,
44        V: AsRef<OsStr>,
45    {
46        self.inner.envs(vars);
47        self
48    }
49
50    pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
51        self.inner.stderr(cfg);
52        self
53    }
54
55    pub fn spawn(&mut self) -> std::io::Result<Child> {
56        let inner = self.inner.spawn()?;
57        Ok(Child::new(inner))
58    }
59}
60
61#[derive(Debug)]
62pub struct Child {
63    pub stderr: Option<ChildStderr>,
64    pub inner: process::Child,
65}
66
67/// Wrapper for an async child process.
68///
69/// The inner implementation depends on the selected async runtime (features `async-std-runtime`
70/// or `tokio-runtime`).
71impl Child {
72    fn new(mut inner: process::Child) -> Self {
73        let stderr = inner.stderr.take();
74        Self {
75            inner,
76            stderr: stderr.map(|inner| ChildStderr { inner }),
77        }
78    }
79
80    /// Kill the child process synchronously and asynchronously wait for the
81    /// child to exit
82    pub async fn kill(&mut self) -> std::io::Result<()> {
83        self.inner.kill().await
84    }
85
86    /// Asynchronously wait for the child process to exit
87    pub async fn wait(&mut self) -> std::io::Result<ExitStatus> {
88        self.inner.wait().await
89    }
90
91    /// If the child process has exited, get its status
92    pub fn try_wait(&mut self) -> std::io::Result<Option<ExitStatus>> {
93        self.inner.try_wait()
94    }
95
96    /// Return a mutable reference to the inner process
97    ///
98    /// `stderr` may not be available.
99    pub fn as_mut_inner(&mut self) -> &mut process::Child {
100        &mut self.inner
101    }
102
103    /// Return the inner process
104    pub fn into_inner(self) -> process::Child {
105        let mut inner = self.inner;
106        inner.stderr = self.stderr.map(ChildStderr::into_inner);
107        inner
108    }
109}
110
111#[derive(Debug)]
112pub struct ChildStderr {
113    pub inner: process::ChildStderr,
114}
115
116impl ChildStderr {
117    pub fn into_inner(self) -> process::ChildStderr {
118        self.inner
119    }
120}
121
122impl futures::AsyncRead for ChildStderr {
123    fn poll_read(
124        mut self: Pin<&mut Self>,
125        cx: &mut Context<'_>,
126        buf: &mut [u8],
127    ) -> Poll<std::io::Result<usize>> {
128        let mut buf = tokio::io::ReadBuf::new(buf);
129        futures::ready!(tokio::io::AsyncRead::poll_read(
130            Pin::new(&mut self.inner),
131            cx,
132            &mut buf
133        ))?;
134        Poll::Ready(Ok(buf.filled().len()))
135    }
136}