summaryrefslogtreecommitdiffstats
path: root/library/std/src/process.rs
diff options
context:
space:
mode:
Diffstat (limited to 'library/std/src/process.rs')
-rw-r--r--library/std/src/process.rs210
1 files changed, 134 insertions, 76 deletions
diff --git a/library/std/src/process.rs b/library/std/src/process.rs
index 7380b45b0..8c1497613 100644
--- a/library/std/src/process.rs
+++ b/library/std/src/process.rs
@@ -12,9 +12,9 @@
//! use std::process::Command;
//!
//! let output = Command::new("echo")
-//! .arg("Hello world")
-//! .output()
-//! .expect("Failed to execute command");
+//! .arg("Hello world")
+//! .output()
+//! .expect("Failed to execute command");
//!
//! assert_eq!(b"Hello world\n", output.stdout.as_slice());
//! ```
@@ -101,7 +101,7 @@
#![stable(feature = "process", since = "1.0.0")]
#![deny(unsafe_op_in_unsafe_fn)]
-#[cfg(all(test, not(any(target_os = "emscripten", target_env = "sgx"))))]
+#[cfg(all(test, not(any(target_os = "emscripten", target_env = "sgx", target_os = "xous"))))]
mod tests;
use crate::io::prelude::*;
@@ -154,12 +154,11 @@ use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
/// use std::process::Command;
///
/// let mut child = Command::new("/bin/cat")
-/// .arg("file.txt")
-/// .spawn()
-/// .expect("failed to execute child");
+/// .arg("file.txt")
+/// .spawn()
+/// .expect("failed to execute child");
///
-/// let ecode = child.wait()
-/// .expect("failed to wait on child");
+/// let ecode = child.wait().expect("failed to wait on child");
///
/// assert!(ecode.success());
/// ```
@@ -481,15 +480,15 @@ impl fmt::Debug for ChildStderr {
///
/// let output = if cfg!(target_os = "windows") {
/// Command::new("cmd")
-/// .args(["/C", "echo hello"])
-/// .output()
-/// .expect("failed to execute process")
+/// .args(["/C", "echo hello"])
+/// .output()
+/// .expect("failed to execute process")
/// } else {
/// Command::new("sh")
-/// .arg("-c")
-/// .arg("echo hello")
-/// .output()
-/// .expect("failed to execute process")
+/// .arg("-c")
+/// .arg("echo hello")
+/// .output()
+/// .expect("failed to execute process")
/// };
///
/// let hello = output.stdout;
@@ -502,8 +501,7 @@ impl fmt::Debug for ChildStderr {
/// use std::process::Command;
///
/// let mut echo_hello = Command::new("sh");
-/// echo_hello.arg("-c")
-/// .arg("echo hello");
+/// echo_hello.arg("-c").arg("echo hello");
/// let hello_1 = echo_hello.output().expect("failed to execute process");
/// let hello_2 = echo_hello.output().expect("failed to execute process");
/// ```
@@ -576,8 +574,8 @@ impl Command {
/// use std::process::Command;
///
/// Command::new("sh")
- /// .spawn()
- /// .expect("sh command failed to start");
+ /// .spawn()
+ /// .expect("sh command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn new<S: AsRef<OsStr>>(program: S) -> Command {
@@ -620,10 +618,10 @@ impl Command {
/// use std::process::Command;
///
/// Command::new("ls")
- /// .arg("-l")
- /// .arg("-a")
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .arg("-l")
+ /// .arg("-a")
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
@@ -650,9 +648,9 @@ impl Command {
/// use std::process::Command;
///
/// Command::new("ls")
- /// .args(["-l", "-a"])
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .args(["-l", "-a"])
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn args<I, S>(&mut self, args: I) -> &mut Command
@@ -688,9 +686,9 @@ impl Command {
/// use std::process::Command;
///
/// Command::new("ls")
- /// .env("PATH", "/bin")
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .env("PATH", "/bin")
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
@@ -731,12 +729,12 @@ impl Command {
/// ).collect();
///
/// Command::new("printenv")
- /// .stdin(Stdio::null())
- /// .stdout(Stdio::inherit())
- /// .env_clear()
- /// .envs(&filtered_env)
- /// .spawn()
- /// .expect("printenv failed to start");
+ /// .stdin(Stdio::null())
+ /// .stdout(Stdio::inherit())
+ /// .env_clear()
+ /// .envs(&filtered_env)
+ /// .spawn()
+ /// .expect("printenv failed to start");
/// ```
#[stable(feature = "command_envs", since = "1.19.0")]
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
@@ -772,9 +770,9 @@ impl Command {
/// use std::process::Command;
///
/// Command::new("ls")
- /// .env_remove("PATH")
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .env_remove("PATH")
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
@@ -789,7 +787,7 @@ impl Command {
/// or [`Command::envs`]. In addition, it will prevent the spawned child process from inheriting
/// any environment variable from its parent process.
///
- /// After calling [`Command::env_remove`], the iterator from [`Command::get_envs`] will be
+ /// After calling [`Command::env_clear`], the iterator from [`Command::get_envs`] will be
/// empty.
///
/// You can use [`Command::env_remove`] to clear a single mapping.
@@ -802,9 +800,9 @@ impl Command {
/// use std::process::Command;
///
/// Command::new("ls")
- /// .env_clear()
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .env_clear()
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn env_clear(&mut self) -> &mut Command {
@@ -830,9 +828,9 @@ impl Command {
/// use std::process::Command;
///
/// Command::new("ls")
- /// .current_dir("/bin")
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .current_dir("/bin")
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
///
/// [`canonicalize`]: crate::fs::canonicalize
@@ -861,9 +859,9 @@ impl Command {
/// use std::process::{Command, Stdio};
///
/// Command::new("ls")
- /// .stdin(Stdio::null())
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .stdin(Stdio::null())
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
@@ -890,9 +888,9 @@ impl Command {
/// use std::process::{Command, Stdio};
///
/// Command::new("ls")
- /// .stdout(Stdio::null())
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .stdout(Stdio::null())
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
@@ -919,9 +917,9 @@ impl Command {
/// use std::process::{Command, Stdio};
///
/// Command::new("ls")
- /// .stderr(Stdio::null())
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .stderr(Stdio::null())
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
@@ -941,8 +939,8 @@ impl Command {
/// use std::process::Command;
///
/// Command::new("ls")
- /// .spawn()
- /// .expect("ls command failed to start");
+ /// .spawn()
+ /// .expect("ls command failed to start");
/// ```
#[stable(feature = "process", since = "1.0.0")]
pub fn spawn(&mut self) -> io::Result<Child> {
@@ -963,9 +961,9 @@ impl Command {
/// use std::process::Command;
/// use std::io::{self, Write};
/// let output = Command::new("/bin/cat")
- /// .arg("file.txt")
- /// .output()
- /// .expect("failed to execute process");
+ /// .arg("file.txt")
+ /// .output()
+ /// .expect("failed to execute process");
///
/// println!("status: {}", output.status);
/// io::stdout().write_all(&output.stdout).unwrap();
@@ -990,9 +988,9 @@ impl Command {
/// use std::process::Command;
///
/// let status = Command::new("/bin/cat")
- /// .arg("file.txt")
- /// .status()
- /// .expect("failed to execute process");
+ /// .arg("file.txt")
+ /// .status()
+ /// .expect("failed to execute process");
///
/// println!("process finished with: {status}");
///
@@ -1501,6 +1499,66 @@ impl From<fs::File> for Stdio {
}
}
+#[stable(feature = "stdio_from_stdio", since = "1.74.0")]
+impl From<io::Stdout> for Stdio {
+ /// Redirect command stdout/stderr to our stdout
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// #![feature(exit_status_error)]
+ /// use std::io;
+ /// use std::process::Command;
+ ///
+ /// # fn test() -> Result<(), Box<dyn std::error::Error>> {
+ /// let output = Command::new("whoami")
+ // "whoami" is a command which exists on both Unix and Windows,
+ // and which succeeds, producing some stdout output but no stderr.
+ /// .stdout(io::stdout())
+ /// .output()?;
+ /// output.status.exit_ok()?;
+ /// assert!(output.stdout.is_empty());
+ /// # Ok(())
+ /// # }
+ /// #
+ /// # if cfg!(unix) {
+ /// # test().unwrap();
+ /// # }
+ /// ```
+ fn from(inherit: io::Stdout) -> Stdio {
+ Stdio::from_inner(inherit.into())
+ }
+}
+
+#[stable(feature = "stdio_from_stdio", since = "1.74.0")]
+impl From<io::Stderr> for Stdio {
+ /// Redirect command stdout/stderr to our stderr
+ ///
+ /// # Examples
+ ///
+ /// ```rust
+ /// #![feature(exit_status_error)]
+ /// use std::io;
+ /// use std::process::Command;
+ ///
+ /// # fn test() -> Result<(), Box<dyn std::error::Error>> {
+ /// let output = Command::new("whoami")
+ /// .stdout(io::stderr())
+ /// .output()?;
+ /// output.status.exit_ok()?;
+ /// assert!(output.stdout.is_empty());
+ /// # Ok(())
+ /// # }
+ /// #
+ /// # if cfg!(unix) {
+ /// # test().unwrap();
+ /// # }
+ /// ```
+ fn from(inherit: io::Stderr) -> Stdio {
+ Stdio::from_inner(inherit.into())
+ }
+}
+
/// Describes the result of a process after it has terminated.
///
/// This `struct` is used to represent the exit status or other termination of a child process.
@@ -1558,9 +1616,9 @@ impl ExitStatus {
/// use std::process::Command;
///
/// let status = Command::new("ls")
- /// .arg("/dev/nonexistent")
- /// .status()
- /// .expect("ls could not be executed");
+ /// .arg("/dev/nonexistent")
+ /// .status()
+ /// .expect("ls could not be executed");
///
/// println!("ls: {status}");
/// status.exit_ok().expect_err("/dev/nonexistent could be listed!");
@@ -1580,9 +1638,9 @@ impl ExitStatus {
/// use std::process::Command;
///
/// let status = Command::new("mkdir")
- /// .arg("projects")
- /// .status()
- /// .expect("failed to execute mkdir");
+ /// .arg("projects")
+ /// .status()
+ /// .expect("failed to execute mkdir");
///
/// if status.success() {
/// println!("'projects/' directory created");
@@ -1613,13 +1671,13 @@ impl ExitStatus {
/// use std::process::Command;
///
/// let status = Command::new("mkdir")
- /// .arg("projects")
- /// .status()
- /// .expect("failed to execute 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")
+ /// None => println!("Process terminated by signal")
/// }
/// ```
#[must_use]
@@ -1749,9 +1807,9 @@ impl ExitStatusError {
}
#[unstable(feature = "exit_status_error", issue = "84908")]
-impl Into<ExitStatus> for ExitStatusError {
- fn into(self) -> ExitStatus {
- ExitStatus(self.0.into())
+impl From<ExitStatusError> for ExitStatus {
+ fn from(error: ExitStatusError) -> Self {
+ Self(error.0.into())
}
}