Skip to content

Commit

Permalink
fork: Make Parent a struct-like variant
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kamalmarhubi committed Mar 29, 2016
1 parent 4eb5918 commit d6b93c1
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
8 changes: 5 additions & 3 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ pub use self::linux::*;

#[derive(Clone, Copy)]
pub enum Fork {
Parent(pid_t),
Parent {
child: pid_t
},
Child
}

Expand All @@ -27,7 +29,7 @@ impl Fork {

pub fn is_parent(&self) -> bool {
match *self {
Fork::Parent(_) => true,
Fork::Parent {..} => true,
_ => false
}
}
Expand All @@ -38,7 +40,7 @@ pub fn fork() -> Result<Fork> {

Errno::result(res).map(|res| match res {
0 => Fork::Child,
res => Fork::Parent(res)
res => Fork::Parent { child: res }
})
}

Expand Down
10 changes: 5 additions & 5 deletions test/sys/test_wait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use libc::exit;
fn test_wait_signal() {
match fork() {
Ok(Child) => loop { /* Wait for signal */ },
Ok(Parent(child_pid)) => {
kill(child_pid, SIGKILL).ok().expect("Error: Kill Failed");
assert_eq!(waitpid(child_pid, None), Ok(WaitStatus::Signaled(child_pid, SIGKILL, false)));
Ok(Parent { child }) => {
kill(child, SIGKILL).ok().expect("Error: Kill Failed");
assert_eq!(waitpid(child, None), Ok(WaitStatus::Signaled(child, SIGKILL, false)));
},
// panic, fork should never fail unless there is a serious problem with the OS
Err(_) => panic!("Error: Fork Failed")
Expand All @@ -21,8 +21,8 @@ fn test_wait_signal() {
fn test_wait_exit() {
match fork() {
Ok(Child) => unsafe { exit(12); },
Ok(Parent(child_pid)) => {
assert_eq!(waitpid(child_pid, None), Ok(WaitStatus::Exited(child_pid, 12)));
Ok(Parent { child }) => {
assert_eq!(waitpid(child, None), Ok(WaitStatus::Exited(child, 12)));
},
// panic, fork should never fail unless there is a serious problem with the OS
Err(_) => panic!("Error: Fork Failed")
Expand Down
4 changes: 2 additions & 2 deletions test/test_mq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ fn test_mq_send_and_receive() {
write(writer, &buf).unwrap(); // pipe result to parent process. Otherwise cargo does not report test failures correctly
mq_close(mqd_in_child).unwrap();
}
Ok(Parent(child_pid)) => {
Ok(Parent { child }) => {
mq_close(mqd_in_parent).unwrap();

// Wait for the child to exit.
waitpid(child_pid, None).unwrap();
waitpid(child, None).unwrap();
// Read 1024 bytes.
let mut read_buf = [0u8; 32];
read(reader, &mut read_buf).unwrap();
Expand Down
16 changes: 8 additions & 8 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ fn test_fork_and_waitpid() {
let pid = fork();
match pid {
Ok(Child) => {} // ignore child here
Ok(Parent(child_pid)) => {
Ok(Parent { child }) => {
// assert that child was created and pid > 0
assert!(child_pid > 0);
let wait_status = waitpid(child_pid, None);
assert!(child > 0);
let wait_status = waitpid(child, None);
match wait_status {
// assert that waitpid returned correct status and the pid is the one of the child
Ok(WaitStatus::Exited(pid_t, _)) => assert!(pid_t == child_pid),
Ok(WaitStatus::Exited(pid_t, _)) => assert!(pid_t == child),

// panic, must never happen
Ok(_) => panic!("Child still alive, should never happen"),
Expand All @@ -34,11 +34,11 @@ fn test_wait() {
let pid = fork();
match pid {
Ok(Child) => {} // ignore child here
Ok(Parent(child_pid)) => {
Ok(Parent { child }) => {
let wait_status = wait();

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

0 comments on commit d6b93c1

Please sign in to comment.