|
| 1 | +//! amjad_os-specific extensions to primitives in the [`std::process`] module. |
| 2 | +//! |
| 3 | +//! [`std::process`]: crate::process |
| 4 | +
|
| 5 | +#![stable(feature = "rust1", since = "1.0.0")] |
| 6 | + |
| 7 | +use crate::{ |
| 8 | + process, |
| 9 | + sealed::Sealed, |
| 10 | + sys_common::{AsInner, FromInner}, |
| 11 | +}; |
| 12 | + |
| 13 | +/// amjad_os extension to [`process::ExitStatus`] that is based on unix as below |
| 14 | +/// ... |
| 15 | +/// |
| 16 | +/// Unix-specific extensions to [`process::ExitStatus`] and |
| 17 | +/// [`ExitStatusError`](process::ExitStatusError). |
| 18 | +/// |
| 19 | +/// On Unix, `ExitStatus` **does not necessarily represent an exit status**, as |
| 20 | +/// passed to the `_exit` system call or returned by |
| 21 | +/// [`ExitStatus::code()`](crate::process::ExitStatus::code). It represents **any wait status** |
| 22 | +/// as returned by one of the `wait` family of system |
| 23 | +/// calls. |
| 24 | +/// |
| 25 | +/// A Unix wait status (a Rust `ExitStatus`) can represent a Unix exit status, but can also |
| 26 | +/// represent other kinds of process event. |
| 27 | +/// |
| 28 | +/// This trait is sealed: it cannot be implemented outside the standard library. |
| 29 | +/// This is so that future additional methods are not breaking changes. |
| 30 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 31 | +pub trait ExitStatusExt: Sealed { |
| 32 | + /// Creates a new `ExitStatus` or `ExitStatusError` from the raw underlying integer status |
| 33 | + /// value from `wait` |
| 34 | + /// |
| 35 | + /// The value should be a **wait status, not an exit status**. |
| 36 | + /// |
| 37 | + /// # Panics |
| 38 | + /// |
| 39 | + /// Panics on an attempt to make an `ExitStatusError` from a wait status of `0`. |
| 40 | + /// |
| 41 | + /// Making an `ExitStatus` always succeeds and never panics. |
| 42 | + #[stable(feature = "exit_status_from", since = "1.12.0")] |
| 43 | + fn from_raw(raw: i32) -> Self; |
| 44 | + |
| 45 | + /// Returns the underlying raw `wait` status. |
| 46 | + /// |
| 47 | + /// The returned integer is a **wait status, not an exit status**. |
| 48 | + #[stable(feature = "unix_process_wait_more", since = "1.58.0")] |
| 49 | + fn into_raw(self) -> i32; |
| 50 | +} |
| 51 | + |
| 52 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 53 | +impl ExitStatusExt for process::ExitStatus { |
| 54 | + fn from_raw(raw: i32) -> Self { |
| 55 | + process::ExitStatus::from_inner(From::from(raw)) |
| 56 | + } |
| 57 | + |
| 58 | + fn into_raw(self) -> i32 { |
| 59 | + self.as_inner().into_raw().into() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +#[unstable(feature = "exit_status_error", issue = "84908")] |
| 64 | +impl ExitStatusExt for process::ExitStatusError { |
| 65 | + fn from_raw(raw: i32) -> Self { |
| 66 | + process::ExitStatus::from_raw(raw) |
| 67 | + .exit_ok() |
| 68 | + .expect_err("<ExitStatusError as ExitStatusExt>::from_raw(0) but zero is not an error") |
| 69 | + } |
| 70 | + |
| 71 | + fn into_raw(self) -> i32 { |
| 72 | + self.into_status().into_raw() |
| 73 | + } |
| 74 | +} |
0 commit comments