Skip to content

Commit b8549b2

Browse files
committed
---
yaml --- r: 273739 b: refs/heads/beta c: 161c541 h: refs/heads/master i: 273737: 3492f59 273735: 5c3d49e
1 parent 5cb7f01 commit b8549b2

Some content is hidden

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

56 files changed

+858
-351
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ refs/tags/0.9: 36870b185fc5f5486636d4515f0e22677493f225
2323
refs/tags/0.10: ac33f2b15782272ae348dbd7b14b8257b2148b5a
2424
refs/tags/0.11.0: e1247cb1d0d681be034adb4b558b5a0c0d5720f9
2525
refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
26-
refs/heads/beta: f611e446624d288da7def4ad175405579c8bc310
26+
refs/heads/beta: 161c541afdd18423940e97c7a02b517b1f6d61be
2727
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
2828
refs/heads/tmp: e06d2ad9fcd5027bcaac5b08fc9aa39a49d0ecd3
2929
refs/tags/1.0.0-alpha.2: 4c705f6bc559886632d3871b04f58aab093bfa2f

branches/beta/CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ Some common make targets are:
132132
- `make check-stage1-std NO_REBUILD=1` - test the standard library without
133133
rebuilding the entire compiler
134134
- `make check TESTNAME=<substring-of-test-name>` - Run a matching set of tests.
135-
- `TESTNAME` should be a substring of the tests to match against e.g. it could
136-
be the fully qualified test name, or just a part of it.
135+
- `TESTNAME` should be a substring of the tests to match against e.g. it could
136+
be the fully qualified test name, or just a part of it.
137137
`TESTNAME=collections::hash::map::test_map::test_capacity_not_less_than_len`
138138
or `TESTNAME=test_capacity_not_less_than_len`.
139139
- `make check-stage1-rpass TESTNAME=<substring-of-test-name>` - Run a single

