Skip to content

Commit dbfd9ef

Browse files
committed
---
yaml --- r: 196583 b: refs/heads/auto c: 50cb31f h: refs/heads/master i: 196581: 072a419 196579: b08b5f2 196575: 8913a43 v: v3
1 parent f2745b3 commit dbfd9ef

Some content is hidden

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

63 files changed

+3263
-2363
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: 92d00262c36e12ed848ca8b248cb81f16e7f2deb
13+
refs/heads/auto: 50cb31f0e767a469751c644136754129f1ac4247
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: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -457,30 +457,25 @@ 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 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:
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:
472468
473469
```{rust}
474470
use std::thread;
475-
use std::sync::{Arc,Mutex};
471+
use std::sync::Mutex;
476472
477473
fn main() {
478-
let numbers = Arc::new(Mutex::new(vec![1, 2, 3]));
474+
let numbers = &Mutex::new(vec![1, 2, 3]);
479475
480476
let guards: Vec<_> = (0..3).map(|i| {
481-
let number = numbers.clone();
482477
thread::scoped(move || {
483-
let mut array = number.lock().unwrap();
478+
let mut array = numbers.lock().unwrap();
484479
array[i] += 1;
485480
println!("numbers[{}] is {}", i, array[i]);
486481
})
@@ -489,12 +484,9 @@ fn main() {
489484
```
490485
491486
We first have to `use` the appropriate library, and then we wrap our vector in
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.
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.
498490
499491
We can compile and run this program without error, and in fact, see the
500492
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 the most complex features,
31+
chapters focus on Rust's 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 tests {
16+
mod test {
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/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 tests {
385+
mod test {
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,27 +243,29 @@ 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-
/// Try accessing a mutable reference to the contents behind an unique `Arc<T>`.
246+
/// Returns a mutable reference to the contained value if the `Arc<T>` is unique.
247247
///
248-
/// The access is granted only if this is the only reference to the object.
249-
/// Otherwise, `None` is returned.
248+
/// Returns `None` if the `Arc<T>` is not unique.
250249
///
251250
/// # Examples
252251
///
253252
/// ```
254253
/// # #![feature(alloc)]
255254
/// extern crate alloc;
256255
/// # fn main() {
257-
/// use alloc::arc;
256+
/// use alloc::arc::{Arc, get_mut};
258257
///
259-
/// let mut four = arc::Arc::new(4);
258+
/// let mut x = Arc::new(3);
259+
/// *get_mut(&mut x).unwrap() = 4;
260+
/// assert_eq!(*x, 4);
260261
///
261-
/// arc::unique(&mut four).map(|num| *num = 5);
262+
/// let _y = x.clone();
263+
/// assert!(get_mut(&mut x).is_none());
262264
/// # }
263265
/// ```
264266
#[inline]
265267
#[unstable(feature = "alloc")]
266-
pub fn unique<T>(this: &mut Arc<T>) -> Option<&mut T> {
268+
pub fn get_mut<T>(this: &mut Arc<T>) -> Option<&mut T> {
267269
if strong_count(this) == 1 && weak_count(this) == 0 {
268270
// This unsafety is ok because we're guaranteed that the pointer
269271
// returned is the *only* pointer that will ever be returned to T. Our
@@ -347,7 +349,7 @@ impl<T: Clone> Arc<T> {
347349
self.inner().weak.load(SeqCst) != 1 {
348350
*self = Arc::new((**self).clone())
349351
}
350-
// As with `unique()`, the unsafety is ok because our reference was
352+
// As with `get_mut()`, the unsafety is ok because our reference was
351353
// either unique to begin with, or became one upon cloning the contents.
352354
let inner = unsafe { &mut **self._ptr };
353355
&mut inner.data
@@ -691,7 +693,7 @@ mod tests {
691693
use std::sync::atomic::Ordering::{Acquire, SeqCst};
692694
use std::thread;
693695
use std::vec::Vec;
694-
use super::{Arc, Weak, weak_count, strong_count, unique};
696+
use super::{Arc, Weak, get_mut, weak_count, strong_count};
695697
use std::sync::Mutex;
696698

697699
struct Canary(*mut atomic::AtomicUsize);
@@ -728,18 +730,16 @@ mod tests {
728730
}
729731

730732
#[test]
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());
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());
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<'a, T>(rc: &'a mut Rc<T>) -> Option<&'a mut T> {
327+
pub fn get_mut<T>(rc: &mut Rc<T>) -> Option<&mut T> {
328328
if is_unique(rc) {
329329
let inner = unsafe { &mut **rc._ptr };
330330
Some(&mut inner.value)

0 commit comments

Comments
 (0)