Skip to content

Commit decd390

Browse files
committed
core::comm: Modernize constructors to use new
1 parent bc60d84 commit decd390

32 files changed

+79
-67
lines changed

doc/tutorial-tasks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ Instead we can use a `SharedChan`, a type that allows a single
236236
use core::comm::{stream, SharedChan};
237237
238238
let (port, chan) = stream();
239-
let chan = SharedChan(chan);
239+
let chan = SharedChan::new(chan);
240240
241241
for uint::range(0, 3) |init_val| {
242242
// Create a new channel handle to distribute to the child task

src/compiletest/procsrv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ pub fn run(lib_path: ~str,
7373
7474
7575
writeclose(pipe_in.out, input);
76-
let p = comm::PortSet();
76+
let p = comm::PortSet::new();
7777
let ch = p.chan();
7878
do task::spawn_sched(task::SingleThreaded) || {
7979
let errput = readclose(pipe_err.in);

src/libcore/comm.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use option::{Option, Some, None};
1919
use uint;
2020
use unstable;
2121
use vec;
22+
use unstable::Exclusive;
2223

2324
use pipes::{recv, try_recv, wait_many, peek, PacketHeader};
2425

@@ -218,13 +219,14 @@ pub struct PortSet<T> {
218219
mut ports: ~[Port<T>],
219220
}
220221
221-
pub fn PortSet<T: Owned>() -> PortSet<T>{
222-
PortSet {
223-
ports: ~[]
222+
pub impl<T: Owned> PortSet<T> {
223+
224+
fn new() -> PortSet<T> {
225+
PortSet {
226+
ports: ~[]
227+
}
224228
}
225-
}
226229
227-
pub impl<T: Owned> PortSet<T> {
228230
fn add(&self, port: Port<T>) {
229231
self.ports.push(port)
230232
}
@@ -279,12 +281,21 @@ impl<T: Owned> Peekable<T> for PortSet<T> {
279281
}
280282
281283
/// A channel that can be shared between many senders.
282-
pub type SharedChan<T> = unstable::Exclusive<Chan<T>>;
284+
pub struct SharedChan<T> {
285+
ch: Exclusive<Chan<T>>
286+
}
287+
288+
impl<T: Owned> SharedChan<T> {
289+
/// Converts a `chan` into a `shared_chan`.
290+
pub fn new(c: Chan<T>) -> SharedChan<T> {
291+
SharedChan { ch: unstable::exclusive(c) }
292+
}
293+
}
283294
284295
impl<T: Owned> GenericChan<T> for SharedChan<T> {
285296
fn send(&self, x: T) {
286297
let mut xx = Some(x);
287-
do self.with_imm |chan| {
298+
do self.ch.with_imm |chan| {
288299
let mut x = None;
289300
x <-> xx;
290301
chan.send(x.unwrap())
@@ -295,17 +306,18 @@ impl<T: Owned> GenericChan<T> for SharedChan<T> {
295306
impl<T: Owned> GenericSmartChan<T> for SharedChan<T> {
296307
fn try_send(&self, x: T) -> bool {
297308
let mut xx = Some(x);
298-
do self.with_imm |chan| {
309+
do self.ch.with_imm |chan| {
299310
let mut x = None;
300311
x <-> xx;
301312
chan.try_send(x.unwrap())
302313
}
303314
}
304315
}
305316
306-
/// Converts a `chan` into a `shared_chan`.
307-
pub fn SharedChan<T:Owned>(c: Chan<T>) -> SharedChan<T> {
308-
unstable::exclusive(c)
317+
impl<T: Owned> ::clone::Clone for SharedChan<T> {
318+
fn clone(&self) -> SharedChan<T> {
319+
SharedChan { ch: self.ch.clone() }
320+
}
309321
}
310322
311323
/*proto! oneshot (

src/libcore/run.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ pub fn program_output(prog: &str, args: &[~str]) -> ProgramOutput {
405405
// or the other. FIXME (#2625): Surely there's a much more
406406
// clever way to do this.
407407
let (p, ch) = stream();
408-
let ch = SharedChan(ch);
408+
let ch = SharedChan::new(ch);
409409
let ch_clone = ch.clone();
410410
do task::spawn_sched(task::SingleThreaded) {
411411
let errput = readclose(pipe_err.in);

src/libcore/task/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ fn test_cant_dup_task_builder() {
657657
#[test] #[ignore(cfg(windows))]
658658
fn test_spawn_unlinked_unsup_no_fail_down() { // grandchild sends on a port
659659
let (po, ch) = stream();
660-
let ch = SharedChan(ch);
660+
let ch = SharedChan::new(ch);
661661
do spawn_unlinked {
662662
let ch = ch.clone();
663663
do spawn_unlinked {
@@ -881,7 +881,7 @@ fn test_spawn_sched_no_threads() {
881881
#[test]
882882
fn test_spawn_sched() {
883883
let (po, ch) = stream::<()>();
884-
let ch = SharedChan(ch);
884+
let ch = SharedChan::new(ch);
885885

886886
fn f(i: int, ch: SharedChan<()>) {
887887
let parent_sched_id = unsafe { rt::rust_get_sched_id() };

src/libcore/unstable/weak_task.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn create_global_service() -> ~WeakTaskService {
6969
debug!("creating global weak task service");
7070
let (port, chan) = stream::<ServiceMsg>();
7171
let port = Cell(port);
72-
let chan = SharedChan(chan);
72+
let chan = SharedChan::new(chan);
7373
let chan_clone = chan.clone();
7474

7575
do task().unlinked().spawn {

src/librustc/rustc.rc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ bug and need to present an error.
307307
pub fn monitor(+f: ~fn(diagnostic::Emitter)) {
308308
use core::comm::*;
309309
let (p, ch) = stream();
310-
let ch = SharedChan(ch);
310+
let ch = SharedChan::new(ch);
311311
let ch_capture = ch.clone();
312312
match do task::try || {
313313
let ch = ch_capture.clone();

src/librustdoc/astsrv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ fn run<T>(owner: SrvOwner<T>, source: ~str, parse: Parser) -> T {
6969
}
7070

7171
let srv_ = Srv {
72-
ch: SharedChan(ch)
72+
ch: SharedChan::new(ch)
7373
};
7474

7575
let res = owner(srv_.clone());

src/librustdoc/markdown_writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ fn write_file(path: &Path, s: ~str) {
232232
pub fn future_writer_factory(
233233
) -> (WriterFactory, Port<(doc::Page, ~str)>) {
234234
let (markdown_po, markdown_ch) = stream();
235-
let markdown_ch = SharedChan(markdown_ch);
235+
let markdown_ch = SharedChan::new(markdown_ch);
236236
let writer_factory: WriterFactory = |page| {
237237
let (writer_po, writer_ch) = comm::stream();
238238
let markdown_ch = markdown_ch.clone();

src/librustdoc/page_pass.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub fn run(
5050

5151
let (result_port, result_chan) = stream();
5252
let (page_port, page_chan) = stream();
53-
let page_chan = SharedChan(page_chan);
53+
let page_chan = SharedChan::new(page_chan);
5454
do task::spawn {
5555
result_chan.send(make_doc_from_pages(&page_port));
5656
};

src/libstd/arc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ mod tests {
499499
let (p, c) = comm::stream();
500500

501501
do task::spawn() || {
502-
let p = comm::PortSet();
502+
let p = comm::PortSet::new();
503503
c.send(p.chan());
504504

505505
let arc_v = p.recv();

src/libstd/net_ip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ enum IpGetAddrErr {
113113
pub fn get_addr(node: &str, iotask: &iotask)
114114
-> result::Result<~[IpAddr], IpGetAddrErr> {
115115
let (output_po, output_ch) = stream();
116-
let mut output_ch = Some(SharedChan(output_ch));
116+
let mut output_ch = Some(SharedChan::new(output_ch));
117117
do str::as_buf(node) |node_ptr, len| {
118118
let output_ch = output_ch.swap_unwrap();
119119
debug!("slice len %?", len);

src/libstd/net_tcp.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,16 @@ pub fn connect(input_ip: ip::IpAddr, port: uint,
150150
-> result::Result<TcpSocket, TcpConnectErrData> {
151151
unsafe {
152152
let (result_po, result_ch) = stream::<ConnAttempt>();
153-
let result_ch = SharedChan(result_ch);
153+
let result_ch = SharedChan::new(result_ch);
154154
let (closed_signal_po, closed_signal_ch) = stream::<()>();
155-
let closed_signal_ch = SharedChan(closed_signal_ch);
155+
let closed_signal_ch = SharedChan::new(closed_signal_ch);
156156
let conn_data = ConnectReqData {
157157
result_ch: result_ch,
158158
closed_signal_ch: closed_signal_ch
159159
};
160160
let conn_data_ptr = ptr::addr_of(&conn_data);
161161
let (reader_po, reader_ch) = stream::<Result<~[u8], TcpErrData>>();
162-
let reader_ch = SharedChan(reader_ch);
162+
let reader_ch = SharedChan::new(reader_ch);
163163
let stream_handle_ptr = malloc_uv_tcp_t();
164164
*(stream_handle_ptr as *mut uv::ll::uv_tcp_t) = uv::ll::tcp_t();
165165
let socket_data = @TcpSocketData {
@@ -517,7 +517,7 @@ pub fn accept(new_conn: TcpNewConnection)
517517
server_handle_ptr) as *TcpListenFcData;
518518
let (reader_po, reader_ch) = stream::<
519519
Result<~[u8], TcpErrData>>();
520-
let reader_ch = SharedChan(reader_ch);
520+
let reader_ch = SharedChan::new(reader_ch);
521521
let iotask = &(*server_data_ptr).iotask;
522522
let stream_handle_ptr = malloc_uv_tcp_t();
523523
*(stream_handle_ptr as *mut uv::ll::uv_tcp_t) =
@@ -537,7 +537,7 @@ pub fn accept(new_conn: TcpNewConnection)
537537
(*client_socket_data_ptr).stream_handle_ptr;
538538

539539
let (result_po, result_ch) = stream::<Option<TcpErrData>>();
540-
let result_ch = SharedChan(result_ch);
540+
let result_ch = SharedChan::new(result_ch);
541541

542542
// UNSAFE LIBUV INTERACTION BEGIN
543543
// .. normally this happens within the context of
@@ -646,9 +646,9 @@ fn listen_common(host_ip: ip::IpAddr,
646646
on_connect_cb: ~fn(*uv::ll::uv_tcp_t))
647647
-> result::Result<(), TcpListenErrData> {
648648
let (stream_closed_po, stream_closed_ch) = stream::<()>();
649-
let stream_closed_ch = SharedChan(stream_closed_ch);
649+
let stream_closed_ch = SharedChan::new(stream_closed_ch);
650650
let (kill_po, kill_ch) = stream::<Option<TcpErrData>>();
651-
let kill_ch = SharedChan(kill_ch);
651+
let kill_ch = SharedChan::new(kill_ch);
652652
let server_stream = uv::ll::tcp_t();
653653
let server_stream_ptr = ptr::addr_of(&server_stream);
654654
let server_data: TcpListenFcData = TcpListenFcData {
@@ -997,7 +997,7 @@ impl io::Writer for TcpSocketBuf {
997997
fn tear_down_socket_data(socket_data: @TcpSocketData) {
998998
unsafe {
999999
let (closed_po, closed_ch) = stream::<()>();
1000-
let closed_ch = SharedChan(closed_ch);
1000+
let closed_ch = SharedChan::new(closed_ch);
10011001
let close_data = TcpSocketCloseData {
10021002
closed_ch: closed_ch
10031003
};
@@ -1147,7 +1147,7 @@ fn write_common_impl(socket_data_ptr: *TcpSocketData,
11471147
vec::len(raw_write_data)) ];
11481148
let write_buf_vec_ptr = ptr::addr_of(&write_buf_vec);
11491149
let (result_po, result_ch) = stream::<TcpWriteResult>();
1150-
let result_ch = SharedChan(result_ch);
1150+
let result_ch = SharedChan::new(result_ch);
11511151
let write_data = WriteReqData {
11521152
result_ch: result_ch
11531153
};
@@ -1554,7 +1554,7 @@ mod test {
15541554
let (server_result_po, server_result_ch) = stream::<~str>();
15551555

15561556
let (cont_po, cont_ch) = stream::<()>();
1557-
let cont_ch = SharedChan(cont_ch);
1557+
let cont_ch = SharedChan::new(cont_ch);
15581558
// server
15591559
let hl_loop_clone = hl_loop.clone();
15601560
do task::spawn_sched(task::ManualThreads(1u)) {
@@ -1592,7 +1592,7 @@ mod test {
15921592
let expected_resp = ~"pong";
15931593

15941594
let (cont_po, cont_ch) = stream::<()>();
1595-
let cont_ch = SharedChan(cont_ch);
1595+
let cont_ch = SharedChan::new(cont_ch);
15961596
// server
15971597
let hl_loop_clone = hl_loop.clone();
15981598
do task::spawn_sched(task::ManualThreads(1u)) {
@@ -1652,7 +1652,7 @@ mod test {
16521652
let expected_resp = ~"pong";
16531653
16541654
let (cont_po, cont_ch) = stream::<()>();
1655-
let cont_ch = SharedChan(cont_ch);
1655+
let cont_ch = SharedChan::new(cont_ch);
16561656
// server
16571657
let hl_loop_clone = hl_loop.clone();
16581658
do task::spawn_sched(task::ManualThreads(1u)) {
@@ -1717,7 +1717,7 @@ mod test {
17171717
let (server_result_po, server_result_ch) = stream::<~str>();
17181718
17191719
let (cont_po, cont_ch) = stream::<()>();
1720-
let cont_ch = SharedChan(cont_ch);
1720+
let cont_ch = SharedChan::new(cont_ch);
17211721
// server
17221722
let iotask_clone = iotask.clone();
17231723
do task::spawn_sched(task::ManualThreads(1u)) {
@@ -1764,7 +1764,7 @@ mod test {
17641764
let expected_resp = ~"A string\nwith multiple lines\n";
17651765

17661766
let (cont_po, cont_ch) = stream::<()>();
1767-
let cont_ch = SharedChan(cont_ch);
1767+
let cont_ch = SharedChan::new(cont_ch);
17681768
// server
17691769
let hl_loop_clone = hl_loop.clone();
17701770
do task::spawn_sched(task::ManualThreads(1u)) {
@@ -1813,7 +1813,7 @@ mod test {
18131813
cont_ch: SharedChan<()>,
18141814
iotask: &IoTask) -> ~str {
18151815
let (server_po, server_ch) = stream::<~str>();
1816-
let server_ch = SharedChan(server_ch);
1816+
let server_ch = SharedChan::new(server_ch);
18171817
let server_ip_addr = ip::v4::parse_addr(server_ip);
18181818
let listen_result = listen(server_ip_addr, server_port, 128,
18191819
iotask,

src/libstd/test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ fn run_tests(opts: &TestOpts,
446446
let mut pending = 0;
447447
448448
let (p, ch) = stream();
449-
let ch = SharedChan(ch);
449+
let ch = SharedChan::new(ch);
450450
451451
while pending > 0 || !remaining.is_empty() {
452452
while pending < concurrency && !remaining.is_empty() {
@@ -797,7 +797,7 @@ mod tests {
797797
testfn: DynTestFn(|| f()),
798798
};
799799
let (p, ch) = stream();
800-
let ch = SharedChan(ch);
800+
let ch = SharedChan::new(ch);
801801
run_test(false, desc, ch);
802802
let (_, res) = p.recv();
803803
assert!(res != TrOk);
@@ -815,7 +815,7 @@ mod tests {
815815
testfn: DynTestFn(|| f()),
816816
};
817817
let (p, ch) = stream();
818-
let ch = SharedChan(ch);
818+
let ch = SharedChan::new(ch);
819819
run_test(false, desc, ch);
820820
let (_, res) = p.recv();
821821
assert!(res == TrIgnored);
@@ -834,7 +834,7 @@ mod tests {
834834
testfn: DynTestFn(|| f()),
835835
};
836836
let (p, ch) = stream();
837-
let ch = SharedChan(ch);
837+
let ch = SharedChan::new(ch);
838838
run_test(false, desc, ch);
839839
let (_, res) = p.recv();
840840
assert!(res == TrOk);
@@ -852,7 +852,7 @@ mod tests {
852852
testfn: DynTestFn(|| f()),
853853
};
854854
let (p, ch) = stream();
855-
let ch = SharedChan(ch);
855+
let ch = SharedChan::new(ch);
856856
run_test(false, desc, ch);
857857
let (_, res) = p.recv();
858858
assert!(res == TrFailed);

src/libstd/timer.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub fn delayed_send<T:Owned>(iotask: &IoTask,
4343
ch: &Chan<T>,
4444
val: T) {
4545
let (timer_done_po, timer_done_ch) = stream::<()>();
46-
let timer_done_ch = SharedChan(timer_done_ch);
46+
let timer_done_ch = SharedChan::new(timer_done_ch);
4747
let timer = uv::ll::timer_t();
4848
let timer_ptr = ptr::addr_of(&timer);
4949
do iotask::interact(iotask) |loop_ptr| {
@@ -199,7 +199,7 @@ mod test {
199199
#[test]
200200
fn test_gl_timer_sleep_stress2() {
201201
let (po, ch) = stream();
202-
let ch = SharedChan(ch);
202+
let ch = SharedChan::new(ch);
203203
let hl_loop = &uv::global_loop::get();
204204

205205
let repeat = 20u;

src/libstd/uv_global_loop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ mod test {
211211
#[ignore]
212212
fn test_stress_gl_uv_global_loop_high_level_global_timer() {
213213
let (exit_po, exit_ch) = stream::<()>();
214-
let exit_ch = SharedChan(exit_ch);
214+
let exit_ch = SharedChan::new(exit_ch);
215215
let cycles = 5000u;
216216
for iter::repeat(cycles) {
217217
let exit_ch_clone = exit_ch.clone();

0 commit comments

Comments
 (0)