@@ -14,6 +14,8 @@ extern mod std;
1414use std:: map;
1515use std:: map:: HashMap ;
1616use std:: sort;
17+ use std:: cell:: Cell ;
18+ use core:: pipes:: * ;
1719
1820fn print_complements ( ) {
1921 let all = ~[ Blue , Red , Yellow ] ;
@@ -98,18 +100,18 @@ fn transform(aa: color, bb: color) -> color {
98100fn creature(
99101 name: uint,
100102 color: color,
101- from_rendezvous: oldcomm:: Port<Option<CreatureInfo>>,
102- to_rendezvous: oldcomm::Chan <CreatureInfo>,
103- to_rendezvous_log: oldcomm::Chan <~str>
103+ from_rendezvous: Port<Option<CreatureInfo>>,
104+ to_rendezvous: SharedChan <CreatureInfo>,
105+ to_rendezvous_log: SharedChan <~str>
104106) {
105107 let mut color = color;
106108 let mut creatures_met = 0;
107109 let mut evil_clones_met = 0;
108110
109111 loop {
110112 // ask for a pairing
111- oldcomm:: send(to_rendezvous, CreatureInfo {name: name, color: color});
112- let resp = oldcomm:: recv(from_rendezvous );
113+ to_rendezvous. send(CreatureInfo {name: name, color: color});
114+ let resp = from_rendezvous. recv();
113115
114116 // log and change, or print and quit
115117 match resp {
@@ -126,7 +128,7 @@ fn creature(
126128 // log creatures met and evil clones of self
127129 let report = fmt!(" %u", creatures_met) + ~" " +
128130 show_number(evil_clones_met);
129- oldcomm:: send(to_rendezvous_log, report);
131+ to_rendezvous_log. send(report);
130132 break;
131133 }
132134 }
@@ -135,61 +137,54 @@ fn creature(
135137
136138fn rendezvous(nn: uint, set: ~[color]) {
137139
138- pub fn spawn_listener<A: Owned>(+f: fn~(oldcomm::Port<A>)) -> oldcomm::Chan<A> {
139- let setup_po = oldcomm::Port();
140- let setup_ch = oldcomm::Chan(&setup_po);
141- do task::spawn |move f| {
142- let po = oldcomm::Port();
143- let ch = oldcomm::Chan(&po);
144- oldcomm::send(setup_ch, ch);
145- f(move po);
146- }
147- oldcomm::recv(setup_po)
148- }
149-
150140 // these ports will allow us to hear from the creatures
151- let from_creatures: oldcomm::Port<CreatureInfo> = oldcomm::Port();
152- let from_creatures_log: oldcomm::Port<~str> = oldcomm::Port();
141+ let (from_creatures, to_rendezvous) = stream::<CreatureInfo>();
142+ let to_rendezvous = SharedChan(to_rendezvous);
143+ let (from_creatures_log, to_rendezvous_log) = stream::<~str>();
144+ let to_rendezvous_log = SharedChan(to_rendezvous_log);
153145
154146 // these channels will be passed to the creatures so they can talk to us
155- let to_rendezvous = oldcomm::Chan(&from_creatures);
156- let to_rendezvous_log = oldcomm::Chan(&from_creatures_log);
157147
158148 // these channels will allow us to talk to each creature by 'name'/index
159- let to_creature: ~[oldcomm:: Chan<Option<CreatureInfo>>] =
149+ let to_creature: ~[Chan<Option<CreatureInfo>>] =
160150 vec::mapi(set, |ii, col| {
161151 // create each creature as a listener with a port, and
162152 // give us a channel to talk to each
163153 let ii = ii;
164154 let col = *col;
165- do spawn_listener |from_rendezvous, move ii, move col| {
166- creature(ii, col, from_rendezvous, to_rendezvous,
167- to_rendezvous_log);
155+ let to_rendezvous = to_rendezvous.clone();
156+ let to_rendezvous_log = to_rendezvous_log.clone();
157+ let (from_rendezvous, to_creature) = stream();
158+ let from_rendezvous = Cell(from_rendezvous);
159+ do task::spawn |move ii, move col| {
160+ creature(ii, col, from_rendezvous.take(), to_rendezvous.clone(),
161+ to_rendezvous_log.clone());
168162 }
163+ to_creature
169164 });
170165
171166 let mut creatures_met = 0;
172167
173168 // set up meetings...
174169 for nn.times {
175- let fst_creature: CreatureInfo = oldcomm:: recv(from_creatures );
176- let snd_creature: CreatureInfo = oldcomm:: recv(from_creatures );
170+ let fst_creature: CreatureInfo = from_creatures. recv();
171+ let snd_creature: CreatureInfo = from_creatures. recv();
177172
178173 creatures_met += 2;
179174
180- oldcomm::send( to_creature[fst_creature.name], Some(snd_creature));
181- oldcomm::send( to_creature[snd_creature.name], Some(fst_creature));
175+ to_creature[fst_creature.name].send( Some(snd_creature));
176+ to_creature[snd_creature.name].send( Some(fst_creature));
182177 }
183178
184179 // tell each creature to stop
185180 for vec::eachi(to_creature) |_ii, to_one| {
186- oldcomm:: send(*to_one, None);
181+ to_one. send(None);
187182 }
188183
189184 // save each creature's meeting stats
190185 let mut report = ~[];
191186 for vec::each(to_creature) |_to_one| {
192- report.push(oldcomm:: recv(from_creatures_log ));
187+ report.push(from_creatures_log. recv());
193188 }
194189
195190 // print each color in the set
0 commit comments