Skip to content

Commit 7858065

Browse files
committed
std: Rename Chan/Port types and constructor
* Chan<T> => Sender<T> * Port<T> => Receiver<T> * Chan::new() => channel() * constructor returns (Sender, Receiver) instead of (Receiver, Sender) * local variables named `port` renamed to `rx` * local variables named `chan` renamed to `tx` Closes #11765
1 parent e86e1d8 commit 7858065

File tree

117 files changed

+1736
-1891
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

117 files changed

+1736
-1891
lines changed

src/doc/guide-tasks.md

+64-67
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ concurrency at this writing:
4848
* [`std::task`] - All code relating to tasks and task scheduling,
4949
* [`std::comm`] - The message passing interface,
5050
* [`sync::DuplexStream`] - An extension of `pipes::stream` that allows both sending and receiving,
51-
* [`sync::SyncChan`] - An extension of `pipes::stream` that provides synchronous message sending,
52-
* [`sync::SyncPort`] - An extension of `pipes::stream` that acknowledges each message received,
51+
* [`sync::SyncSender`] - An extension of `pipes::stream` that provides synchronous message sending,
52+
* [`sync::SyncReceiver`] - An extension of `pipes::stream` that acknowledges each message received,
5353
* [`sync::rendezvous`] - Creates a stream whose channel, upon sending a message, blocks until the
5454
message is received.
5555
* [`sync::Arc`] - The Arc (atomically reference counted) type, for safely sharing immutable data,
@@ -70,8 +70,8 @@ concurrency at this writing:
7070
[`std::task`]: std/task/index.html
7171
[`std::comm`]: std/comm/index.html
7272
[`sync::DuplexStream`]: sync/struct.DuplexStream.html
73-
[`sync::SyncChan`]: sync/struct.SyncChan.html
74-
[`sync::SyncPort`]: sync/struct.SyncPort.html
73+
[`sync::SyncSender`]: sync/struct.SyncSender.html
74+
[`sync::SyncReceiver`]: sync/struct.SyncReceiver.html
7575
[`sync::rendezvous`]: sync/fn.rendezvous.html
7676
[`sync::Arc`]: sync/struct.Arc.html
7777
[`sync::RWArc`]: sync/struct.RWArc.html
@@ -141,118 +141,115 @@ receiving messages. Pipes are low-level communication building-blocks and so
141141
come in a variety of forms, each one appropriate for a different use case. In
142142
what follows, we cover the most commonly used varieties.
143143

144-
The simplest way to create a pipe is to use `Chan::new`
145-
function to create a `(Port, Chan)` pair. In Rust parlance, a *channel*
146-
is a sending endpoint of a pipe, and a *port* is the receiving
144+
The simplest way to create a pipe is to use the `channel`
145+
function to create a `(Sender, Receiver)` pair. In Rust parlance, a *sender*
146+
is a sending endpoint of a pipe, and a *receiver* is the receiving
147147
endpoint. Consider the following example of calculating two results
148148
concurrently:
149149

150150
~~~~
151151
# use std::task::spawn;
152152
153-
let (port, chan): (Port<int>, Chan<int>) = Chan::new();
153+
let (tx, rx): (Sender<int>, Receiver<int>) = channel();
154154
155155
spawn(proc() {
156156
let result = some_expensive_computation();
157-
chan.send(result);
157+
tx.send(result);
158158
});
159159
160160
some_other_expensive_computation();
161-
let result = port.recv();
161+
let result = rx.recv();
162162
# fn some_expensive_computation() -> int { 42 }
163163
# fn some_other_expensive_computation() {}
164164
~~~~
165165

166166
Let's examine this example in detail. First, the `let` statement creates a
167167
stream for sending and receiving integers (the left-hand side of the `let`,
168-
`(chan, port)`, is an example of a *destructuring let*: the pattern separates
168+
`(tx, rx)`, is an example of a *destructuring let*: the pattern separates
169169
a tuple into its component parts).
170170

171171
~~~~
172-
let (port, chan): (Port<int>, Chan<int>) = Chan::new();
172+
let (tx, rx): (Sender<int>, Receiver<int>) = channel();
173173
~~~~
174174

