Skip to content

Commit

Permalink
std::process: impl From<io::Stdout> (etc.) for Stdio
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
  • Loading branch information
ijackson committed Aug 7, 2023
1 parent 1bab95b commit 36295fa
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions library/std/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1491,6 +1491,66 @@ impl From<fs::File> for Stdio {
}
}

#[stable(feature = "stdio_from_stdio", since = "CURRENT_RUSTC_VERSION")]
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 = "CURRENT_RUSTC_VERSION")]
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.
Expand Down

0 comments on commit 36295fa

Please sign in to comment.