Skip to content

Commit 738215d

Browse files
committed
Amjad50/OS: Added support for ExitStatusExt
This allows us to create `ExitStatus` struct
1 parent c450aeb commit 738215d

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

library/std/src/os/amjad_os/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
pub mod ffi;
44
pub mod io;
5+
pub mod process;
56

67
/// A prelude for conveniently writing platform-specific code.
78
///
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

library/std/src/sys/amjad_os/process.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,17 @@ impl ExitStatus {
297297
pub fn code(&self) -> Option<i32> {
298298
Some(self.0)
299299
}
300+
301+
pub fn into_raw(&self) -> i32 {
302+
self.0
303+
}
304+
}
305+
306+
/// Converts a raw `i32` to a type-safe `ExitStatus` by wrapping it without copying.
307+
impl From<i32> for ExitStatus {
308+
fn from(a: i32) -> ExitStatus {
309+
ExitStatus(a)
310+
}
300311
}
301312

302313
impl fmt::Display for ExitStatus {

0 commit comments

Comments
 (0)