1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
14 #[phase(syntax)] extern crate green;
16 use std::string::String;
21 fn print_complements() {
22 let all = [Blue, Red, Yellow];
23 for aa in all.iter() {
24 for bb in all.iter() {
25 println!("{} + {} -> {}", *aa, *bb, transform(*aa, *bb));
30 enum Color { Red, Yellow, Blue }
31 impl fmt::Show for Color {
32 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
33 let str = match *self {
47 fn show_color_list(set: Vec<Color>) -> String {
48 let mut out = String::new();
49 for col in set.iter() {
51 out.push_str(col.to_str().as_slice());
56 fn show_digit(nn: uint) -> &'static str {
68 _ => {fail!("expected digits from 0 to 9...")}
73 impl fmt::Show for Number {
74 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
76 let Number(mut num) = *self;
77 if num == 0 { out.push(show_digit(0)) };
82 let s = show_digit(dig);
86 for s in out.iter().rev() {
87 try!(write!(f, "{}", s))
93 fn transform(aa: Color, bb: Color) -> Color {
95 (Red, Red ) => { Red }
96 (Red, Yellow) => { Blue }
97 (Red, Blue ) => { Yellow }
98 (Yellow, Red ) => { Blue }
99 (Yellow, Yellow) => { Yellow }
100 (Yellow, Blue ) => { Red }
101 (Blue, Red ) => { Yellow }
102 (Blue, Yellow) => { Red }
103 (Blue, Blue ) => { Blue }
110 from_rendezvous: Receiver<CreatureInfo>,
111 to_rendezvous: Sender<CreatureInfo>,
112 to_rendezvous_log: Sender<String>
114 let mut creatures_met = 0;
115 let mut evil_clones_met = 0;
116 let mut rendezvous = from_rendezvous.iter();
120 to_rendezvous.send(CreatureInfo {name: name, color: color});
122 // log and change, or quit
123 match rendezvous.next() {
124 Some(other_creature) => {
125 color = transform(color, other_creature.color);
127 // track some statistics
129 if other_creature.name == name {
130 evil_clones_met += 1;
136 // log creatures met and evil clones of self
137 let report = format_strbuf!("{}{}",
139 Number(evil_clones_met));
140 to_rendezvous_log.send(report);
143 fn rendezvous(nn: uint, set: Vec<Color>) {
144 // these ports will allow us to hear from the creatures
145 let (to_rendezvous, from_creatures) = channel::<CreatureInfo>();
147 // these channels will be passed to the creatures so they can talk to us
148 let (to_rendezvous_log, from_creatures_log) = channel::<String>();
150 // these channels will allow us to talk to each creature by 'name'/index
151 let mut to_creature: Vec<Sender<CreatureInfo>> =
152 set.iter().enumerate().map(|(ii, &col)| {
153 // create each creature as a listener with a port, and
154 // give us a channel to talk to each
155 let to_rendezvous = to_rendezvous.clone();
156 let to_rendezvous_log = to_rendezvous_log.clone();
157 let (to_creature, from_rendezvous) = channel();
168 let mut creatures_met = 0;
170 // set up meetings...
171 for _ in range(0, nn) {
172 let fst_creature = from_creatures.recv();
173 let snd_creature = from_creatures.recv();
177 to_creature.get_mut(fst_creature.name).send(snd_creature);
178 to_creature.get_mut(snd_creature.name).send(fst_creature);
181 // tell each creature to stop
184 // print each color in the set
185 println!("{}", show_color_list(set));
187 // print each creature's stats
188 drop(to_rendezvous_log);
189 for rep in from_creatures_log.iter() {
193 // print the total number of creatures met
194 println!("{}\n", Number(creatures_met));
198 let nn = if std::os::getenv("RUST_BENCH").is_some() {
201 std::os::args().as_slice()
203 .and_then(|arg| from_str(arg.as_slice()))
210 rendezvous(nn, vec!(Blue, Red, Yellow));
213 vec!(Blue, Red, Yellow, Red, Yellow, Blue, Red, Yellow, Red, Blue));