Skip to content

Commit c2acb9e

Browse files
committed
unistd: Redesign the enum returned by fork()
This commit changes the name of the enum returned by `fork()` to `ForkResult`, and changes the `Parent` variant to be struct-like. The result can be matched like use nix::unistd::ForkResult::*; match fork().unwrap() { Parent { child } => { ... } Child => { ... } } using the shorthand matching syntax for struct-like enum variants. This is a breaking change.
1 parent a09d07b commit c2acb9e

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

src/unistd.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,33 @@ use std::os::unix::io::RawFd;
1212
pub use self::linux::*;
1313

1414
#[derive(Clone, Copy)]
15-
pub enum Fork {
16-
Parent(pid_t),
15+
pub enum ForkResult {
16+
Parent {
17+
child: pid_t
18+
},
1719
Child
1820
}
1921

20-
impl Fork {
22+
impl ForkResult {
2123
pub fn is_child(&self) -> bool {
2224
match *self {
23-
Fork::Child => true,
25+
ForkResult::Child => true,
2426
_ => false
2527
}
2628
}
2729

2830
pub fn is_parent(&self) -> bool {
29-
match *self {
30-
Fork::Parent(_) => true,
31-
_ => false
32-
}
31+
!self.is_child()
3332
}
3433
}
3534

36-
pub fn fork() -> Result<Fork> {
35+
pub fn fork() -> Result<ForkResult> {
36+
use self::ForkResult::*;
3737
let res = unsafe { libc::fork() };
3838

3939
Errno::result(res).map(|res| match res {
40-
0 => Fork::Child,
41-
res => Fork::Parent(res)
40+
0 => Child,
41+
res => Parent { child: res }
4242
})
4343
}
4444

test/sys/test_wait.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use nix::unistd::*;
2-
use nix::unistd::Fork::*;
2+
use nix::unistd::ForkResult::*;
33
use nix::sys::signal::*;
44
use nix::sys::wait::*;
55
use libc::exit;
@@ -8,9 +8,9 @@ use libc::exit;
88
fn test_wait_signal() {
99
match fork() {
1010
Ok(Child) => pause().unwrap_or(()),
11-
Ok(Parent(child_pid)) => {
12-
kill(child_pid, SIGKILL).ok().expect("Error: Kill Failed");
13-
assert_eq!(waitpid(child_pid, None), Ok(WaitStatus::Signaled(child_pid, SIGKILL, false)));
11+
Ok(Parent { child }) => {
12+
kill(child, SIGKILL).ok().expect("Error: Kill Failed");
13+
assert_eq!(waitpid(child, None), Ok(WaitStatus::Signaled(child, SIGKILL, false)));
1414
},
1515
// panic, fork should never fail unless there is a serious problem with the OS
1616
Err(_) => panic!("Error: Fork Failed")
@@ -21,8 +21,8 @@ fn test_wait_signal() {
2121
fn test_wait_exit() {
2222
match fork() {
2323
Ok(Child) => unsafe { exit(12); },
24-
Ok(Parent(child_pid)) => {
25-
assert_eq!(waitpid(child_pid, None), Ok(WaitStatus::Exited(child_pid, 12)));
24+
Ok(Parent { child }) => {
25+
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 12)));
2626
},
2727
// panic, fork should never fail unless there is a serious problem with the OS
2828
Err(_) => panic!("Error: Fork Failed")

test/test_mq.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::str;
99
use libc::c_long;
1010

1111
use nix::unistd::{fork, read, write, pipe};
12-
use nix::unistd::Fork::{Child, Parent};
12+
use nix::unistd::ForkResult::*;
1313
use nix::sys::wait::*;
1414
use nix::errno::Errno::*;
1515
use nix::Error::Sys;
@@ -37,11 +37,11 @@ fn test_mq_send_and_receive() {
3737
write(writer, &buf).unwrap(); // pipe result to parent process. Otherwise cargo does not report test failures correctly
3838
mq_close(mqd_in_child).unwrap();
3939
}
40-
Ok(Parent(child_pid)) => {
40+
Ok(Parent { child }) => {
4141
mq_close(mqd_in_parent).unwrap();
4242

4343
// Wait for the child to exit.
44-
waitpid(child_pid, None).unwrap();
44+
waitpid(child, None).unwrap();
4545
// Read 1024 bytes.
4646
let mut read_buf = [0u8; 32];
4747
read(reader, &mut read_buf).unwrap();

test/test_unistd.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use nix::unistd::*;
2-
use nix::unistd::Fork::*;
2+
use nix::unistd::ForkResult::*;
33
use nix::sys::wait::*;
44
use std::ffi::CString;
55

@@ -8,13 +8,13 @@ fn test_fork_and_waitpid() {
88
let pid = fork();
99
match pid {
1010
Ok(Child) => {} // ignore child here
11-
Ok(Parent(child_pid)) => {
11+
Ok(Parent { child }) => {
1212
// assert that child was created and pid > 0
13-
assert!(child_pid > 0);
14-
let wait_status = waitpid(child_pid, None);
13+
assert!(child > 0);
14+
let wait_status = waitpid(child, None);
1515
match wait_status {
1616
// assert that waitpid returned correct status and the pid is the one of the child
17-
Ok(WaitStatus::Exited(pid_t, _)) => assert!(pid_t == child_pid),
17+
Ok(WaitStatus::Exited(pid_t, _)) => assert!(pid_t == child),
1818

1919
// panic, must never happen
2020
Ok(_) => panic!("Child still alive, should never happen"),
@@ -34,11 +34,11 @@ fn test_wait() {
3434
let pid = fork();
3535
match pid {
3636
Ok(Child) => {} // ignore child here
37-
Ok(Parent(child_pid)) => {
37+
Ok(Parent { child }) => {
3838
let wait_status = wait();
3939

4040
// just assert that (any) one child returns with WaitStatus::Exited
41-
assert_eq!(wait_status, Ok(WaitStatus::Exited(child_pid, 0)));
41+
assert_eq!(wait_status, Ok(WaitStatus::Exited(child, 0)));
4242
},
4343
// panic, fork should never fail unless there is a serious problem with the OS
4444
Err(_) => panic!("Error: Fork Failed")
@@ -95,9 +95,9 @@ macro_rules! execve_test_factory(
9595
&[CString::new(b"foo=bar".as_ref()).unwrap(),
9696
CString::new(b"baz=quux".as_ref()).unwrap()]).unwrap();
9797
},
98-
Parent(child_pid) => {
98+
Parent { child } => {
9999
// Wait for the child to exit.
100-
waitpid(child_pid, None).unwrap();
100+
waitpid(child, None).unwrap();
101101
// Read 1024 bytes.
102102
let mut buf = [0u8; 1024];
103103
read(reader, &mut buf).unwrap();

0 commit comments

Comments
 (0)