Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Use enums better.
Before, we were using positive to mean take, negative to mean put, and zero to mean that we're done. Now, we have an Enum to replace all three states. This is way better, as it removes the weird `-x` and casting, and also makes intent much more clear.
- Loading branch information
Showing
1 changed file
with
20 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,12 +7,18 @@ enum PickupPermission { | ||
|
|
||
| struct Philosopher { | ||
| name: String, | ||
| sender: Sender<int>, | ||
| sender: Sender<PhilosopherAction>, | ||
| receiver: Receiver<PickupPermission>, | ||
| first_chopstick: uint, | ||
| second_chopstick: uint, | ||
| } | ||
|
|
||
| enum PhilosopherAction { | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
steveklabnik
via email
Author
Owner
|
||
| Take(uint), | ||
| Put(uint), | ||
| Sated, | ||
| } | ||
|
|
||
| impl Philosopher { | ||
| fn eat(&self) { | ||
| println!("{} has sat down to eat.", self.name); | ||
| @@ -25,14 +31,14 @@ impl Philosopher { | ||
| println!("{} is hungry.", self.name); | ||
|
|
||
| loop { | ||
| self.sender.send(self.first_chopstick as int); | ||
| self.sender.send(Take(self.first_chopstick)); | ||
| match self.receiver.recv() { Allowed => break, _ => {}} | ||
| } | ||
|
|
||
| println!("{} picked up their first chopstick.", self.name); | ||
|
|
||
| loop { | ||
| self.sender.send(self.second_chopstick as int); | ||
| self.sender.send(Take(self.second_chopstick)); | ||
| match self.receiver.recv() { Allowed => break, _ => {}} | ||
| } | ||
|
|
||
| @@ -43,20 +49,20 @@ impl Philosopher { | ||
|
|
||
| println!("{} is done eating.", self.name); | ||
|
|
||
| self.sender.send(self.first_chopstick as int * -1); | ||
| self.sender.send(Put(self.first_chopstick )); | ||
| println!("{} has put down their first chopstick.", self.name); | ||
| self.sender.send(self.second_chopstick as int * -1); | ||
| self.sender.send(Put(self.second_chopstick)); | ||
| println!("{} has put down their second chopstick.", self.name); | ||
| } | ||
|
|
||
| self.sender.send(0); | ||
| self.sender.send(Sated); | ||
|
|
||
| println!("{} is done with their meal.", self.name); | ||
| } | ||
|
|
||
| fn new(name: &str, | ||
| first_chopstick: uint, | ||
| second_chopstick: uint) -> (Philosopher, Sender<PickupPermission>, Receiver<int>) { | ||
| second_chopstick: uint) -> (Philosopher, Sender<PickupPermission>, Receiver<PhilosopherAction>) { | ||
| let (tx, rx) = channel(); | ||
| let (tx1, rx1) = channel(); | ||
|
|
||
| @@ -106,26 +112,26 @@ fn main() { | ||
|
|
||
| fn process_philosopher(chopsticks: &mut [bool, ..5], | ||
| tx: &Sender<PickupPermission>, | ||
| rx: &Receiver<int>, | ||
| rx: &Receiver<PhilosopherAction>, | ||
| remaining: &mut uint) { | ||
|
|
||
| let response = match rx.try_recv() { | ||
| Ok(i) => i, | ||
| Ok(action) => action, | ||
| Err(_) => return, | ||
| }; | ||
|
|
||
| match response { | ||
| 0 => { | ||
| Sated => { | ||
| *remaining += -1; | ||
| }, | ||
| x if x > 0 => { | ||
| if chopsticks[(x - 1) as uint] { | ||
| Take(x) => { | ||
| if chopsticks[x - 1] { | ||
| tx.send(NotAllowed); | ||
| } else { | ||
| chopsticks[(x - 1) as uint] = true; | ||
| chopsticks[x - 1] = true; | ||
| tx.send(Allowed); | ||
| } | ||
| }, | ||
| x => { chopsticks[((-x) - 1) as uint] = false; }, | ||
| Put(x) => { chopsticks[x - 1] = false; }, | ||
| } | ||
| } | ||
Now one
enumdecl is above the struct and other – below. Looks odd. I’d either put them both either above the struct or below the struct.