]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unsupported/process.rs
Rollup merge of #105684 - GuillaumeGomez:improve-rustdoc-var-name, r=notriddle
[rust.git] / library / std / src / sys / unsupported / process.rs
1 use crate::ffi::OsStr;
2 use crate::fmt;
3 use crate::io;
4 use crate::marker::PhantomData;
5 use crate::num::NonZeroI32;
6 use crate::path::Path;
7 use crate::sys::fs::File;
8 use crate::sys::pipe::AnonPipe;
9 use crate::sys::unsupported;
10 use crate::sys_common::process::{CommandEnv, CommandEnvs};
11
12 pub use crate::ffi::OsString as EnvKey;
13
14 ////////////////////////////////////////////////////////////////////////////////
15 // Command
16 ////////////////////////////////////////////////////////////////////////////////
17
18 pub struct Command {
19     env: CommandEnv,
20 }
21
22 // passed back to std::process with the pipes connected to the child, if any
23 // were requested
24 pub struct StdioPipes {
25     pub stdin: Option<AnonPipe>,
26     pub stdout: Option<AnonPipe>,
27     pub stderr: Option<AnonPipe>,
28 }
29
30 pub enum Stdio {
31     Inherit,
32     Null,
33     MakePipe,
34 }
35
36 impl Command {
37     pub fn new(_program: &OsStr) -> Command {
38         Command { env: Default::default() }
39     }
40
41     pub fn arg(&mut self, _arg: &OsStr) {}
42
43     pub fn env_mut(&mut self) -> &mut CommandEnv {
44         &mut self.env
45     }
46
47     pub fn cwd(&mut self, _dir: &OsStr) {}
48
49     pub fn stdin(&mut self, _stdin: Stdio) {}
50
51     pub fn stdout(&mut self, _stdout: Stdio) {}
52
53     pub fn stderr(&mut self, _stderr: Stdio) {}
54
55     pub fn get_program(&self) -> &OsStr {
56         panic!("unsupported")
57     }
58
59     pub fn get_args(&self) -> CommandArgs<'_> {
60         CommandArgs { _p: PhantomData }
61     }
62
63     pub fn get_envs(&self) -> CommandEnvs<'_> {
64         self.env.iter()
65     }
66
67     pub fn get_current_dir(&self) -> Option<&Path> {
68         None
69     }
70
71     pub fn spawn(
72         &mut self,
73         _default: Stdio,
74         _needs_stdin: bool,
75     ) -> io::Result<(Process, StdioPipes)> {
76         unsupported()
77     }
78 }
79
80 impl From<AnonPipe> for Stdio {
81     fn from(pipe: AnonPipe) -> Stdio {
82         pipe.diverge()
83     }
84 }
85
86 impl From<File> for Stdio {
87     fn from(_file: File) -> Stdio {
88         panic!("unsupported")
89     }
90 }
91
92 impl fmt::Debug for Command {
93     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
94         Ok(())
95     }
96 }
97
98 pub struct ExitStatus(!);
99
100 impl ExitStatus {
101     pub fn exit_ok(&self) -> Result<(), ExitStatusError> {
102         self.0
103     }
104
105     pub fn code(&self) -> Option<i32> {
106         self.0
107     }
108 }
109
110 impl Clone for ExitStatus {
111     fn clone(&self) -> ExitStatus {
112         self.0
113     }
114 }
115
116 impl Copy for ExitStatus {}
117
118 impl PartialEq for ExitStatus {
119     fn eq(&self, _other: &ExitStatus) -> bool {
120         self.0
121     }
122 }
123
124 impl Eq for ExitStatus {}
125
126 impl fmt::Debug for ExitStatus {
127     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
128         self.0
129     }
130 }
131
132 impl fmt::Display for ExitStatus {
133     fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
134         self.0
135     }
136 }
137
138 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
139 pub struct ExitStatusError(ExitStatus);
140
141 impl Into<ExitStatus> for ExitStatusError {
142     fn into(self) -> ExitStatus {
143         self.0.0
144     }
145 }
146
147 impl ExitStatusError {
148     pub fn code(self) -> Option<NonZeroI32> {
149         self.0.0
150     }
151 }
152
153 #[derive(PartialEq, Eq, Clone, Copy, Debug)]
154 pub struct ExitCode(bool);
155
156 impl ExitCode {
157     pub const SUCCESS: ExitCode = ExitCode(false);
158     pub const FAILURE: ExitCode = ExitCode(true);
159
160     pub fn as_i32(&self) -> i32 {
161         self.0 as i32
162     }
163 }
164
165 impl From<u8> for ExitCode {
166     fn from(code: u8) -> Self {
167         match code {
168             0 => Self::SUCCESS,
169             1..=255 => Self::FAILURE,
170         }
171     }
172 }
173
174 pub struct Process(!);
175
176 impl Process {
177     pub fn id(&self) -> u32 {
178         self.0
179     }
180
181     pub fn kill(&mut self) -> io::Result<()> {
182         self.0
183     }
184
185     pub fn wait(&mut self) -> io::Result<ExitStatus> {
186         self.0
187     }
188
189     pub fn try_wait(&mut self) -> io::Result<Option<ExitStatus>> {
190         self.0
191     }
192 }
193
194 pub struct CommandArgs<'a> {
195     _p: PhantomData<&'a ()>,
196 }
197
198 impl<'a> Iterator for CommandArgs<'a> {
199     type Item = &'a OsStr;
200     fn next(&mut self) -> Option<&'a OsStr> {
201         None
202     }
203     fn size_hint(&self) -> (usize, Option<usize>) {
204         (0, Some(0))
205     }
206 }
207
208 impl<'a> ExactSizeIterator for CommandArgs<'a> {}
209
210 impl<'a> fmt::Debug for CommandArgs<'a> {
211     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
212         f.debug_list().finish()
213     }
214 }