175-
The child task will use the channel to send data to the parent task,
176-
which will wait to receive the data on the port. The next statement
175+
The child task will use the sender to send data to the parent task,
176+
which will wait to receive the data on the receiver. The next statement
177177
spawns the child task.
178178

179179
~~~~
180180
# use std::task::spawn;
181181
# fn some_expensive_computation() -> int { 42 }
182-
# let (port, chan) = Chan::new();
182+
# let (tx, rx) = channel();
183183
spawn(proc() {
184184
let result = some_expensive_computation();
185-
chan.send(result);
185+
tx.send(result);
186186
});
187187
~~~~
188188

189-
Notice that the creation of the task closure transfers `chan` to the child
190-
task implicitly: the closure captures `chan` in its environment. Both `Chan`
191-
and `Port` are sendable types and may be captured into tasks or otherwise
189+
Notice that the creation of the task closure transfers `tx` to the child
190+
task implicitly: the closure captures `tx` in its environment. Both `Sender`
191+
and `Receiver` are sendable types and may be captured into tasks or otherwise
192192
transferred between them. In the example, the child task runs an expensive
193193
computation, then sends the result over the captured channel.
194194

195195
Finally, the parent continues with some other expensive
196196
computation, then waits for the child's result to arrive on the
197-
port:
197+
receiver:
198198

199199
~~~~
200200
# fn some_other_expensive_computation() {}
201-
# let (port, chan) = Chan::<int>::new();
202-
# chan.send(0);
201+
# let (tx, rx) = channel::<int>();
202+
# tx.send(0);
203203
some_other_expensive_computation();
204-
let result = port.recv();
204+
let result = rx.recv();
205205
~~~~
206206

207-
The `Port` and `Chan` pair created by `Chan::new` enables efficient
207+
The `Sender` and `Receiver` pair created by `channel` enables efficient
208208
communication between a single sender and a single receiver, but multiple
209-
senders cannot use a single `Chan`, and multiple receivers cannot use a single
210-
`Port`. What if our example needed to compute multiple results across a number
211-
of tasks? The following program is ill-typed:
209+
senders cannot use a single `Sender` value, and multiple receivers cannot use a
210+
single `Receiver` value. What if our example needed to compute multiple
211+
results across a number of tasks? The following program is ill-typed:
212212

213213
~~~ {.ignore}
214-
# use std::task::{spawn};
215214
# fn some_expensive_computation() -> int { 42 }
216-
let (port, chan) = Chan::new();
215+
let (tx, rx) = channel();
217216
218217
spawn(proc() {
219-
chan.send(some_expensive_computation());
218+
tx.send(some_expensive_computation());
220219
});
221220
222-
// ERROR! The previous spawn statement already owns the channel,
221+
// ERROR! The previous spawn statement already owns the sender,
223222
// so the compiler will not allow it to be captured again
224223
spawn(proc() {
225-
chan.send(some_expensive_computation());
224+
tx.send(some_expensive_computation());
226225
});
227226
~~~
228227

229-
Instead we can clone the `chan`, which allows for multiple senders.
228+
Instead we can clone the `tx`, which allows for multiple senders.
230229

231230
~~~
232-
# use std::task::spawn;
233-
234-
let (port, chan) = Chan::new();
231+
let (tx, rx) = channel();
235232
236233
for init_val in range(0u, 3) {
237234
// Create a new channel handle to distribute to the child task
238-
let child_chan = chan.clone();
235+
let child_tx = tx.clone();
239236
spawn(proc() {
240-
child_chan.send(some_expensive_computation(init_val));
237+
child_tx.send(some_expensive_computation(init_val));
241238
});
242239
}
243240
244-
let result = port.recv() + port.recv() + port.recv();
241+
let result = rx.recv() + rx.recv() + rx.recv();
245242
# fn some_expensive_computation(_i: uint) -> int { 42 }
246243
~~~
247244

248-
Cloning a `Chan` produces a new handle to the same channel, allowing multiple
249-
tasks to send data to a single port. It also upgrades the channel internally in
245+
Cloning a `Sender` produces a new handle to the same channel, allowing multiple
246+
tasks to send data to a single receiver. It upgrades the channel internally in
250247
order to allow this functionality, which means that channels that are not
251248
cloned can avoid the overhead required to handle multiple senders. But this
252249
fact has no bearing on the channel's usage: the upgrade is transparent.
253250

