Skip to content

Commit 682b8ad

Browse files
author
Olivier Saut
committed
---
yaml --- r: 65052 b: refs/heads/master c: 4b13895 h: refs/heads/master v: v3
1 parent 1604e66 commit 682b8ad

Some content is hidden

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

49 files changed

+393
-971
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 5242464d543f0a7e811bcfd7fe8943b62842148a
2+
refs/heads/master: 4b13895c2f43024b2b0986d4c2ddcb742926c3c6
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 18e3db7392d2d0697b7e27d6d986139960144d85
55
refs/heads/try: 7b78b52e602bb3ea8174f9b2006bff3315f03ef9

trunk/doc/tutorial-tasks.md

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,24 @@ in the core and standard libraries, which are still under development
4343
and do not always present a consistent or complete interface.
4444

4545
For your reference, these are the standard modules involved in Rust
46-
concurrency at this writing.
46+
concurrency at this writing:
4747

48-
* [`core::task`] - All code relating to tasks and task scheduling
49-
* [`core::comm`] - The message passing interface
50-
* [`core::pipes`] - The underlying messaging infrastructure
51-
* [`std::comm`] - Additional messaging types based on `core::pipes`
52-
* [`std::sync`] - More exotic synchronization tools, including locks
48+
* [`core::task`] - All code relating to tasks and task scheduling,
49+
* [`core::comm`] - The message passing interface,
50+
* [`core::pipes`] - The underlying messaging infrastructure,
51+
* [`std::comm`] - Additional messaging types based on `core::pipes`,
52+
* [`std::sync`] - More exotic synchronization tools, including locks,
5353
* [`std::arc`] - The ARC (atomically reference counted) type,
54-
for safely sharing immutable data
54+
for safely sharing immutable data,
55+
* [`std::future`] - A type representing values that may be computed concurrently and retrieved at a later time.
5556

5657
[`core::task`]: core/task.html
5758
[`core::comm`]: core/comm.html
5859
[`core::pipes`]: core/pipes.html
5960
[`std::comm`]: std/comm.html
6061
[`std::sync`]: std/sync.html
6162
[`std::arc`]: std/arc.html
63+
[`std::future`]: std/future.html
6264

6365
# Basics
6466

@@ -70,7 +72,7 @@ closure in the new task.
7072

7173
~~~~
7274
# use core::io::println;
73-
use core::task::spawn;
75+
# use core::task::spawn;
7476
7577
// Print something profound in a different task using a named function
7678
fn print_message() { println("I am running in a different task!"); }
@@ -145,8 +147,8 @@ endpoint. Consider the following example of calculating two results
145147
concurrently:
146148

147149
~~~~
148-
use core::task::spawn;
149-
use core::comm::{stream, Port, Chan};
150+
# use core::task::spawn;
151+
# use core::comm::{stream, Port, Chan};
150152
151153
let (port, chan): (Port<int>, Chan<int>) = stream();
152154
@@ -233,7 +235,7 @@ Instead we can use a `SharedChan`, a type that allows a single
233235

234236
~~~
235237
# use core::task::spawn;
236-
use core::comm::{stream, SharedChan};
238+
# use core::comm::{stream, SharedChan};
237239
238240
let (port, chan) = stream();
239241
let chan = SharedChan::new(chan);
@@ -282,6 +284,51 @@ let result = ports.foldl(0, |accum, port| *accum + port.recv() );
282284
# fn some_expensive_computation(_i: uint) -> int { 42 }
283285
~~~
284286

