@@ -1016,27 +1016,115 @@ impl fmt::Debug for Stdio {
1016
1016
1017
1017
#[ stable( feature = "stdio_from" , since = "1.20.0" ) ]
1018
1018
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
+ /// ```
1019
1041
fn from ( child : ChildStdin ) -> Stdio {
1020
1042
Stdio :: from_inner ( child. into_inner ( ) . into ( ) )
1021
1043
}
1022
1044
}
1023
1045
1024
1046
#[ stable( feature = "stdio_from" , since = "1.20.0" ) ]
1025
1047
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
+ /// ```
1026
1070
fn from ( child : ChildStdout ) -> Stdio {
1027
1071
Stdio :: from_inner ( child. into_inner ( ) . into ( ) )
1028
1072
}
1029
1073
}
1030
1074
1031
1075
#[ stable( feature = "stdio_from" , since = "1.20.0" ) ]
1032
1076
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
+ /// ```
1033
1101
fn from ( child : ChildStderr ) -> Stdio {
1034
1102
Stdio :: from_inner ( child. into_inner ( ) . into ( ) )
1035
1103
}
1036
1104
}
1037
1105
1038
1106
#[ stable( feature = "stdio_from" , since = "1.20.0" ) ]
1039
1107
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
+ /// ```
1040
1128
fn from ( file : fs:: File ) -> Stdio {
1041
1129
Stdio :: from_inner ( file. into_inner ( ) . into ( ) )
1042
1130
}
0 commit comments