Skip to content

Commit de6742b

Browse files
committed
Auto merge of #24053 - kvark:get_mut, r=alexcrichton
As requested by @kballard in #23844
2 parents 529de5f + bc1aef3 commit de6742b

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/liballoc/arc.rs

+21-21
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]

src/liballoc/rc.rs

+1-1
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)