287+
## Futures
288+
With `std::future`, rust has a mechanism for requesting a computation and getting the result
289+
later.
290+
291+
The basic example below illustrates this.
292+
~~~
293+
# fn make_a_sandwich() {};
294+
fn fib(n: uint) -> uint {
295+
// lengthy computation returning an uint
296+
12586269025
297+
}
298+
299+
let mut delayed_fib = std::future::spawn (|| fib(50) );
300+
make_a_sandwich();
301+
println(fmt!("fib(50) = %?", delayed_fib.get()))
302+
~~~
303+
304+
The call to `future::spawn` returns immediately a `future` object regardless of how long it
305+
takes to run `fib(50)`. You can then make yourself a sandwich while the computation of `fib` is
306+
running. The result of the execution of the method is obtained by calling `get` on the future.
307+
This call will block until the value is available (*i.e.* the computation is complete). Note that
308+
the future needs to be mutable so that it can save the result for next time `get` is called.
309+
310+
Here is another example showing how futures allow you to background computations. The workload will
311+
be distributed on the available cores.
312+
~~~
313+
fn partial_sum(start: uint) -> f64 {
314+
let mut local_sum = 0f64;
315+
for uint::range(start*100000, (start+1)*100000) |num| {
316+
local_sum += (num as f64 + 1.0).pow(-2.0);
317+
}
318+
local_sum
319+
}
320+
321+
fn main() {
322+
let mut futures = vec::from_fn(1000, |ind| do std::future::spawn { partial_sum(ind) });
323+
324+
let mut final_res = 0f64;
325+
for futures.each_mut |ft| {
326+
final_res += ft.get();
327+
}
328+
println(fmt!("π^2/6 is not far from : %?", final_res));
329+
}
330+
~~~
331+
285332
# Handling task failure
286333

