@@ -121,17 +121,16 @@ receiving messages. Pipes are low-level communication building-blocks and so
121121come in a variety of forms, each one appropriate for a different use case. In
122122what follows, we cover the most commonly used varieties.
123123
124- The simplest way to create a pipe is to use the ` comm::stream `
124+ The simplest way to create a pipe is to use ` Chan::new `
125125function to create a ` (Port, Chan) ` pair. In Rust parlance, a * channel*
126126is a sending endpoint of a pipe, and a * port* is the receiving
127127endpoint. Consider the following example of calculating two results
128128concurrently:
129129
130130~~~~
131131# use std::task::spawn;
132- # use std::comm::{stream, Port, Chan};
133132
134- let (port, chan): (Port<int>, Chan<int>) = stream ();
133+ let (port, chan): (Port<int>, Chan<int>) = Chan::new ();
135134
136135do spawn || {
137136 let result = some_expensive_computation();
@@ -150,8 +149,7 @@ stream for sending and receiving integers (the left-hand side of the `let`,
150149a tuple into its component parts).
151150
152151~~~~
153- # use std::comm::{stream, Chan, Port};
154- let (port, chan): (Port<int>, Chan<int>) = stream();
152+ let (port, chan): (Port<int>, Chan<int>) = Chan::new();
155153~~~~
156154
157155The child task will use the channel to send data to the parent task,
@@ -160,9 +158,8 @@ spawns the child task.
160158
161159~~~~
162160# use std::task::spawn;
163- # use std::comm::stream;
164161# fn some_expensive_computation() -> int { 42 }
165- # let (port, chan) = stream ();
162+ # let (port, chan) = Chan::new ();
166163do spawn || {
167164 let result = some_expensive_computation();
168165 chan.send(result);
@@ -180,25 +177,23 @@ computation, then waits for the child's result to arrive on the
180177port:
181178
182179~~~~
183- # use std::comm::{stream};
184180# fn some_other_expensive_computation() {}
185- # let (port, chan) = stream ::<int>();
181+ # let (port, chan) = Chan ::<int>::new ();
186182# chan.send(0);
187183some_other_expensive_computation();
188184let result = port.recv();
189185~~~~
190186
191- The ` Port ` and ` Chan ` pair created by ` stream ` enables efficient communication
192- between a single sender and a single receiver, but multiple senders cannot use
193- a single ` Chan ` , and multiple receivers cannot use a single ` Port ` . What if our
194- example needed to compute multiple results across a number of tasks? The
195- following program is ill-typed:
187+ The ` Port ` and ` Chan ` pair created by ` Chan::new ` enables efficient
188+ communication between a single sender and a single receiver, but multiple
189+ senders cannot use a single ` Chan ` , and multiple receivers cannot use a single
190+ ` Port ` . What if our example needed to compute multiple results across a number
191+ of tasks? The following program is ill-typed:
196192
197193~~~ {.xfail-test}
198194# use std::task::{spawn};
199- # use std::comm::{stream, Port, Chan};
200195# fn some_expensive_computation() -> int { 42 }
201- let (port, chan) = stream ();
196+ let (port, chan) = Chan::new ();
202197
203198do spawn {
204199 chan.send(some_expensive_computation());
@@ -216,10 +211,8 @@ Instead we can use a `SharedChan`, a type that allows a single
216211
217212~~~
218213# use std::task::spawn;
219- # use std::comm::{stream, SharedChan};
220214
221- let (port, chan) = stream();
222- let chan = SharedChan::new(chan);
215+ let (port, chan) = SharedChan::new();
223216
224217for init_val in range(0u, 3) {
225218 // Create a new channel handle to distribute to the child task
@@ -238,23 +231,22 @@ Here we transfer ownership of the channel into a new `SharedChan` value. Like
238231as an * affine* or * linear* type). Unlike with ` Chan ` , though, the programmer
239232may duplicate a ` SharedChan ` , with the ` clone() ` method. A cloned
240233` SharedChan ` produces a new handle to the same channel, allowing multiple
241- tasks to send data to a single port. Between ` spawn ` , ` stream ` and
234+ tasks to send data to a single port. Between ` spawn ` , ` Chan ` and
242235` SharedChan ` , we have enough tools to implement many useful concurrency
243236patterns.
244237
245238Note that the above ` SharedChan ` example is somewhat contrived since
246- you could also simply use three ` stream ` pairs, but it serves to
239+ you could also simply use three ` Chan ` pairs, but it serves to
247240illustrate the point. For reference, written with multiple streams, it
248241might look like the example below.
249242
250243~~~
251244# use std::task::spawn;
252- # use std::comm::stream;
253245# use std::vec;
254246
255247// Create a vector of ports, one for each child task
256248let ports = vec::from_fn(3, |init_val| {
257- let (port, chan) = stream ();
249+ let (port, chan) = Chan::new ();
258250 do spawn {
259251 chan.send(some_expensive_computation(init_val));
260252 }
@@ -341,7 +333,7 @@ fn main() {
341333 let numbers_arc = Arc::new(numbers);
342334
343335 for num in range(1u, 10) {
344- let (port, chan) = stream ();
336+ let (port, chan) = Chan::new ();
345337 chan.send(numbers_arc.clone());
346338
347339 do spawn {
@@ -370,7 +362,7 @@ and a clone of it is sent to each task
370362# use std::rand;
371363# let numbers=vec::from_fn(1000000, |_| rand::random::<f64>());
372364# let numbers_arc = Arc::new(numbers);
373- # let (port, chan) = stream ();
365+ # let (port, chan) = Chan::new ();
374366chan.send(numbers_arc.clone());
375367~~~
376368copying only the wrapper and not its contents.
@@ -382,7 +374,7 @@ Each task recovers the underlying data by
382374# use std::rand;
383375# let numbers=vec::from_fn(1000000, |_| rand::random::<f64>());
384376# let numbers_arc=Arc::new(numbers);
385- # let (port, chan) = stream ();
377+ # let (port, chan) = Chan::new ();
386378# chan.send(numbers_arc.clone());
387379# let local_arc : Arc<~[f64]> = port.recv();
388380let task_numbers = local_arc.get();
@@ -499,7 +491,7 @@ Here is the code for the parent task:
499491# }
500492# fn main() {
501493
502- let (from_child, to_child) = DuplexStream();
494+ let (from_child, to_child) = DuplexStream::new ();
503495
504496do spawn {
505497 stringifier(&to_child);
0 commit comments