Rollup merge of #55200 - octronics:gh51430, r=kennytm
Documents `From` implementations for `Stdio` This PR solves part of #51430 by adding a basic summary and an example to each `impl From` inside `process` module (`ChildStdin`, `ChildStdout`, `ChildStderr`, `File`). It does not document if the conversions allocate memory and how expensive they are.
This commit is contained in:
commit
49eb443b3b
@ -1016,6 +1016,28 @@ impl fmt::Debug for Stdio {
|
||||
|
||||
#[stable(feature = "stdio_from", since = "1.20.0")]
|
||||
impl From<ChildStdin> for Stdio {
|
||||
/// Converts a `ChildStdin` into a `Stdio`
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// `ChildStdin` will be converted to `Stdio` using `Stdio::from` under the hood.
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::process::{Command, Stdio};
|
||||
///
|
||||
/// let reverse = Command::new("rev")
|
||||
/// .stdin(Stdio::piped())
|
||||
/// .spawn()
|
||||
/// .expect("failed reverse command");
|
||||
///
|
||||
/// let _echo = Command::new("echo")
|
||||
/// .arg("Hello, world!")
|
||||
/// .stdout(reverse.stdin.unwrap()) // Converted into a Stdio here
|
||||
/// .output()
|
||||
/// .expect("failed echo command");
|
||||
///
|
||||
/// // "!dlrow ,olleH" echoed to console
|
||||
/// ```
|
||||
fn from(child: ChildStdin) -> Stdio {
|
||||
Stdio::from_inner(child.into_inner().into())
|
||||
}
|
||||
@ -1023,6 +1045,28 @@ impl From<ChildStdin> for Stdio {
|
||||
|
||||
#[stable(feature = "stdio_from", since = "1.20.0")]
|
||||
impl From<ChildStdout> for Stdio {
|
||||
/// Converts a `ChildStdout` into a `Stdio`
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// `ChildStdout` will be converted to `Stdio` using `Stdio::from` under the hood.
|
||||
///
|
||||
/// ```rust
|
||||
/// use std::process::{Command, Stdio};
|
||||
///
|
||||
/// let hello = Command::new("echo")
|
||||
/// .arg("Hello, world!")
|
||||
/// .stdout(Stdio::piped())
|
||||
/// .spawn()
|
||||
/// .expect("failed echo command");
|
||||
///
|
||||
/// let reverse = Command::new("rev")
|
||||
/// .stdin(hello.stdout.unwrap()) // Converted into a Stdio here
|
||||
/// .output()
|
||||
/// .expect("failed reverse command");
|
||||
///
|
||||
/// assert_eq!(reverse.stdout, b"!dlrow ,olleH\n");
|
||||
/// ```
|
||||
fn from(child: ChildStdout) -> Stdio {
|
||||
Stdio::from_inner(child.into_inner().into())
|
||||
}
|
||||
@ -1030,6 +1074,30 @@ impl From<ChildStdout> for Stdio {
|
||||
|
||||
#[stable(feature = "stdio_from", since = "1.20.0")]
|
||||
impl From<ChildStderr> for Stdio {
|
||||
/// Converts a `ChildStderr` into a `Stdio`
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use std::process::{Command, Stdio};
|
||||
///
|
||||
/// let reverse = Command::new("rev")
|
||||
/// .arg("non_existing_file.txt")
|
||||
/// .stderr(Stdio::piped())
|
||||
/// .spawn()
|
||||
/// .expect("failed reverse command");
|
||||
///
|
||||
/// let cat = Command::new("cat")
|
||||
/// .arg("-")
|
||||
/// .stdin(reverse.stderr.unwrap()) // Converted into a Stdio here
|
||||
/// .output()
|
||||
/// .expect("failed echo command");
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// String::from_utf8_lossy(&cat.stdout),
|
||||
/// "rev: cannot open non_existing_file.txt: No such file or directory\n"
|
||||
/// );
|
||||
/// ```
|
||||
fn from(child: ChildStderr) -> Stdio {
|
||||
Stdio::from_inner(child.into_inner().into())
|
||||
}
|
||||
@ -1037,6 +1105,26 @@ impl From<ChildStderr> for Stdio {
|
||||
|
||||
#[stable(feature = "stdio_from", since = "1.20.0")]
|
||||
impl From<fs::File> for Stdio {
|
||||
/// Converts a `File` into a `Stdio`
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// `File` will be converted to `Stdio` using `Stdio::from` under the hood.
|
||||
///
|
||||
/// ```rust,no_run
|
||||
/// use std::fs::File;
|
||||
/// use std::process::Command;
|
||||
///
|
||||
/// // With the `foo.txt` file containing `Hello, world!"
|
||||
/// let file = File::open("foo.txt").unwrap();
|
||||
///
|
||||
/// let reverse = Command::new("rev")
|
||||
/// .stdin(file) // Implicit File convertion into a Stdio
|
||||
/// .output()
|
||||
/// .expect("failed reverse command");
|
||||
///
|
||||
/// assert_eq!(reverse.stdout, b"!dlrow ,olleH");
|
||||
/// ```
|
||||
fn from(file: fs::File) -> Stdio {
|
||||
Stdio::from_inner(file.into_inner().into())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user