254251
Note that the above cloning example is somewhat contrived since
255-
you could also simply use three `Chan` pairs, but it serves to
252+
you could also simply use three `Sender` pairs, but it serves to
256253
illustrate the point. For reference, written with multiple streams, it
257254
might look like the example below.
258255

@@ -261,16 +258,16 @@ might look like the example below.
261258
# use std::vec;
262259
263260
// Create a vector of ports, one for each child task
264-
let ports = vec::from_fn(3, |init_val| {
265-
let (port, chan) = Chan::new();
261+
let rxs = vec::from_fn(3, |init_val| {
262+
let (tx, rx) = channel();
266263
spawn(proc() {
267-
chan.send(some_expensive_computation(init_val));
264+
tx.send(some_expensive_computation(init_val));
268265
});
269-
port
266+
rx
270267
});
271268
272269
// Wait on each port, accumulating the results
273-
let result = ports.iter().fold(0, |accum, port| accum + port.recv() );
270+
let result = rxs.iter().fold(0, |accum, rx| accum + rx.recv() );
274271
# fn some_expensive_computation(_i: uint) -> int { 42 }
275272
~~~
276273

@@ -281,7 +278,7 @@ later.
281278
The basic example below illustrates this.
282279

283280
~~~
284-
# extern crate sync;
281+
extern crate sync;
285282
286283
# fn main() {
287284
# fn make_a_sandwich() {};
@@ -342,9 +339,10 @@ Here is a small example showing how to use Arcs. We wish to run concurrently sev
342339
a single large vector of floats. Each task needs the full vector to perform its duty.
343340

344341
~~~
345-
# extern crate sync;
346-
extern crate rand;
347-
# use std::vec;
342+
extern crate rand;
343+
extern crate sync;
344+
345+
use std::vec;
348346
use sync::Arc;
349347
350348
fn pnorm(nums: &~[f64], p: uint) -> f64 {
@@ -358,11 +356,11 @@ fn main() {
358356
let numbers_arc = Arc::new(numbers);
359357
360358
for num in range(1u, 10) {
361-
let (port, chan) = Chan::new();
362-
chan.send(numbers_arc.clone());
359+
let (tx, rx) = channel();
360+
tx.send(numbers_arc.clone());
363361
364362
spawn(proc() {
365-
let local_arc : Arc<~[f64]> = port.recv();
363+
let local_arc : Arc<~[f64]> = rx.recv();
366364
let task_numbers = local_arc.get();
367365
println!("{}-norm = {}", num, pnorm(task_numbers, num));
368366
});
@@ -395,8 +393,8 @@ and a clone of it is sent to each task
395393
# fn main() {
396394
# let numbers=vec::from_fn(1000000, |_| rand::random::<f64>());
397395
# let numbers_arc = Arc::new(numbers);
398-
# let (port, chan) = Chan::new();
399-
chan.send(numbers_arc.clone());
396+
# let (tx, rx) = channel();
397+
tx.send(numbers_arc.clone());
400398
# }
401399
~~~
402400

@@ -412,9 +410,9 @@ Each task recovers the underlying data by
412410
# fn main() {
413411
# let numbers=vec::from_fn(1000000, |_| rand::random::<f64>());
414412
# let numbers_arc=Arc::new(numbers);
415-
# let (port, chan) = Chan::new();
416-
# chan.send(numbers_arc.clone());
417-
# let local_arc : Arc<~[f64]> = port.recv();
413+
# let (tx, rx) = channel();
414+
# tx.send(numbers_arc.clone());
415+
# let local_arc : Arc<~[f64]> = rx.recv();
418416
let task_numbers = local_arc.get();
419417
# }
420418
~~~
@@ -486,19 +484,18 @@ proceed).
486484

487485
A very common thing to do is to spawn a child task where the parent
488486
and child both need to exchange messages with each other. The
489-
function `sync::comm::DuplexStream()` supports this pattern. We'll
487+
function `sync::comm::duplex` supports this pattern. We'll
490488
look briefly at how to use it.
491489

492-
To see how `DuplexStream()` works, we will create a child task
490+
To see how `duplex` works, we will create a child task
493491
that repeatedly receives a `uint` message, converts it to a string, and sends
494492
the string in response. The child terminates when it receives `0`.
495493
Here is the function that implements the child task:
496494

497495
~~~
498-
# extern crate sync;
496+
extern crate sync;
499497
# fn main() {
500-
# use sync::DuplexStream;
501-
fn stringifier(channel: &DuplexStream<~str, uint>) {
498+
fn stringifier(channel: &sync::DuplexStream<~str, uint>) {
502499
let mut value: uint;
503500
loop {
504501
value = channel.recv();
@@ -520,10 +517,10 @@ response itself is simply the stringified version of the received value,
520517
Here is the code for the parent task:
521518
522519
~~~
523-
# extern crate sync;
520+
extern crate sync;
524521
# use std::task::spawn;
525522
# use sync::DuplexStream;
526-
# fn stringifier(channel: &DuplexStream<~str, uint>) {
523+
# fn stringifier(channel: &sync::DuplexStream<~str, uint>) {
527524
# let mut value: uint;
528525
# loop {
529526
# value = channel.recv();
@@ -533,7 +530,7 @@ Here is the code for the parent task:
533530
# }
534531
# fn main() {
535532

536-
let (from_child, to_child) = DuplexStream::new();
533+
let (from_child, to_child) = sync::duplex();
537534

538535
spawn(proc() {
539536
stringifier(&to_child);

src/etc/vim/syntax/rust.vim

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ syn keyword rustTrait MutableVector MutableTotalOrdVector
9696
syn keyword rustTrait Vector VectorVector CloneableVector ImmutableVector
9797

9898
"syn keyword rustFunction stream
99-
syn keyword rustTrait Port Chan
99+
syn keyword rustTrait Sender Receiver
100100
"syn keyword rustFunction spawn
101101

102102
syn keyword rustSelf self

src/libextra/workcache.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ pub struct Exec {
237237

238238
enum Work<'a, T> {
239239
WorkValue(T),
240-
WorkFromTask(&'a Prep<'a>, Port<(Exec, T)>),
240+
WorkFromTask(&'a Prep<'a>, Receiver<(Exec, T)>),
241241
}
242242

243243
fn json_encode<'a, T:Encodable<json::Encoder<'a>>>(t: &T) -> ~str {
@@ -411,7 +411,7 @@ impl<'a> Prep<'a> {
411411

412412
_ => {
413413
debug!("Cache miss!");
414-
let (port, chan) = Chan::new();
414+
let (tx, rx) = channel();
415415
let blk = bo.take_unwrap();
416416

417417
// FIXME: What happens if the task fails?
@@ -421,9 +421,9 @@ impl<'a> Prep<'a> {
421421
discovered_outputs: WorkMap::new(),
422422
};
423423
let v = blk(&mut exe);
424-
chan.send((exe, v));
424+
tx.send((exe, v));
425425
});
426-
Work::from_task(self, port)
426+
Work::from_task(self, rx)
427427
}
428428
}
429429
}
@@ -437,7 +437,7 @@ impl<'a, T:Send +
437437
pub fn from_value(elt: T) -> Work<'a, T> {
438438
WorkValue(elt)
439439
}
440-
pub fn from_task(prep: &'a Prep<'a>, port: Port<(Exec, T)>)
440+
pub fn from_task(prep: &'a Prep<'a>, port: Receiver<(Exec, T)>)
441441
-> Work<'a, T> {
442442
WorkFromTask(prep, port)
443443
}

src/libgreen/basic.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,11 @@ mod test {
255255
#[test]
256256
fn some_channels() {
257257
run(proc() {
258-
let (p, c) = Chan::new();
258+
let (tx, rx) = channel();
259259
spawn(proc() {
260-
c.send(());
260+
tx.send(());
261261
});
262-
p.recv();
262+
rx.recv();
263263
});
264264
}
265265

@@ -272,11 +272,11 @@ mod test {
272272

273273
for _ in range(0, 20) {
274274
pool.spawn(TaskOpts::new(), proc() {
275-
let (p, c) = Chan::new();
275+
let (tx, rx) = channel();
276276
spawn(proc() {
277-
c.send(());
277+
tx.send(());
278278
});
279-
p.recv();
279+
rx.recv();
280280
});
281281
}
282282

0 commit comments

Comments
 (0)