Skip to content

Commit d6b93c1

Browse files
committed
fork: Make Parent a struct-like variant
This changes `Fork(pid_t)` to `Fork { child: pid_t }` to make the meaning of the field clearer. For most uses, it can be bound as match fork().unwrap() { Parent { child } => { ... } Child => { ... } } using the shorthand matching syntax for struct-like enum variants. This is a breaking change.
1 parent 4eb5918 commit d6b93c1

File tree

4 files changed

+20
-18
lines changed

4 files changed

+20
-18
lines changed

src/unistd.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ pub use self::linux::*;
1313

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

@@ -27,7 +29,7 @@ impl Fork {
2729

2830
pub fn is_parent(&self) -> bool {
2931
match *self {
30-
Fork::Parent(_) => true,
32+
Fork::Parent {..} => true,
3133
_ => false
3234
}
3335
}
@@ -38,7 +40,7 @@ pub fn fork() -> Result<Fork> {
3840

3941
Errno::result(res).map(|res| match res {
4042
0 => Fork::Child,
41-
res => Fork::Parent(res)
43+
res => Fork::Parent { child: res }
4244
})
4345
}
4446

test/sys/test_wait.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use libc::exit;
88
fn test_wait_signal() {
99
match fork() {
1010
Ok(Child) => loop { /* Wait for signal */ },
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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)