branches/beta/mk/target.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ $$(TLIB$(1)_T_$(2)_H_$(3))/stamp.$(4): \
8989
$$(RUSTFLAGS$(1)_$(4)_T_$(2)) \
9090
--out-dir $$(@D) \
9191
-C extra-filename=-$$(CFG_FILENAME_EXTRA) \
92+
-C metadata=$$(CFG_FILENAME_EXTRA) \
9293
$$<
9394
@touch -r $$@.start_time $$@ && rm $$@.start_time
9495
$$(call LIST_ALL_OLD_GLOB_MATCHES, \

branches/beta/src/compiletest/runtest.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,8 +727,11 @@ fn run_debuginfo_lldb_test(config: &Config, props: &TestProps, testpaths: &TestP
727727
script_str.push_str("type category enable Rust\n");
728728

729729
// Set breakpoints on every line that contains the string "#break"
730+
let source_file_name = testpaths.file.file_name().unwrap().to_string_lossy();
730731
for line in &breakpoint_lines {
731-
script_str.push_str(&format!("breakpoint set --line {}\n", line));
732+
script_str.push_str(&format!("breakpoint set --file '{}' --line {}\n",
733+
source_file_name,
734+
line));
732735
}
733736

734737
// Append the other commands

branches/beta/src/doc/book/casting-between-types.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ function result.
1717
The most common case of coercion is removing mutability from a reference:
1818

1919
* `&mut T` to `&T`
20-
20+
2121
An analogous conversion is to remove mutability from a
2222
[raw pointer](raw-pointers.md):
2323

2424
* `*mut T` to `*const T`
25-
25+
2626
References can also be coerced to raw pointers:
2727

2828
* `&T` to `*const T`
@@ -32,7 +32,7 @@ References can also be coerced to raw pointers:
3232
Custom coercions may be defined using [`Deref`](deref-coercions.md).
3333

3434
Coercion is transitive.
35-
35+
3636
# `as`
3737

3838
The `as` keyword does safe casting:
@@ -64,7 +64,7 @@ A cast `e as U` is also valid in any of the following cases:
6464
and `U` is an integer type; *enum-cast*
6565
* `e` has type `bool` or `char` and `U` is an integer type; *prim-int-cast*
6666
* `e` has type `u8` and `U` is `char`; *u8-char-cast*
67-
67+
6868
For example
6969

7070
```rust
@@ -98,9 +98,9 @@ The semantics of numeric casts are:
9898

9999
[float-int]: https://github.com/rust-lang/rust/issues/10184
100100
[float-float]: https://github.com/rust-lang/rust/issues/15536
101-
101+
102102
## Pointer casts
103-
103+
104104
Perhaps surprisingly, it is safe to cast [raw pointers](raw-pointers.md) to and
105105
from integers, and to cast between pointers to different types subject to
106106
some constraints. It is only unsafe to dereference the pointer:
@@ -114,7 +114,7 @@ let b = a as u32;
114114

115115
* `e` has type `*T`, `U` has type `*U_0`, and either `U_0: Sized` or
116116
`unsize_kind(T) == unsize_kind(U_0)`; a *ptr-ptr-cast*
117-
117+
118118
* `e` has type `*T` and `U` is a numeric type, while `T: Sized`; *ptr-addr-cast*
119119

120120
* `e` is an integer and `U` is `*U_0`, while `U_0: Sized`; *addr-ptr-cast*

branches/beta/src/doc/book/closures.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,5 +502,5 @@ assert_eq!(6, answer);
502502
```
503503

504504
By making the inner closure a `move Fn`, we create a new stack frame for our
505-
closure. By `Box`ing it up, we’ve given it a known size, and allowing it to
505+
closure. By `Box`ing it up, we’ve given it a known size, allowing it to
506506
escape our stack frame.

branches/beta/src/doc/book/concurrency.md

Lines changed: 124 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,54 @@ fn main() {
9494
}
9595
```
9696

97+
As closures can capture variables from their environment, we can also try to
98+
bring some data into the other thread:
99+
100+
```rust,ignore
101+
use std::thread;
102+
103+
fn main() {
104+
let x = 1;
105+
thread::spawn(|| {
106+
println!("x is {}", x);
107+
});
108+
}
109+
```
110+
111+
However, this gives us an error:
112+
113+
```text
114+
5:19: 7:6 error: closure may outlive the current function, but it
115+
borrows `x`, which is owned by the current function
116+
...
117+
5:19: 7:6 help: to force the closure to take ownership of `x` (and any other referenced variables),
118+
use the `move` keyword, as shown:
119+
thread::spawn(move || {
120+
println!("x is {}", x);
121+
});
122+
```
123+
124+
This is because by default closures capture variables by reference, and thus the
125+
closure only captures a _reference to `x`_. This is a problem, because the
126+
thread may outlive the scope of `x`, leading to a dangling pointer.
127+
128+
To fix this, we use a `move` closure as mentioned in the error message. `move`
129+
closures are explained in depth [here](closures.html#move-closures); basically
130+
they move variables from their environment into themselves. This means that `x`
131+
is now owned by the closure, and cannot be used in `main()` after the call to
132+
`spawn()`.
133+
134+
```rust
135+
use std::thread;
136+
137+
fn main() {
138+
let x = 1;
139+
thread::spawn(move || {
140+
println!("x is {}", x);
141+
});
142+
}
143+
```
144+
97145
Many languages have the ability to execute threads, but it's wildly unsafe.
98146
There are entire books about how to prevent errors that occur from shared
99147
mutable state. Rust helps out with its type system here as well, by preventing
@@ -145,23 +193,64 @@ This gives us an error:
145193
```
146194

147195
Rust knows this wouldn't be safe! If we had a reference to `data` in each
148-
thread, and the thread takes ownership of the reference, we'd have three
149-
owners!
196+
thread, and the thread takes ownership of the reference, we'd have three owners!
197+
`data` gets moved out of `main` in the first call to `spawn()`, so subsequent
198+
calls in the loop cannot use this variable.
199+
200+
So, we need some type that lets us have more than one owning reference to a
201+
value. Usually, we'd use `Rc<T>` for this, which is a reference counted type
202+
that provides shared ownership. It has some runtime bookkeeping that keeps track
203+
of the number of references to it, hence the "reference count" part of its name.
204+
205+
Calling `clone()` on an `Rc<T>` will return a new owned reference and bump the
206+
internal reference count. We create one of these for each thread:
150207

151-
So, we need some type that lets us have more than one reference to a value and
152-
that we can share between threads, that is it must implement `Sync`.
153208

154-
We'll use `Arc<T>`, Rust's standard atomic reference count type, which
155-
wraps a value up with some extra runtime bookkeeping which allows us to
156-
share the ownership of the value between multiple references at the same time.
209+
```ignore
210+
use std::thread;
211+
use std::time::Duration;
212+
use std::rc::Rc;
157213
158-
The bookkeeping consists of a count of how many of these references exist to
159-
the value, hence the reference count part of the name.
214+
fn main() {
215+
let mut data = Rc::new(vec![1, 2, 3]);
216+
217+
for i in 0..3 {
218+
// create a new owned reference
219+
let data_ref = data.clone();
220+
221+
// use it in a thread
222+
thread::spawn(move || {
223+
data_ref[i] += 1;
224+
});
225+
}
226+
227+
thread::sleep(Duration::from_millis(50));
228+
}
229+
```
230+
231+
This won't work, however, and will give us the error:
232+
233+
```text
234+
13:9: 13:22 error: the trait `core::marker::Send` is not
235+
implemented for the type `alloc::rc::Rc<collections::vec::Vec<i32>>`
236+
...
237+
13:9: 13:22 note: `alloc::rc::Rc<collections::vec::Vec<i32>>`
238+
cannot be sent between threads safely
239+
```
240+
241+
As the error message mentions, `Rc` cannot be sent between threads safely. This
242+
is because the internal reference count is not maintained in a thread safe
243+
matter and can have a data race.
244+
245+
To solve this, we'll use `Arc<T>`, Rust's standard atomic reference count type.
160246

161247
The Atomic part means `Arc<T>` can safely be accessed from multiple threads.
162248
To do this the compiler guarantees that mutations of the internal count use
163249
indivisible operations which can't have data races.
164250

251+
In essence, `Arc<T>` is a type that lets us share ownership of data _across
252+
threads_.
253+
165254

166255
```ignore
167256
use std::thread;
@@ -182,7 +271,7 @@ fn main() {
182271
}
183272
```
184273

185-
We now call `clone()` on our `Arc<T>`, which increases the internal count.
274+
Similarly to last time, we use `clone()` to create a new owned handle.
186275
This handle is then moved into the new thread.
187276

188277
And... still gives us an error.
@@ -193,14 +282,21 @@ And... still gives us an error.
193282
^~~~
194283
```
195284

196-
`Arc<T>` assumes one more property about its contents to ensure that it is safe
197-
to share across threads: it assumes its contents are `Sync`. This is true for
198-
our value if it's immutable, but we want to be able to mutate it, so we need
199-
something else to persuade the borrow checker we know what we're doing.
285+
`Arc<T>` by default has immutable contents. It allows the _sharing_ of data
286+
between threads, but shared mutable data is unsafe and when threads are
287+
involved can cause data races!
288+
200289

201-
It looks like we need some type that allows us to safely mutate a shared value,
202-
for example a type that can ensure only one thread at a time is able to
203-
mutate the value inside it at any one time.
290+
Usually when we wish to make something in an immutable position mutable, we use
291+
`Cell<T>` or `RefCell<T>` which allow safe mutation via runtime checks or
292+
otherwise (see also: [Choosing Your Guarantees](choosing-your-guarantees.html)).
293+
However, similar to `Rc`, these are not thread safe. If we try using these, we
294+
will get an error about these types not being `Sync`, and the code will fail to
295+
compile.
296+
297+
It looks like we need some type that allows us to safely mutate a shared value
298+
across threads, for example a type that can ensure only one thread at a time is
299+
able to mutate the value inside it at any one time.
204300

205301
For that, we can use the `Mutex<T>` type!
206302

@@ -229,7 +325,17 @@ fn main() {
229325
Note that the value of `i` is bound (copied) to the closure and not shared
230326
among the threads.
231327

232-
Also note that [`lock`](../std/sync/struct.Mutex.html#method.lock) method of
328+
We're "locking" the mutex here. A mutex (short for "mutual exclusion"), as
329+
mentioned, only allows one thread at a time to access a value. When we wish to
330+
access the value, we use `lock()` on it. This will "lock" the mutex, and no
331+
other thread will be able to lock it (and hence, do anything with the value)
332+
until we're done with it. If a thread attempts to lock a mutex which is already
333+
locked, it will wait until the other thread releases the lock.
334+
335+
The lock "release" here is implicit; when the result of the lock (in this case,
336+
`data`) goes out of scope, the lock is automatically released.
337+
338+
Note that [`lock`](../std/sync/struct.Mutex.html#method.lock) method of
233339
[`Mutex`](../std/sync/struct.Mutex.html) has this signature:
234340

235341
```ignore

branches/beta/src/doc/book/const-and-static.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ a [`Drop`][drop] implementation.
7272
# Initializing
7373

7474
Both `const` and `static` have requirements for giving them a value. They must
75-
be given a value that’s a constant expression. In other words, you cannot use
75+
be given a value that’s a constant expression. In other words, you cannot use
7676
the result of a function call or anything similarly complex or at runtime.
7777

7878
# Which construct should I use?

branches/beta/src/doc/book/getting-started.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ first. This leaves the top-level project directory (in this case,
417417
to your code. In this way, using Cargo helps you keep your projects nice and
418418
tidy. There's a place for everything, and everything is in its place.
419419

420-
Now, copy *main.rs* to the *src* directory, and delete the compiled file you
420+
Now, move *main.rs* into the *src* directory, and delete the compiled file you
421421
created with `rustc`. As usual, replace `main` with `main.exe` if you're on
422422
Windows.
423423

branches/beta/src/doc/book/macros.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ fn main() {
337337
}
338338
```
339339

340-
Instead you need to pass the variable name into the invocation, so it’s tagged
341-
with the right syntax context.
340+
Instead you need to pass the variable name into the invocation, so that it’s
341+
tagged with the right syntax context.
342342

343343
```rust
344344
macro_rules! foo {
@@ -470,7 +470,7 @@ which syntactic form it matches.
470470
* `ty`: a type. Examples: `i32`; `Vec<(char, String)>`; `&T`.
471471
* `pat`: a pattern. Examples: `Some(t)`; `(17, 'a')`; `_`.
472472
* `stmt`: a single statement. Example: `let x = 3`.
473-
* `block`: a brace-delimited sequence of statements. Example:
473+
* `block`: a brace-delimited sequence of statements and optionally an expression. Example:
474474
`{ log(error, "hi"); return 12; }`.
475475
* `item`: an [item][item]. Examples: `fn foo() { }`; `struct Bar;`.
476476
* `meta`: a "meta item", as found in attributes. Example: `cfg(target_os = "windows")`.

branches/beta/src/doc/book/match.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ patterns][patterns] that covers all the patterns that are possible here.
2828

2929
[patterns]: patterns.html
3030

31-
One of the many advantages of `match` is it enforces ‘exhaustiveness checking’.
32-
For example if we remove the last arm with the underscore `_`, the compiler will
31+
One of the many advantages of `match` is it enforces ‘exhaustiveness checking’.
32+
For example if we remove the last arm with the underscore `_`, the compiler will
3333
give us an error:
3434

3535
```text
@@ -58,7 +58,7 @@ let number = match x {
5858
};
5959
```
6060

61-
Sometimes it’s a nice way of converting something from one type to another; in
61+
Sometimes it’s a nice way of converting something from one type to another; in
6262
this example the integers are converted to `String`.
6363

6464
# Matching on enums
@@ -90,7 +90,7 @@ fn process_message(msg: Message) {
9090

9191
Again, the Rust compiler checks exhaustiveness, so it demands that you
9292
have a match arm for every variant of the enum. If you leave one off, it
93-
will give you a compile-time error unless you use `_` or provide all possible
93+
will give you a compile-time error unless you use `_` or provide all possible
9494
arms.
9595

9696
Unlike the previous uses of `match`, you can’t use the normal `if`

branches/beta/src/doc/book/ownership.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ special annotation here, it’s the default thing that Rust does.
124124
## The details
125125

126126
The reason that we cannot use a binding after we’ve moved it is subtle, but
127-
important.
127+
important.
128128

129129
When we write code like this:
130130

@@ -148,7 +148,7 @@ The first line allocates memory for the vector object `v` on the stack like
148148
it does for `x` above. But in addition to that it also allocates some memory
149149
on the [heap][sh] for the actual data (`[1, 2, 3]`). Rust copies the address
150150
of this heap allocation to an internal pointer, which is part of the vector
151-
object placed on the stack (let's call it the data pointer).
151+
object placed on the stack (let's call it the data pointer).
152152

153153
It is worth pointing out (even at the risk of stating the obvious) that the
154154
vector object and its data live in separate memory regions instead of being a
@@ -163,7 +163,7 @@ does not create a copy of the heap allocation containing the actual data.
163163
Which means that there would be two pointers to the contents of the vector
164164
both pointing to the same memory allocation on the heap. It would violate
165165
Rust’s safety guarantees by introducing a data race if one could access both
166-
`v` and `v2` at the same time.
166+
`v` and `v2` at the same time.
167167

168168
For example if we truncated the vector to just two elements through `v2`:
169169

branches/beta/src/doc/book/primitive-types.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ copying. For example, you might want to reference only one line of a file read
164164
into memory. By nature, a slice is not created directly, but from an existing
165165
variable binding. Slices have a defined length, can be mutable or immutable.
166166

167-
Internally, slices are represented as a pointer to the beginning of the data
167+
Internally, slices are represented as a pointer to the beginning of the data
168168
and a length.
169169

170170
## Slicing syntax

branches/beta/src/doc/book/vectors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ for i in v {
116116
```
117117

118118
Note: You cannot use the vector again once you have iterated by taking ownership of the vector.
119-
You can iterate the vector multiple times by taking a reference to the vector whilst iterating.
119+
You can iterate the vector multiple times by taking a reference to the vector whilst iterating.
120120
For example, the following code does not compile.
121121

122122
```rust,ignore

0 commit comments

Comments
 (0)