arta_async_std/process/
command.rs

1use super::AsyncStdChild;
2use crate::AsyncStdGlobalRuntime;
3use arta::process::{ProcessRuntime, RuntimeCommand};
4use futures::prelude::Future;
5use std::{
6    ffi::OsStr,
7    path::Path,
8    process::{ExitStatus, Output, Stdio},
9};
10
11/// Async-std specific [`RuntimeCommand`] implementation.
12pub struct AsyncStdCommand {
13    inner: async_std::process::Command,
14}
15
16impl RuntimeCommand for AsyncStdCommand {
17    type Runtime = AsyncStdGlobalRuntime;
18
19    fn new(program: impl AsRef<OsStr>) -> Self
20    where
21        Self: Sized,
22    {
23        Self {
24            inner: async_std::process::Command::new(program),
25        }
26    }
27
28    fn arg(&mut self, arg: impl AsRef<OsStr>) -> &mut Self {
29        self.inner.arg(arg);
30        self
31    }
32
33    fn args(&mut self, args: impl Iterator<Item = impl AsRef<OsStr>>) -> &mut Self {
34        self.inner.args(args);
35        self
36    }
37
38    fn env(&mut self, key: impl AsRef<OsStr>, value: impl AsRef<OsStr>) -> &mut Self {
39        self.inner.env(key, value);
40        self
41    }
42
43    fn envs(
44        &mut self,
45        vars: impl Iterator<Item = (impl AsRef<OsStr>, impl AsRef<OsStr>)>,
46    ) -> &mut Self {
47        self.inner.envs(vars);
48        self
49    }
50
51    fn env_remove(&mut self, key: impl AsRef<OsStr>) -> &mut Self {
52        self.inner.env_remove(key);
53        self
54    }
55
56    fn env_clear(&mut self) -> &mut Self {
57        self.inner.env_clear();
58        self
59    }
60
61    fn current_dir(&mut self, dir: impl AsRef<Path>) -> &mut Self {
62        self.inner.current_dir(dir);
63        self
64    }
65
66    fn stdin(&mut self, stdin: impl Into<Stdio>) -> &mut Self {
67        self.inner.stdin(stdin);
68        self
69    }
70
71    fn stdout(&mut self, stdout: impl Into<Stdio>) -> &mut Self {
72        self.inner.stdout(stdout);
73        self
74    }
75
76    fn stderr(&mut self, stderr: impl Into<Stdio>) -> &mut Self {
77        self.inner.stderr(stderr);
78        self
79    }
80
81    fn spawn(&mut self) -> std::io::Result<<Self::Runtime as ProcessRuntime>::Child> {
82        self.inner
83            .spawn()
84            .map(|child| AsyncStdChild { inner: child })
85    }
86
87    fn output(&mut self) -> impl Future<Output = std::io::Result<Output>> + Send {
88        self.inner.output()
89    }
90
91    fn status(&mut self) -> impl Future<Output = std::io::Result<ExitStatus>> + Send {
92        self.inner.status()
93    }
94
95    #[cfg(unix)]
96    fn uid(&mut self, uid: u32) -> &mut Self {
97        use async_std::os::unix::process::CommandExt;
98
99        self.inner.uid(uid);
100        self
101    }
102
103    #[cfg(unix)]
104    fn gid(&mut self, gid: u32) -> &mut Self {
105        use async_std::os::unix::process::CommandExt;
106
107        self.inner.gid(gid);
108        self
109    }
110
111    #[cfg(unix)]
112    unsafe fn pre_exec(
113        &mut self,
114        f: impl FnMut() -> std::io::Result<()> + Send + Sync + 'static,
115    ) -> &mut Self {
116        use async_std::os::unix::process::CommandExt;
117
118        self.inner.pre_exec(f);
119        self
120    }
121
122    #[cfg(windows)]
123    fn creation_flags(&mut self, flags: u32) -> &mut Self {
124        use async_std::os::windows::process::CommandExt;
125
126        self.inner.creation_flags(flags);
127        self
128    }
129}