Skip to content

Commit f2745b3

Browse files
---
yaml --- r: 196582 b: refs/heads/auto c: 92d0026 h: refs/heads/master v: v3
1 parent 072a419 commit f2745b3

Some content is hidden

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

64 files changed

+2364
-3264
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503
1010
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1111
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1212
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
13-
refs/heads/auto: 6f852f562094c926e2aaca11c9f508f60e449302
13+
refs/heads/auto: 92d00262c36e12ed848ca8b248cb81f16e7f2deb
1414
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1515
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1616
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/doc/intro.md

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -457,25 +457,30 @@ a problem. That’s what it means by ‘cannot move out of captured outer
457457
variable’: our `thread::scoped` closure wants to take ownership, and it can’t,
458458
because the closure for `map` won’t let it.
459459
460-
What to do here? Rust has a type that helps us: `Mutex<T>`. Because the threads
461-
are scoped, it is possible to use an _immutable_ reference to `numbers` inside
462-
of the closure. However, Rust prevents us from having multiple _mutable_
463-
references to the same object, so we need a `Mutex` to be able to modify what
464-
we're sharing. A Mutex will synchronize our accesses, so that we can ensure
465-
that our mutation doesn't cause a data race.
466-
467-
Here's what using a Mutex looks like:
460+
What to do here? Rust has two types that helps us: `Arc<T>` and `Mutex<T>`.
461+
*Arc* stands for "atomically reference counted". In other words, an Arc will
462+
keep track of the number of references to something, and not free the
463+
associated resource until the count is zero. The *atomic* portion refers to an
464+
Arc's usage of concurrency primitives to atomically update the count, making it
465+
safe across threads. If we use an Arc, we can have our three references. But,
466+
an Arc does not allow mutable borrows of the data it holds, and we want to
467+
modify what we're sharing. In this case, we can use a `Mutex<T>` inside of our
468+
Arc. A Mutex will synchronize our accesses, so that we can ensure that our
469+
mutation doesn't cause a data race.
470+
471+
Here's what using an Arc with a Mutex looks like:
468472
469473
```{rust}
470474
use std::thread;
471-
use std::sync::Mutex;
475+
use std::sync::{Arc,Mutex};
472476
473477
fn main() {
474-
let numbers = &Mutex::new(vec![1, 2, 3]);
478+
let numbers = Arc::new(Mutex::new(vec![1, 2, 3]));
475479
476480
let guards: Vec<_> = (0..3).map(|i| {
481+
let number = numbers.clone();
477482
thread::scoped(move || {
478-
let mut array = numbers.lock().unwrap();
483+
let mut array = number.lock().unwrap();
479484
array[i] += 1;
480485
println!("numbers[{}] is {}", i, array[i]);
481486
})
@@ -484,9 +489,12 @@ fn main() {
484489
```
485490
486491
We first have to `use` the appropriate library, and then we wrap our vector in
487-
a `Mutex` with the call to `Mutex::new()`. Inside of the loop, the `lock()`
488-
call will return us a reference to the value inside the Mutex, and block any
489-
other calls to `lock()` until said reference goes out of scope.
492+
an Arc with the call to `Arc::new()`. Inside of the loop, we make a new
493+
reference to the Arc with the `clone()` method. This will increment the
494+
reference count. When each new `numbers` variable binding goes out of scope, it
495+
will decrement the count. The `lock()` call will return us a reference to the
496+
value inside the Mutex, and block any other calls to `lock()` until said
497+
reference goes out of scope.
490498
491499
We can compile and run this program without error, and in fact, see the
492500
non-deterministic aspect:

branches/auto/src/doc/trpl/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ and will be able to understand most Rust code and write more complex programs.
2828

2929
In a similar fashion to "Intermediate," this section is full of individual,
3030
deep-dive chapters, which stand alone and can be read in any order. These
31-
chapters focus on Rust's most complex features.
31+
chapters focus on the most complex features,
3232

3333
<h2 class="section-header"><a href="unstable.html">Unstable</a></h2>
3434

