Skip to main content

chromiumoxide/
async_process.rs

1//! Internal module providing an async child process abstraction.
2
3use std::ffi::OsStr;
4use std::pin::Pin;
5pub use std::process::{ExitStatus, Stdio};
6use std::task::{Context, Poll};
7
8use tokio::process;
9
10#[derive(Debug)]
11pub struct Command {
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 stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
52        self.inner.stdout(cfg);
53        self
54    }
55
56    pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
57        self.inner.stderr(cfg);
58        self
59    }
60
61    pub fn spawn(&mut self) -> std::io::Result<Child> {
62        let inner = self.inner.spawn()?;
63        Ok(Child::new(inner))
64    }
65}
66
67#[derive(Debug)]
68pub struct Child {
69    pub stderr: Option<ChildStderr>,
70    pub inner: process::Child,
71}
72
73/// Wrapper for an async child process.
74impl Child {
75    fn new(mut inner: process::Child) -> Self {
76        let stderr = inner.stderr.take();
77        Self {
78            inner,
79            stderr: stderr.map(|inner| ChildStderr { inner }),
80        }
81    }
82
83    /// Kill the child process synchronously and asynchronously wait for the
84    /// child to exit
85    pub async fn kill(&mut self) -> std::io::Result<()> {
86        // Tokio already waits internally
87        self.inner.kill().await
88    }
89
90    /// Asynchronously wait for the child process to exit
91    pub async fn wait(&mut self) -> std::io::Result<ExitStatus> {
92        self.inner.wait().await
93    }
94
95    /// If the child process has exited, get its status
96    pub fn try_wait(&mut self) -> std::io::Result<Option<ExitStatus>> {
97        self.inner.try_wait()
98    }
99
100    /// Return a mutable reference to the inner process
101    ///
102    /// `stderr` may not be available.
103    pub fn as_mut_inner(&mut self) -> &mut process::Child {
104        &mut self.inner
105    }
106
107    /// Return the inner process
108    pub fn into_inner(self) -> process::Child {
109        let mut inner = self.inner;
110        inner.stderr = self.stderr.map(ChildStderr::into_inner);
111        inner
112    }
113}
114
115#[derive(Debug)]
116pub struct ChildStderr {
117    pub inner: process::ChildStderr,
118}
119
120impl ChildStderr {
121    pub fn into_inner(self) -> process::ChildStderr {
122        self.inner
123    }
124}
125
126impl futures::AsyncRead for ChildStderr {
127    fn poll_read(
128        mut self: Pin<&mut Self>,
129        cx: &mut Context<'_>,
130        buf: &mut [u8],
131    ) -> Poll<std::io::Result<usize>> {
132        let mut buf = tokio::io::ReadBuf::new(buf);
133        futures::ready!(tokio::io::AsyncRead::poll_read(
134            Pin::new(&mut self.inner),
135            cx,
136            &mut buf
137        ))?;
138        Poll::Ready(Ok(buf.filled().len()))
139    }
140}