Skip to content

Commit 5c3d398

Browse files
committed
Mostly rote conversion of proc() to move|| (and occasionally Thunk::new)
1 parent 394f684 commit 5c3d398

File tree

207 files changed

+692
-693
lines changed

Some content is hidden

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

207 files changed

+692
-693
lines changed

src/compiletest/compiletest.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
#![crate_type = "bin"]
12-
#![feature(phase, slicing_syntax, globs)]
12+
#![feature(phase, slicing_syntax, globs, unboxed_closures)]
1313

1414
#![deny(warnings)]
1515

@@ -23,6 +23,7 @@ use std::os;
2323
use std::io;
2424
use std::io::fs;
2525
use std::str::FromStr;
26+
use std::thunk::{Thunk};
2627
use getopts::{optopt, optflag, reqopt};
2728
use common::Config;
2829
use common::{Pretty, DebugInfoGdb, DebugInfoLldb, Codegen};
@@ -369,16 +370,16 @@ pub fn make_test_closure(config: &Config, testfile: &Path) -> test::TestFn {
369370
let config = (*config).clone();
370371
// FIXME (#9639): This needs to handle non-utf8 paths
371372
let testfile = testfile.as_str().unwrap().to_string();
372-
test::DynTestFn(proc() {
373+
test::DynTestFn(Thunk::new(move || {
373374
runtest::run(config, testfile)
374-
})
375+
}))
375376
}
376377

