Skip to content

Commit a35e65a

Browse files
committed
std: add Output::exit_ok
approved in ACP rust-lang/libs-team#554
1 parent ae9173d commit a35e65a

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

+24
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,30 @@ 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 equivelent 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+
#[unstable(feature = "exit_status_error", issue = "84908")]
1307+
pub fn exit_ok(self) -> Result<Self, ExitStatusError> {
1308+
self.status.exit_ok()?;
1309+
self
1310+
}
1311+
}
1312+
12891313
// If either stderr or stdout are valid utf8 strings it prints the valid
12901314
// strings, otherwise it prints the byte sequence instead
12911315
#[stable(feature = "process_output_debug", since = "1.7.0")]

0 commit comments

Comments
 (0)