Skip to content

Commit 5221790

Browse files
committed
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.
2 parents 2fe4308 + 0b82e03 commit 5221790

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

src/libstd/process.rs

+88
Original file line numberDiff line numberDiff line change
@@ -1016,27 +1016,115 @@ impl fmt::Debug for Stdio {
10161016

10171017
#[stable(feature = "stdio_from", since = "1.20.0")]
10181018
impl From<ChildStdin> for Stdio {
1019+
/// Converts a `ChildStdin` into a `Stdio`
1020+
///
1021+
/// # Examples
1022+
///
1023+
/// `ChildStdin` will be converted to `Stdio` using `Stdio::from` under the hood.
1024+
///
1025+
/// ```rust
1026+
/// use std::process::{Command, Stdio};
1027+
///
1028+
/// let reverse = Command::new("rev")
1029+
/// .stdin(Stdio::piped())
1030+
/// .spawn()
1031+
/// .expect("failed reverse command");
1032+
///
1033+
/// let _echo = Command::new("echo")
1034+
/// .arg("Hello, world!")
1035+
/// .stdout(reverse.stdin.unwrap()) // Converted into a Stdio here
1036+
/// .output()
1037+
/// .expect("failed echo command");
1038+
///
1039+
/// // "!dlrow ,olleH" echoed to console
1040+
/// ```
10191041
fn from(child: ChildStdin) -> Stdio {
10201042
Stdio::from_inner(child.into_inner().into())
10211043
}
10221044
}
10231045

10241046
#[stable(feature = "stdio_from", since = "1.20.0")]
10251047
impl From<ChildStdout> for Stdio {
1048+
/// Converts a `ChildStdout` into a `Stdio`
1049+
///
1050+
/// # Examples
1051+
///
1052+
/// `ChildStdout` will be converted to `Stdio` using `Stdio::from` under the hood.
1053+
///
1054+
/// ```rust
1055+
/// use std::process::{Command, Stdio};
1056+
///
1057+
/// let hello = Command::new("echo")
1058+
/// .arg("Hello, world!")
1059+
/// .stdout(Stdio::piped())
1060+
/// .spawn()
1061+
/// .expect("failed echo command");
1062+
///
1063+
/// let reverse = Command::new("rev")
1064+
/// .stdin(hello.stdout.unwrap()) // Converted into a Stdio here
1065+
/// .output()
1066+
/// .expect("failed reverse command");
1067+
///
1068+
/// assert_eq!(reverse.stdout, b"!dlrow ,olleH\n");
1069+
/// ```
10261070
fn from(child: ChildStdout) -> Stdio {
10271071
Stdio::from_inner(child.into_inner().into())
10281072
}
10291073
}
10301074

10311075
#[stable(feature = "stdio_from", since = "1.20.0")]
10321076
impl From<ChildStderr> for Stdio {
1077+
/// Converts a `ChildStderr` into a `Stdio`
1078+
///
1079+
/// # Examples
1080+
///
1081+
/// ```rust,no_run
1082+
/// use std::process::{Command, Stdio};
1083+
///
1084+
/// let reverse = Command::new("rev")
1085+
/// .arg("non_existing_file.txt")
1086+
/// .stderr(Stdio::piped())
1087+
/// .spawn()
1088+
/// .expect("failed reverse command");
1089+
///
1090+
/// let cat = Command::new("cat")
1091+
/// .arg("-")
1092+
/// .stdin(reverse.stderr.unwrap()) // Converted into a Stdio here
1093+
/// .output()
1094+
/// .expect("failed echo command");
1095+
///
1096+
/// assert_eq!(
1097+
/// String::from_utf8_lossy(&cat.stdout),
1098+
/// "rev: cannot open non_existing_file.txt: No such file or directory\n"
1099+
/// );
1100+
/// ```
10331101
fn from(child: ChildStderr) -> Stdio {
10341102
Stdio::from_inner(child.into_inner().into())
10351103
}
10361104
}
10371105

10381106
#[stable(feature = "stdio_from", since = "1.20.0")]
10391107
impl From<fs::File> for Stdio {
1108+
/// Converts a `File` into a `Stdio`
1109+
///
1110+
/// # Examples
1111+
///
1112+
/// `File` will be converted to `Stdio` using `Stdio::from` under the hood.
1113+
///
1114+
/// ```rust,no_run
1115+
/// use std::fs::File;
1116+
/// use std::process::Command;
1117+
///
1118+
/// // With the `foo.txt` file containing `Hello, world!"
1119+
/// let file = File::open("foo.txt").unwrap();
1120+
///
1121+
/// let reverse = Command::new("rev")
1122+
/// .stdin(file) // Implicit File convertion into a Stdio
1123+
/// .output()
1124+
/// .expect("failed reverse command");
1125+
///
1126+
/// assert_eq!(reverse.stdout, b"!dlrow ,olleH");
1127+
/// ```
10401128
fn from(file: fs::File) -> Stdio {
10411129
Stdio::from_inner(file.into_inner().into())
10421130
}

0 commit comments

Comments
 (0)