377378
pub fn make_metrics_test_closure(config: &Config, testfile: &Path) -> test::TestFn {
378379
let config = (*config).clone();
379380
// FIXME (#9639): This needs to handle non-utf8 paths
380381
let testfile = testfile.as_str().unwrap().to_string();
381-
test::DynMetricFn(proc(mm) {
382+
test::DynMetricFn(box move |: mm: &mut test::MetricMap| {
382383
runtest::run_metrics(config, testfile, mm)
383384
})
384385
}

src/compiletest/runtest.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ fn run_debuginfo_gdb_test(config: &Config, props: &TestProps, testfile: &Path) {
445445
loop {
446446
//waiting 1 second for gdbserver start
447447
timer::sleep(Duration::milliseconds(1000));
448-
let result = task::try(proc() {
448+
let result = task::try(move || {
449449
tcp::TcpStream::connect("127.0.0.1:5039").unwrap();
450450
});
451451
if result.is_err() {

src/doc/guide-tasks.md

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,37 @@ with a closure argument. `spawn` executes the closure in the new task.
2929
fn print_message() { println!("I am running in a different task!"); }
3030
spawn(print_message);
3131
32-
// Alternatively, use a `proc` expression instead of a named function.
33-
// The `proc` expression evaluates to an (unnamed) proc.
34-
// That proc will call `println!(...)` when the spawned task runs.
35-
spawn(proc() println!("I am also running in a different task!") );
32+
// Alternatively, use a `move ||` expression instead of a named function.
33+
// `||` expressions evaluate to an unnamed closures. The `move` keyword
34+
// indicates that the closure should take ownership of any variables it
35+
// touches.
36+
spawn(move || println!("I am also running in a different task!"));
3637
```
3738

3839
In Rust, a task is not a concept that appears in the language semantics.
3940
Instead, Rust's type system provides all the tools necessary to implement safe
4041
concurrency: particularly, ownership. The language leaves the implementation
4142
details to the standard library.
4243

43-
The `spawn` function has a very simple type signature: `fn spawn(f: proc():
44-
Send)`. Because it accepts only procs, and procs contain only owned data,
45-
`spawn` can safely move the entire proc and all its associated state into an
46-
entirely different task for execution. Like any closure, the function passed to
47-
`spawn` may capture an environment that it carries across tasks.
44+
The `spawn` function has the type signature: `fn
45+
spawn<F:FnOnce()+Send>(f: F)`. This indicates that it takes as
46+
argument a closure (of type `F`) that it will run exactly once. This
47+
closure is limited to capturing `Send`-able data form its environment
48+
(that is, data which is deeply owned). Limiting the closure to `Send`
49+
ensures that `spawn` can safely move the entire closure and all its
50+
associated state into an entirely different task for execution.
4851

4952
```{rust}
5053
# use std::task::spawn;
5154
# fn generate_task_number() -> int { 0 }
5255
// Generate some state locally
5356
let child_task_number = generate_task_number();
5457
55-
spawn(proc() {
56-
// Capture it in the remote task
58+
spawn(move || {
59+
// Capture it in the remote task. The `move` keyword indicates
60+
// that this closure should move `child_task_number` into its
61+
// environment, rather than capturing a reference into the
62+
// enclosing stack frame.
5763
println!("I am child number {}", child_task_number);
5864
});
5965
```
@@ -74,7 +80,7 @@ example of calculating two results concurrently:
7480
7581
let (tx, rx): (Sender<int>, Receiver<int>) = channel();
7682
77-
spawn(proc() {
83+
spawn(move || {
7884
let result = some_expensive_computation();
7985
tx.send(result);
8086
});
@@ -102,7 +108,7 @@ task.
102108
# use std::task::spawn;
103109
# fn some_expensive_computation() -> int { 42 }
104110
# let (tx, rx) = channel();
105-
spawn(proc() {
111+
spawn(move || {
106112
let result = some_expensive_computation();
107113
tx.send(result);
108114
});
@@ -135,13 +141,13 @@ results across a number of tasks? The following program is ill-typed:
135141
# fn some_expensive_computation() -> int { 42 }
136142
let (tx, rx) = channel();
137143
138-
spawn(proc() {
144+
spawn(move || {
139145
tx.send(some_expensive_computation());
140146
});
141147
142148
// ERROR! The previous spawn statement already owns the sender,
143149
// so the compiler will not allow it to be captured again
144-
spawn(proc() {
150+
spawn(move || {
145151
tx.send(some_expensive_computation());
146152
});
147153
```
@@ -154,7 +160,7 @@ let (tx, rx) = channel();
154160
for init_val in range(0u, 3) {
155161
// Create a new channel handle to distribute to the child task
156162
let child_tx = tx.clone();
157-
spawn(proc() {
163+
spawn(move || {
158164
child_tx.send(some_expensive_computation(init_val));
159165
});
160166
}
@@ -179,7 +185,7 @@ reference, written with multiple streams, it might look like the example below.
179185
// Create a vector of ports, one for each child task
180186
let rxs = Vec::from_fn(3, |init_val| {
181187
let (tx, rx) = channel();
182-
spawn(proc() {
188+
spawn(move || {
183189
tx.send(some_expensive_computation(init_val));
184190
});
185191
rx
@@ -207,7 +213,7 @@ fn fib(n: u64) -> u64 {
207213
12586269025
208214
}
209215
210-
let mut delayed_fib = Future::spawn(proc() fib(50));
216+
let mut delayed_fib = Future::spawn(move || fib(50));
211217
make_a_sandwich();
212218
println!("fib(50) = {}", delayed_fib.get())
213219
# }
@@ -236,7 +242,7 @@ fn partial_sum(start: uint) -> f64 {
236242
}
237243
238244
fn main() {
239-
let mut futures = Vec::from_fn(200, |ind| Future::spawn( proc() { partial_sum(ind) }));
245+
let mut futures = Vec::from_fn(200, |ind| Future::spawn(move || partial_sum(ind)));
240246
241247
let mut final_res = 0f64;
242248
for ft in futures.iter_mut() {
@@ -278,7 +284,7 @@ fn main() {
278284
for num in range(1u, 10) {
279285
let task_numbers = numbers_arc.clone();
280286
281-
spawn(proc() {
287+
spawn(move || {
282288
println!("{}-norm = {}", num, pnorm(task_numbers.as_slice(), num));
283289
});
284290
}
@@ -312,7 +318,7 @@ if it were local.
312318
# let numbers_arc = Arc::new(numbers);
313319
# let num = 4;
314320
let task_numbers = numbers_arc.clone();
315-
spawn(proc() {
321+
spawn(move || {
316322
// Capture task_numbers and use it as if it was the underlying vector
317323
println!("{}-norm = {}", num, pnorm(task_numbers.as_slice(), num));
318324
});
@@ -344,7 +350,7 @@ result with an `int` field (representing a successful result) or an `Err` result
344350
# use std::task;
345351
# fn some_condition() -> bool { false }
346352
# fn calculate_result() -> int { 0 }
347-
let result: Result<int, Box<std::any::Any + Send>> = task::try(proc() {
353+
let result: Result<int, Box<std::any::Any + Send>> = task::try(move || {
348354
if some_condition() {
349355
calculate_result()
350356
} else {

src/doc/rustdoc.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,11 +210,11 @@ that one can still write things like `#[deriving(Eq)]`).
210210
# // what's actually being documented.
211211
# fn fib(n: int) { n + 2 }
212212

213-
spawn(proc() { fib(200); })
213+
spawn(move || { fib(200); })
214214
```
215215
~~~
216216

217-
The documentation online would look like `spawn(proc() { fib(200); })`, but when
217+
The documentation online would look like `spawn(move || { fib(200); })`, but when
218218
testing this code, the `fib` function will be included (so it can compile).
219219

220220
## Running tests (advanced)

src/liballoc/arc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ use heap::deallocate;
4646
/// for _ in range(0u, 10) {
4747
/// let child_numbers = shared_numbers.clone();
4848
///
49-
/// spawn(proc() {
49+
/// spawn(move || {
5050
/// let local_numbers = child_numbers.as_slice();
5151
///
5252
/// // Work with the local numbers
@@ -358,7 +358,7 @@ mod tests {
358358

359359
let (tx, rx) = channel();
360360

361-
task::spawn(proc() {
361+
task::spawn(move || {
362362
let arc_v: Arc<Vec<int>> = rx.recv();
363363
assert_eq!((*arc_v)[3], 4);
364364
});

src/libcollections/dlist.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ mod tests {
11331133
#[test]
11341134
fn test_send() {
11351135
let n = list_from(&[1i,2,3]);
1136-
spawn(proc() {
1136+
spawn(move || {
11371137
check_links(&n);
11381138
let a: &[_] = &[&1,&2,&3];
11391139
assert_eq!(a, n.iter().collect::<Vec<&int>>());

src/libflate/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
2222
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
2323
html_root_url = "http://doc.rust-lang.org/nightly/")]
24-
#![feature(phase)]
24+
#![feature(phase, unboxed_closures)]
2525

2626
#[cfg(test)] #[phase(plugin, link)] extern crate log;
2727

@@ -59,7 +59,7 @@ fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
5959
&mut outsz,
6060
flags);
6161
if !res.is_null() {
62-
Some(CVec::new_with_dtor(res as *mut u8, outsz as uint, proc() libc::free(res)))
62+
Some(CVec::new_with_dtor(res as *mut u8, outsz as uint, move|:| libc::free(res)))
6363
} else {
6464
None
6565
}
@@ -84,7 +84,7 @@ fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
8484
&mut outsz,
8585
flags);
8686
if !res.is_null() {
87-
Some(CVec::new_with_dtor(res as *mut u8, outsz as uint, proc() libc::free(res)))
87+
Some(CVec::new_with_dtor(res as *mut u8, outsz as uint, move|:| libc::free(res)))
8888
} else {
8989
None
9090
}

src/liblog/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
166166
html_root_url = "http://doc.rust-lang.org/nightly/",
167167
html_playground_url = "http://play.rust-lang.org/")]
168-
#![feature(macro_rules)]
168+
#![feature(macro_rules, unboxed_closures)]
169169
#![deny(missing_docs)]
170170

171171
extern crate regex;
@@ -422,7 +422,7 @@ fn init() {
422422
DIRECTIVES = mem::transmute(box directives);
423423

424424
// Schedule the cleanup for the globals for when the runtime exits.
425-
rt::at_exit(proc() {
425+
rt::at_exit(move |:| {
426426
assert!(!DIRECTIVES.is_null());
427427
let _directives: Box<Vec<directive::LogDirective>> =
428428
mem::transmute(DIRECTIVES);

src/librustc_driver/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub mod driver;
7171
pub mod pretty;
7272

7373
pub fn run(args: Vec<String>) -> int {
74-
monitor(proc() run_compiler(args.as_slice()));
74+
monitor(move |:| run_compiler(args.as_slice()));
7575
0
7676
}
7777

@@ -471,7 +471,7 @@ pub fn list_metadata(sess: &Session, path: &Path,
471471
///
472472
/// The diagnostic emitter yielded to the procedure should be used for reporting
473473
/// errors of the compiler.
474-
pub fn monitor(f: proc():Send) {
474+
pub fn monitor<F:FnOnce()+Send>(f: F) {
475475
static STACK_SIZE: uint = 32000000; // 32MB
476476

477477
let (tx, rx) = channel();

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ mod callee;
136136
/// closures defined within the function. For example:
137137
///
138138
/// fn foo() {
139-
/// bar(proc() { ... })
139+
/// bar(move|| { ... })
140140
/// }
141141
///
142142
/// Here, the function `foo()` and the closure passed to

src/librustdoc/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ fn rust_input(cratefile: &str, externs: core::Externs, matches: &getopts::Matche
342342

343343
let cr = Path::new(cratefile);
344344
info!("starting to run rustc");
345-
let (mut krate, analysis) = std::task::try(proc() {
345+
let (mut krate, analysis) = std::task::try(move |:| {
346346
let cr = cr;
347347
core::run_core(libs, cfgs, externs, &cr, triple)
348348
}).map_err(|_| "rustc failed").unwrap();

src/librustdoc/test.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::io;
1515
use std::os;
1616
use std::str;
1717
use std::string::String;
18+
use std::thunk::Thunk;
1819

1920
use std::collections::{HashSet, HashMap};
2021
use testing;
@@ -142,7 +143,7 @@ fn runtest(test: &str, cratename: &str, libs: Vec<Path>, externs: core::Externs,
142143
let w1 = io::ChanWriter::new(tx);
143144
let w2 = w1.clone();
144145
let old = io::stdio::set_stderr(box w1);
145-
spawn(proc() {
146+
spawn(move |:| {
146147
let mut p = io::ChanReader::new(rx);
147148
let mut err = match old {
148149
Some(old) => {
@@ -282,15 +283,15 @@ impl Collector {
282283
ignore: should_ignore,
283284
should_fail: testing::ShouldFail::No, // compiler failures are test failures
284285
},
285-
testfn: testing::DynTestFn(proc() {
286+
testfn: testing::DynTestFn(Thunk::new(move|| {
286287
runtest(test.as_slice(),
287288
cratename.as_slice(),
288289
libs,
289290
externs,
290291
should_fail,
291292
no_run,
292293
as_test_harness);
293-
}),
294+
}))
294295
});
295296
}
296297

src/librustrt/c_str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ mod tests {
639639

640640
#[test]
641641
fn test_to_c_str_fail() {
642-
assert!(task::try(proc() { "he\x00llo".to_c_str() }).is_err());
642+
assert!(task::try(move|| { "he\x00llo".to_c_str() }).is_err());
643643
}
644644

645645
#[test]

src/librustrt/exclusive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ mod tests {
9999
let (tx, rx) = channel();
100100
futures.push(rx);
101101

102-
task::spawn(proc() {
102+
task::spawn(move || {
103103
for _ in range(0u, count) {
104104
**total.lock() += 1;
105105
}

0 commit comments

Comments
 (0)