arta_tokio/process/
command.rs

1use super::TokioChild;
2use crate::TokioGlobalRuntime;
3use arta::process::{ProcessRuntime, RuntimeCommand};
4use std::{
5    ffi::OsStr,
6    future::Future,
7    path::Path,
8    process::{ExitStatus, Output},
9};
10
11/// Tokio specific [`RuntimeCommand`] implementation.
12pub struct TokioCommand {
13    inner: tokio::process::Command,
14}
15
16impl RuntimeCommand for TokioCommand {
17    type Runtime = TokioGlobalRuntime;
18
19    fn new(program: impl AsRef<OsStr>) -> Self
20    where
21        Self: Sized,
22    {
23        Self {
24            inner: tokio::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<std::process::Stdio>) -> &mut Self {
67        self.inner.stdin(stdin);
68        self
69    }
70
71    fn stdout(&mut self, stdout: impl Into<std::process::Stdio>) -> &mut Self {
72        self.inner.stdout(stdout);
73        self
74    }
75
76    fn stderr(&mut self, stderr: impl Into<std::process::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.spawn().map(|child| TokioChild {
83            id: child.id().unwrap(),
84            inner: child,
85        })
86    }
87
88    fn output(&mut self) -> impl Future<Output = std::io::Result<Output>> + Send {
89        self.inner.output()
90    }
91
92    fn status(&mut self) -> impl Future<Output = std::io::Result<ExitStatus>> + Send {
93        self.inner.status()
94    }
95
96    #[cfg(unix)]
97    fn uid(&mut self, uid: u32) -> &mut Self {
98        self.inner.uid(uid);
99        self
100    }
101
102    #[cfg(unix)]
103    fn gid(&mut self, gid: u32) -> &mut Self {
104        self.inner.gid(gid);
105        self
106    }
107
108    #[cfg(unix)]
109    unsafe fn pre_exec(
110        &mut self,
111        f: impl FnMut() -> std::io::Result<()> + Send + Sync + 'static,
112    ) -> &mut Self {
113        self.inner.pre_exec(f);
114        self
115    }
116
117    #[cfg(windows)]
118    fn creation_flags(&mut self, flags: u32) -> &mut Self {
119        self.inner.creation_flags(flags);
120        self
121    }
122}