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