Skip to content

Commit d179ea0

Browse files
authored
Rollup merge of #139554 - lolbinarycat:std-output-exit_ok, r=tgross35
std: add Output::exit_ok approved in ACP rust-lang/libs-team#554 Tracking issue: #84908
2 parents 1373a2f + 9676d4a commit d179ea0

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Diff for: library/std/src/process.rs

+34
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,40 @@ pub struct Output {
12861286
pub stderr: Vec<u8>,
12871287
}
12881288

1289+
impl Output {
1290+
/// Returns an error if a nonzero exit status was received.
1291+
///
1292+
/// If the [`Command`] exited successfully,
1293+
/// `self` is returned.
1294+
///
1295+
/// This is equivalent to calling [`exit_ok`](ExitStatus::exit_ok)
1296+
/// on [`Output.status`](Output::status).
1297+
///
1298+
/// Note that this will throw away the [`Output::stderr`] field in the error case.
1299+
/// If the child process outputs useful informantion to stderr, you can:
1300+
/// * Use `cmd.stderr(Stdio::inherit())` to forward the
1301+
/// stderr child process to the parent's stderr,
1302+
/// usually printing it to console where the user can see it.
1303+
/// This is usually correct for command-line applications.
1304+
/// * Capture `stderr` using a custom error type.
1305+
/// This is usually correct for libraries.
1306+
///
1307+
/// # Examples
1308+
///
1309+
/// ```
1310+
/// #![feature(exit_status_error)]
1311+
/// # #[cfg(unix)] {
1312+
/// use std::process::Command;
1313+
/// assert!(Command::new("false").output().unwrap().exit_ok().is_err());
1314+
/// # }
1315+
/// ```
1316+
#[unstable(feature = "exit_status_error", issue = "84908")]
1317+
pub fn exit_ok(self) -> Result<Self, ExitStatusError> {
1318+
self.status.exit_ok()?;
1319+
Ok(self)
1320+
}
1321+
}
1322+
12891323
// If either stderr or stdout are valid utf8 strings it prints the valid
12901324
// strings, otherwise it prints the byte sequence instead
12911325
#[stable(feature = "process_output_debug", since = "1.7.0")]

0 commit comments

Comments
 (0)