branches/auto/src/doc/trpl/benchmark-tests.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn add_two(a: i32) -> i32 {
1313
}
1414
1515
#[cfg(test)]
16-
mod test {
16+
mod tests {
1717
use super::*;
1818
use test::Bencher;
1919

branches/auto/src/doc/trpl/closures.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ The second is that the syntax is similar, but a bit different. I've added spaces
5454
here to make them look a little closer:
5555

5656
```rust
57-
fn plus_one_v1 (x: i32 ) -> i32 { x + 1 }
57+
fn plus_one_v1 ( x: i32 ) -> i32 { x + 1 }
5858
let plus_one_v2 = |x: i32 | -> i32 { x + 1 };
5959
let plus_one_v3 = |x: i32 | x + 1 ;
6060
```

branches/auto/src/doc/trpl/hello-cargo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
% Hello, Cargo!
22

33
[Cargo](http://crates.io) is a tool that Rustaceans use to help manage their
4-
Rust projects. Cargo is currently in an alpha state, just like Rust, and so it
4+
Rust projects. Cargo is currently in a pre-1.0 state, just like Rust, and so it
55
is still a work in progress. However, it is already good enough to use for many
66
Rust projects, and so it is assumed that Rust projects will use Cargo from the
77
beginning.

branches/auto/src/doc/trpl/testing.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ pub fn add_two(a: i32) -> i32 {
382382
}
383383
384384
#[cfg(test)]
385-
mod test {
385+
mod tests {
386386
use super::*;
387387
388388
#[test]

branches/auto/src/liballoc/arc.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -243,29 +243,27 @@ pub fn weak_count<T>(this: &Arc<T>) -> usize { this.inner().weak.load(SeqCst) -
243243
pub fn strong_count<T>(this: &Arc<T>) -> usize { this.inner().strong.load(SeqCst) }
244244

245245

246-
/// Returns a mutable reference to the contained value if the `Arc<T>` is unique.
246+
/// Try accessing a mutable reference to the contents behind an unique `Arc<T>`.
247247
///
248-
/// Returns `None` if the `Arc<T>` is not unique.
248+
/// The access is granted only if this is the only reference to the object.
249+
/// Otherwise, `None` is returned.
249250
///
250251
/// # Examples
251252
///
252253
/// ```
253254
/// # #![feature(alloc)]
254255
/// extern crate alloc;
255256
/// # fn main() {
256-
/// use alloc::arc::{Arc, get_mut};
257+
/// use alloc::arc;
257258
///
258-
/// let mut x = Arc::new(3);
259-
/// *get_mut(&mut x).unwrap() = 4;
260-
/// assert_eq!(*x, 4);
259+
/// let mut four = arc::Arc::new(4);
261260
///
262-
/// let _y = x.clone();
263-
/// assert!(get_mut(&mut x).is_none());
261+
/// arc::unique(&mut four).map(|num| *num = 5);
264262
/// # }
265263
/// ```
266264
#[inline]
267265
#[unstable(feature = "alloc")]
268-
pub fn get_mut<T>(this: &mut Arc<T>) -> Option<&mut T> {
266+
pub fn unique<T>(this: &mut Arc<T>) -> Option<&mut T> {
269267
if strong_count(this) == 1 && weak_count(this) == 0 {
270268
// This unsafety is ok because we're guaranteed that the pointer
271269
// returned is the *only* pointer that will ever be returned to T. Our
@@ -349,7 +347,7 @@ impl<T: Clone> Arc<T> {
349347
self.inner().weak.load(SeqCst) != 1 {
350348
*self = Arc::new((**self).clone())
351349
}
352-
// As with `get_mut()`, the unsafety is ok because our reference was
350+
// As with `unique()`, the unsafety is ok because our reference was
353351
// either unique to begin with, or became one upon cloning the contents.
354352
let inner = unsafe { &mut **self._ptr };
355353
&mut inner.data
@@ -693,7 +691,7 @@ mod tests {
693691
use std::sync::atomic::Ordering::{Acquire, SeqCst};
694692
use std::thread;
695693
use std::vec::Vec;
696-
use super::{Arc, Weak, get_mut, weak_count, strong_count};
694+
use super::{Arc, Weak, weak_count, strong_count, unique};
697695
use std::sync::Mutex;
698696

699697
struct Canary(*mut atomic::AtomicUsize);
@@ -730,16 +728,18 @@ mod tests {
730728
}
731729

732730
#[test]
733-
fn test_arc_get_mut() {
734-
let mut x = Arc::new(3);
735-
*get_mut(&mut x).unwrap() = 4;
736-
assert_eq!(*x, 4);
737-
let y = x.clone();
738-
assert!(get_mut(&mut x).is_none());
739-
drop(y);
740-
assert!(get_mut(&mut x).is_some());
741-
let _w = x.downgrade();
742-
assert!(get_mut(&mut x).is_none());
731+
fn test_arc_unique() {
732+
let mut x = Arc::new(10);
733+
assert!(unique(&mut x).is_some());
734+
{
735+
let y = x.clone();
736+
assert!(unique(&mut x).is_none());
737+
}
738+
{
739+
let z = x.downgrade();
740+
assert!(unique(&mut x).is_none());
741+
}
742+
assert!(unique(&mut x).is_some());
743743
}
744744

745745
#[test]

branches/auto/src/liballoc/rc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ pub fn try_unwrap<T>(rc: Rc<T>) -> Result<T, Rc<T>> {
324324
/// ```
325325
#[inline]
326326
#[unstable(feature = "alloc")]
327-
pub fn get_mut<T>(rc: &mut Rc<T>) -> Option<&mut T> {
327+
pub fn get_mut<'a, T>(rc: &'a mut Rc<T>) -> Option<&'a mut T> {
328328
if is_unique(rc) {
329329
let inner = unsafe { &mut **rc._ptr };
330330
Some(&mut inner.value)

0 commit comments

Comments
 (0)