287334
Rust has a built-in mechanism for raising exceptions. The `fail!()` macro
@@ -363,8 +410,8 @@ either task fails, it kills the other one.
363410
~~~
364411
# fn sleep_forever() { loop { task::yield() } }
365412
# do task::try {
366-
do task::spawn {
367-
do task::spawn {
413+
do spawn {
414+
do spawn {
368415
fail!(); // All three tasks will fail.
369416
}
370417
sleep_forever(); // Will get woken up by force, then fail

trunk/mk/platform.mk

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -239,32 +239,6 @@ CFG_RUN_arm-linux-androideabi=
239239
CFG_RUN_TARG_arm-linux-androideabi=
240240
RUSTC_FLAGS_arm-linux-androideabi :=--android-cross-path=$(CFG_ANDROID_CROSS_PATH)
241241

242-
# arm-unknown-linux-gnueabihf configuration
243-
CC_arm-unknown-linux-gnueabihf=arm-linux-gnueabihf-gcc
244-
CXX_arm-unknown-linux-gnueabihf=arm-linux-gnueabihf-g++
245-
CPP_arm-unknown-linux-gnueabihf=arm-linux-gnueabihf-gcc -E
246-
AR_arm-unknown-linux-gnueabihf=arm-linux-gnueabihf-ar
247-
CFG_LIB_NAME_arm-unknown-linux-gnueabihf=lib$(1).so
248-
CFG_LIB_GLOB_arm-unknown-linux-gnueabihf=lib$(1)-*.so
249-
CFG_LIB_DSYM_GLOB_arm-unknown-linux-gnueabihf=lib$(1)-*.dylib.dSYM
250-
CFG_GCCISH_CFLAGS_arm-unknown-linux-gnueabihf := -Wall -g -fPIC
251-
CFG_GCCISH_CXXFLAGS_arm-unknown-linux-gnueabihf := -fno-rtti
252-
CFG_GCCISH_LINK_FLAGS_arm-unknown-linux-gnueabihf := -shared -fPIC -g
253-
CFG_GCCISH_DEF_FLAG_arm-unknown-linux-gnueabihf := -Wl,--export-dynamic,--dynamic-list=
254-
CFG_GCCISH_PRE_LIB_FLAGS_arm-unknown-linux-gnueabihf := -Wl,-whole-archive
255-
CFG_GCCISH_POST_LIB_FLAGS_arm-unknown-linux-gnueabihf := -Wl,-no-whole-archive
256-
CFG_DEF_SUFFIX_arm-unknown-linux-gnueabihf := .linux.def
257-
CFG_INSTALL_NAME_ar,-unknown-linux-gnueabihf =
258-
CFG_LIBUV_LINK_FLAGS_arm-unknown-linux-gnueabihf =
259-
CFG_EXE_SUFFIX_arm-unknown-linux-gnueabihf :=
260-
CFG_WINDOWSY_arm-unknown-linux-gnueabihf :=
261-
CFG_UNIXY_arm-unknown-linux-gnueabihf := 1
262-
CFG_PATH_MUNGE_arm-unknown-linux-gnueabihf := true
263-
CFG_LDPATH_arm-unknown-linux-gnueabihf :=
264-
CFG_RUN_arm-unknown-linux-gnueabihf=
265-
CFG_RUN_TARG_arm-unknown-linux-gnueabihf=
266-
RUSTC_FLAGS_arm-unknown-linux-gnueabihf := --linker=$(CC_arm-unknown-linux-gnueabihf)
267-
268242
# mips-unknown-linux-gnu configuration
269243
CC_mips-unknown-linux-gnu=mips-linux-gnu-gcc
270244
CXX_mips-unknown-linux-gnu=mips-linux-gnu-g++

trunk/mk/rt.mk

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@
2626
# Hack for passing flags into LIBUV, see below.
2727
LIBUV_FLAGS_i386 = -m32 -fPIC
2828
LIBUV_FLAGS_x86_64 = -m64 -fPIC
29-
ifeq ($(OSTYPE_$(1)), linux-androideabi)
3029
LIBUV_FLAGS_arm = -fPIC -DANDROID -std=gnu99
31-
else
32-
LIBUV_FLAGS_arm = -fPIC -std=gnu99
33-
endif
3430
LIBUV_FLAGS_mips = -fPIC -mips32r2 -msoft-float -mabi=32
3531

3632
# when we're doing a snapshot build, we intentionally degrade as many

trunk/src/libcore/bool.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ pub fn is_false(v: bool) -> bool { !v }
4949
/// Parse logic value from `s`
5050
impl FromStr for bool {
5151
fn from_str(s: &str) -> Option<bool> {
52-
match s {
53-
"true" => Some(true),
54-
"false" => Some(false),
55-
_ => None,
52+
if s == "true" {
53+
Some(true)
54+
} else if s == "false" {
55+
Some(false)
56+
} else {
57+
None
5658
}
5759
}
5860
}

trunk/src/libcore/cmp.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ totalord_impl!(uint)
127127

128128
totalord_impl!(char)
129129

130-
/// Compares (a1, b1) against (a2, b2), where the a values are more significant.
131130
pub fn cmp2<A:TotalOrd,B:TotalOrd>(
132131
a1: &A, b1: &B,
133132
a2: &A, b2: &B) -> Ordering
134133
{
134+
//! Compares (a1, b1) against (a2, b2), where the a values are more significant.
135+
135136
match a1.cmp(a2) {
136137
Less => Less,
137138
Greater => Greater,

trunk/src/libcore/either.rs

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,26 @@ pub enum Either<T, U> {
2626
Right(U)
2727
}
2828

29-
/// Applies a function based on the given either value
30-
///
31-
/// If `value` is left(T) then `f_left` is applied to its contents, if
32-
/// `value` is right(U) then `f_right` is applied to its contents, and the
33-
/// result is returned.
3429
#[inline(always)]
3530
pub fn either<T, U, V>(f_left: &fn(&T) -> V,
3631
f_right: &fn(&U) -> V, value: &Either<T, U>) -> V {
32+
/*!
33+
* Applies a function based on the given either value
34+
*
35+
* If `value` is left(T) then `f_left` is applied to its contents, if
36+
* `value` is right(U) then `f_right` is applied to its contents, and the
37+
* result is returned.
38+
*/
39+
3740
match *value {
38-
Left(ref l) => f_left(l),
39-
Right(ref r) => f_right(r)
41+
Left(ref l) => f_left(l),
42+
Right(ref r) => f_right(r)
4043
}
4144
}
4245

43-
/// Extracts from a vector of either all the left values
4446
pub fn lefts<T:Copy,U>(eithers: &[Either<T, U>]) -> ~[T] {
47+
//! Extracts from a vector of either all the left values
48+
4549
do vec::build_sized(eithers.len()) |push| {
4650
for eithers.each |elt| {
4751
match *elt {
@@ -52,8 +56,9 @@ pub fn lefts<T:Copy,U>(eithers: &[Either<T, U>]) -> ~[T] {
5256
}
5357
}
5458

55-
/// Extracts from a vector of either all the right values
5659
pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
60+
//! Extracts from a vector of either all the right values
61+
5762
do vec::build_sized(eithers.len()) |push| {
5863
for eithers.each |elt| {
5964
match *elt {
@@ -64,73 +69,80 @@ pub fn rights<T, U: Copy>(eithers: &[Either<T, U>]) -> ~[U] {
6469
}
6570
}
6671

67-
/// Extracts from a vector of either all the left values and right values
68-
///
69-
/// Returns a structure containing a vector of left values and a vector of
70-
/// right values.
71-
pub fn partition<T, U>(eithers: ~[Either<T, U>]) -> (~[T], ~[U]) {
72+
pub fn partition<T, U>(eithers: ~[Either<T, U>])
73+
-> (~[T], ~[U]) {
74+
/*!
75+
* Extracts from a vector of either all the left values and right values
76+
*
77+
* Returns a structure containing a vector of left values and a vector of
78+
* right values.
79+
*/
80+
7281
let mut lefts: ~[T] = ~[];
7382
let mut rights: ~[U] = ~[];
7483
do vec::consume(eithers) |_i, elt| {
7584
match elt {
76-
Left(l) => lefts.push(l),
77-
Right(r) => rights.push(r)
85+
Left(l) => lefts.push(l),
86+
Right(r) => rights.push(r)
7887
}
7988
}
8089
return (lefts, rights);
8190
}
8291

83-
/// Flips between left and right of a given either
8492
#[inline(always)]
8593
pub fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
94+
//! Flips between left and right of a given either
95+
8696
match eith {
87-
Right(r) => Left(r),
88-
Left(l) => Right(l)
97+
Right(r) => Left(r),
98+
Left(l) => Right(l)
8999
}
90100
}
91101

92-
/// Converts either::t to a result::t
93-
///
94-
/// Converts an `either` type to a `result` type, making the "right" choice
95-
/// an ok result, and the "left" choice a fail
96102
#[inline(always)]
97-
pub fn to_result<T, U>(eith: Either<T, U>) -> Result<U, T> {
103+
pub fn to_result<T, U>(eith: Either<T, U>)
104+
-> Result<U, T> {
105+
/*!
106+
* Converts either::t to a result::t
107+
*
108+
* Converts an `either` type to a `result` type, making the "right" choice
109+
* an ok result, and the "left" choice a fail
110+
*/
111+
98112
match eith {
99-
Right(r) => result::Ok(r),
100-
Left(l) => result::Err(l)
113+
Right(r) => result::Ok(r),
114+
Left(l) => result::Err(l)
101115
}
102116
}
103117

104-
/// Checks whether the given value is a left
105118
#[inline(always)]
106119
pub fn is_left<T, U>(eith: &Either<T, U>) -> bool {
107-
match *eith {
108-
Left(_) => true,
109-
_ => false
110-
}
120+
//! Checks whether the given value is a left
121+
122+
match *eith { Left(_) => true, _ => false }
111123
}
112124

113-
/// Checks whether the given value is a right
114125
#[inline(always)]
115126
pub fn is_right<T, U>(eith: &Either<T, U>) -> bool {
116-
match *eith {
117-
Right(_) => true,
118-
_ => false
119-
}
127+
//! Checks whether the given value is a right
128+
129+
match *eith { Right(_) => true, _ => false }
120130
}
121131

122-
/// Retrieves the value in the left branch. Fails if the either is Right.
123132
#[inline(always)]
124133
pub fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
134+
//! Retrieves the value in the left branch. Fails if the either is Right.
135+
125136
match eith {
126137
Left(x) => x,
127138
Right(_) => fail!("either::unwrap_left Right")
128139
}
129140
}
130141

131-
/// Retrieves the value in the right branch. Fails if the either is Left.
132142
#[inline(always)]
133143
pub fn unwrap_right<T,U>(eith: Either<T,U>) -> U {
144+
//! Retrieves the value in the right branch. Fails if the either is Left.
145+
134146
match eith {
135147
Right(x) => x,
136148
Left(_) => fail!("either::unwrap_right Left")

0 commit comments

Comments
 (0)