1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// This file was automatically generated by `elaborate`.
// https://github.com/trailofbits/elaborate
#[allow(unused_imports)]
use anyhow::Context;
pub trait ChildContext {
/// Attempts to collect the exit status of the child if it has already
/// exited.
///
/// This function will not block the calling thread and will only
/// check to see if the child process has exited or not. If the child has
/// exited then on Unix the process ID is reaped. This function is
/// guaranteed to repeatedly return a successful exit status so long as the
/// child has already exited.
///
/// If the child has exited, then `Ok(Some(status))` is returned. If the
/// exit status is not available at this time then `Ok(None)` is returned.
/// If an error occurs, then that error is returned.
///
/// Note that unlike `wait`, this function will not attempt to drop stdin.
///
/// # Examples
///
/// ```no_run
/// use std::process::Command;
///
/// let mut child = Command::new("ls").spawn()?;
///
/// match child.try_wait() {
/// Ok(Some(status)) => println!("exited with: {status}"),
/// Ok(None) => {
/// println!("status not ready yet, let's really wait");
/// let res = child.wait();
/// println!("result: {res:?}");
/// }
/// Err(e) => println!("error attempting to wait: {e}"),
/// }
/// # std::io::Result::Ok(())
/// ```
fn try_wait_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < core :: option :: Option < std :: process :: ExitStatus > > );
/// Forces the child process to exit. If the child has already exited, `Ok(())`
/// is returned.
///
/// The mapping to [`ErrorKind`]s is not part of the compatibility contract of the function.
///
/// This is equivalent to sending a SIGKILL on Unix platforms.
///
/// # Examples
///
/// ```no_run
/// use std::process::Command;
///
/// let mut command = Command::new("yes");
/// if let Ok(mut child) = command.spawn() {
/// child.kill().expect("command couldn't be killed");
/// } else {
/// println!("yes command didn't start");
/// }
/// ```
///
/// [`ErrorKind`]: io::ErrorKind
/// [`InvalidInput`]: io::ErrorKind::InvalidInput
fn kill_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < ( ) > );
/// Simultaneously waits for the child to exit and collect all remaining
/// output on the stdout/stderr handles, returning an `Output`
/// instance.
///
/// The stdin handle to the child process, if any, will be closed
/// before waiting. This helps avoid deadlock: it ensures that the
/// child does not block waiting for input from the parent, while
/// the parent waits for the child to exit.
///
/// By default, stdin, stdout and stderr are inherited from the parent.
/// In order to capture the output into this `Result<Output>` it is
/// necessary to create new pipes between parent and child. Use
/// `stdout(Stdio::piped())` or `stderr(Stdio::piped())`, respectively.
///
/// # Examples
///
/// ```should_panic
/// use std::process::{Command, Stdio};
///
/// let child = Command::new("/bin/cat")
/// .arg("file.txt")
/// .stdout(Stdio::piped())
/// .spawn()
/// .expect("failed to execute child");
///
/// let output = child
/// .wait_with_output()
/// .expect("failed to wait on child");
///
/// assert!(output.status.success());
/// ```
fn wait_with_output_wc ( self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: Output > );
/// Waits for the child to exit completely, returning the status that it
/// exited with. This function will continue to have the same return value
/// after it has been called at least once.
///
/// The stdin handle to the child process, if any, will be closed
/// before waiting. This helps avoid deadlock: it ensures that the
/// child does not block waiting for input from the parent, while
/// the parent waits for the child to exit.
///
/// # Examples
///
/// ```no_run
/// use std::process::Command;
///
/// let mut command = Command::new("ls");
/// if let Ok(mut child) = command.spawn() {
/// child.wait().expect("command wasn't running");
/// println!("Child has finished its execution!");
/// } else {
/// println!("ls command didn't start");
/// }
/// ```
fn wait_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: ExitStatus > );
}
impl ChildContext for std :: process :: Child {
fn try_wait_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < core :: option :: Option < std :: process :: ExitStatus > > ) {
std :: process :: Child :: try_wait(self)
.with_context(|| crate::call_failed!(Some(self), "try_wait"))
}
fn kill_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < ( ) > ) {
std :: process :: Child :: kill(self)
.with_context(|| crate::call_failed!(Some(self), "kill"))
}
fn wait_with_output_wc ( self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: Output > ) {
std :: process :: Child :: wait_with_output(self)
.with_context(|| crate::call_failed!(Some(crate::CustomDebugMessage("value of type std::process::Child")), "wait_with_output"))
}
fn wait_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: ExitStatus > ) {
std :: process :: Child :: wait(self)
.with_context(|| crate::call_failed!(Some(self), "wait"))
}
}
pub trait CommandContext {
/// Executes a command as a child process, waiting for it to finish and
/// collecting its status.
///
/// By default, stdin, stdout and stderr are inherited from the parent.
///
/// # Examples
///
/// ```should_panic
/// use std::process::Command;
///
/// let status = Command::new("/bin/cat")
/// .arg("file.txt")
/// .status()
/// .expect("failed to execute process");
///
/// println!("process finished with: {status}");
///
/// assert!(status.success());
/// ```
fn status_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: ExitStatus > );
/// Executes the command as a child process, returning a handle to it.
///
/// By default, stdin, stdout and stderr are inherited from the parent.
///
/// # Examples
///
/// ```no_run
/// use std::process::Command;
///
/// Command::new("ls")
/// .spawn()
/// .expect("ls command failed to start");
/// ```
fn spawn_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: Child > );
/// Executes the command as a child process, waiting for it to finish and
/// collecting all of its output.
///
/// By default, stdout and stderr are captured (and used to provide the
/// resulting output). Stdin is not inherited from the parent and any
/// attempt by the child process to read from the stdin stream will result
/// in the stream immediately closing.
///
/// # Examples
///
/// ```should_panic
/// use std::process::Command;
/// use std::io::{self, Write};
/// let output = Command::new("/bin/cat")
/// .arg("file.txt")
/// .output()?;
///
/// println!("status: {}", output.status);
/// io::stdout().write_all(&output.stdout)?;
/// io::stderr().write_all(&output.stderr)?;
///
/// assert!(output.status.success());
/// # io::Result::Ok(())
/// ```
fn output_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: Output > );
/// Returns the working directory for the child process.
///
/// This returns [`None`] if the working directory will not be changed.
///
/// # Examples
///
/// ```
/// use std::path::Path;
/// use std::process::Command;
///
/// let mut cmd = Command::new("ls");
/// assert_eq!(cmd.get_current_dir(), None);
/// cmd.current_dir("/bin");
/// assert_eq!(cmd.get_current_dir(), Some(Path::new("/bin")));
/// ```
fn get_current_dir_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: option :: Option < & std :: path :: Path > );
}
impl CommandContext for std :: process :: Command {
fn status_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: ExitStatus > ) {
std :: process :: Command :: status(self)
.with_context(|| crate::call_failed!(Some(self), "status"))
}
fn spawn_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: Child > ) {
std :: process :: Command :: spawn(self)
.with_context(|| crate::call_failed!(Some(self), "spawn"))
}
fn output_wc ( & mut self ) -> crate :: rewrite_output_type ! ( std :: io :: Result < std :: process :: Output > ) {
std :: process :: Command :: output(self)
.with_context(|| crate::call_failed!(Some(self), "output"))
}
fn get_current_dir_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: option :: Option < & std :: path :: Path > ) {
std :: process :: Command :: get_current_dir(self)
.with_context(|| crate::call_failed!(Some(self), "get_current_dir"))
}
}
pub trait ExitStatusContext {
/// Returns the exit code of the process, if any.
///
/// In Unix terms the return value is the **exit status**: the value passed to `exit`, if the
/// process finished by calling `exit`. Note that on Unix the exit status is truncated to 8
/// bits, and that values that didn't come from a program's call to `exit` may be invented by the
/// runtime system (often, for example, 255, 254, 127 or 126).
///
/// On Unix, this will return `None` if the process was terminated by a signal.
/// [`ExitStatusExt`](crate::os::unix::process::ExitStatusExt) is an
/// extension trait for extracting any such signal, and other details, from the `ExitStatus`.
///
/// # Examples
///
/// ```no_run
/// use std::process::Command;
///
/// let status = Command::new("mkdir")
/// .arg("projects")
/// .status()
/// .expect("failed to execute mkdir");
///
/// match status.code() {
/// Some(code) => println!("Exited with status code: {code}"),
/// None => println!("Process terminated by signal")
/// }
/// ```
fn code_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: option :: Option < i32 > );
/// Was termination successful? Returns a `Result`.
///
/// # Examples
///
/// ```
/// #![feature(exit_status_error)]
/// # if cfg!(all(unix, not(all(target_vendor = "apple", not(target_os = "macos"))))) {
/// use std::process::Command;
///
/// let status = Command::new("ls")
/// .arg("/dev/nonexistent")
/// .status()
/// .expect("ls could not be executed");
///
/// println!("ls: {status}");
/// status.exit_ok().expect_err("/dev/nonexistent could be listed!");
/// # } // cfg!(unix)
/// ```
#[cfg(feature = "exit_status_error")]
fn exit_ok_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: result :: Result < ( ) , std :: process :: ExitStatusError > );
}
impl ExitStatusContext for std :: process :: ExitStatus {
fn code_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: option :: Option < i32 > ) {
std :: process :: ExitStatus :: code(self)
.with_context(|| crate::call_failed!(Some(self), "code"))
}
#[cfg(feature = "exit_status_error")]
fn exit_ok_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: result :: Result < ( ) , std :: process :: ExitStatusError > ) {
std :: process :: ExitStatus :: exit_ok(self)
.with_context(|| crate::call_failed!(Some(self), "exit_ok"))
}
}
#[cfg(feature = "exit_status_error")]
pub trait ExitStatusErrorContext {
/// Reports the exit code, if applicable, from an `ExitStatusError`, as a [`NonZero`].
///
/// This is exactly like [`code()`](Self::code), except that it returns a <code>[NonZero]<[i32]></code>.
///
/// Plain `code`, returning a plain integer, is provided because it is often more convenient.
/// The returned value from `code()` is indeed also nonzero; use `code_nonzero()` when you want
/// a type-level guarantee of nonzeroness.
///
/// # Examples
///
/// ```
/// #![feature(exit_status_error)]
///
/// # if cfg!(all(unix, not(target_os = "android"), not(all(target_vendor = "apple", not(target_os = "macos"))))) {
/// use std::num::NonZero;
/// use std::process::Command;
///
/// let bad = Command::new("false").status().unwrap().exit_ok().unwrap_err();
/// assert_eq!(bad.code_nonzero().unwrap(), NonZero::new(1).unwrap());
/// # } // cfg!(unix)
/// ```
fn code_nonzero_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: option :: Option < core :: num :: NonZero < i32 > > );
/// Reports the exit code, if applicable, from an `ExitStatusError`.
///
/// In Unix terms the return value is the **exit status**: the value passed to `exit`, if the
/// process finished by calling `exit`. Note that on Unix the exit status is truncated to 8
/// bits, and that values that didn't come from a program's call to `exit` may be invented by the
/// runtime system (often, for example, 255, 254, 127 or 126).
///
/// On Unix, this will return `None` if the process was terminated by a signal. If you want to
/// handle such situations specially, consider using methods from
/// [`ExitStatusExt`](crate::os::unix::process::ExitStatusExt).
///
/// If the process finished by calling `exit` with a nonzero value, this will return
/// that exit status.
///
/// If the error was something else, it will return `None`.
///
/// If the process exited successfully (ie, by calling `exit(0)`), there is no
/// `ExitStatusError`. So the return value from `ExitStatusError::code()` is always nonzero.
///
/// # Examples
///
/// ```
/// #![feature(exit_status_error)]
/// # #[cfg(all(unix, not(target_os = "android"), not(all(target_vendor = "apple", not(target_os = "macos")))))] {
/// use std::process::Command;
///
/// let bad = Command::new("false").status().unwrap().exit_ok().unwrap_err();
/// assert_eq!(bad.code(), Some(1));
/// # } // #[cfg(unix)]
/// ```
fn code_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: option :: Option < i32 > );
}
#[cfg(feature = "exit_status_error")]
impl ExitStatusErrorContext for std :: process :: ExitStatusError {
fn code_nonzero_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: option :: Option < core :: num :: NonZero < i32 > > ) {
std :: process :: ExitStatusError :: code_nonzero(self)
.with_context(|| crate::call_failed!(Some(self), "code_nonzero"))
}
fn code_wc ( & self ) -> crate :: rewrite_output_type ! ( core :: option :: Option < i32 > ) {
std :: process :: ExitStatusError :: code(self)
.with_context(|| crate::call_failed!(Some(self), "code"))
}
}
#[cfg(feature = "exit_status_error")]
pub trait OutputContext: Sized {
/// Returns an error if a nonzero exit status was received.
///
/// If the [`Command`] exited successfully,
/// `self` is returned.
///
/// This is equivalent to calling [`exit_ok`](ExitStatus::exit_ok)
/// on [`Output.status`](Output::status).
///
/// Note that this will throw away the [`Output::stderr`] field in the error case.
/// If the child process outputs useful informantion to stderr, you can:
/// * Use `cmd.stderr(Stdio::inherit())` to forward the
/// stderr child process to the parent's stderr,
/// usually printing it to console where the user can see it.
/// This is usually correct for command-line applications.
/// * Capture `stderr` using a custom error type.
/// This is usually correct for libraries.
///
/// # Examples
///
/// ```
/// #![feature(exit_status_error)]
/// # #[cfg(all(unix, not(target_os = "android"), not(all(target_vendor = "apple", not(target_os = "macos")))))] {
/// use std::process::Command;
/// assert!(Command::new("false").output().unwrap().exit_ok().is_err());
/// # }
/// ```
fn exit_ok_wc ( self ) -> crate :: rewrite_output_type ! ( core :: result :: Result < Self , std :: process :: ExitStatusError > );
}
#[cfg(feature = "exit_status_error")]
impl OutputContext for std :: process :: Output {
fn exit_ok_wc ( self ) -> crate :: rewrite_output_type ! ( core :: result :: Result < Self , std :: process :: ExitStatusError > ) {
std :: process :: Output :: exit_ok(self)
.with_context(|| crate::call_failed!(Some(crate::CustomDebugMessage("value of type std::process::Output")), "exit_ok"))
}
}