Skip to content

Commit c1d716e

Browse files
committed
Renamed Arc::try_unique to get_mut
1 parent b8a0273 commit c1d716e

File tree

1 file changed

+21
-23
lines changed

1 file changed

+21
-23
lines changed

src/liballoc/arc.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -243,27 +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-
/// 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;
256-
/// # fn main() {
257-
/// use alloc::arc;
255+
/// use alloc::arc::{Arc, get_mut};
258256
///
259-
/// let mut four = arc::Arc::new(4);
257+
/// let mut x = Arc::new(3);
258+
/// *get_mut(&mut x).unwrap() = 4;
259+
/// assert_eq!(*x, 4);
260260
///
261-
/// arc::unique(&mut four).map(|num| *num = 5);
262-
/// # }
261+
/// let _y = x.clone();
262+
/// assert!(get_mut(&mut x).is_none());
263263
/// ```
264264
#[inline]
265265
#[unstable(feature = "alloc")]
266-
pub fn unique<T>(this: &mut Arc<T>) -> Option<&mut T> {
266+
pub fn get_mut<'a, T>(this: &'a mut Arc<T>) -> Option<&'a mut T> {
267267
if strong_count(this) == 1 && weak_count(this) == 0 {
268268
// This unsafety is ok because we're guaranteed that the pointer
269269
// returned is the *only* pointer that will ever be returned to T. Our
@@ -347,7 +347,7 @@ impl<T: Clone> Arc<T> {
347347
self.inner().weak.load(SeqCst) != 1 {
348348
*self = Arc::new((**self).clone())
349349
}
350-
// As with `unique()`, the unsafety is ok because our reference was
350+
// As with `get_mut()`, the unsafety is ok because our reference was
351351
// either unique to begin with, or became one upon cloning the contents.
352352
let inner = unsafe { &mut **self._ptr };
353353
&mut inner.data
@@ -691,7 +691,7 @@ mod tests {
691691
use std::sync::atomic::Ordering::{Acquire, SeqCst};
692692
use std::thread;
693693
use std::vec::Vec;
694-
use super::{Arc, Weak, weak_count, strong_count, unique};
694+
use super::{Arc, Weak, get_mut, weak_count, strong_count};
695695
use std::sync::Mutex;
696696

697697
struct Canary(*mut atomic::AtomicUsize);
@@ -728,18 +728,16 @@ mod tests {
728728
}
729729

730730
#[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());
731+
fn test_arc_get_mut() {
732+
let mut x = Arc::new(3);
733+
*get_mut(&mut x).unwrap() = 4;
734+
assert_eq!(*x, 4);
735+
let y = x.clone();
736+
assert!(get_mut(&mut x).is_none());
737+
drop(y);
738+
assert!(get_mut(&mut x).is_some());
739+
let _w = x.downgrade();
740+
assert!(get_mut(&mut x).is_none());
743741
}
744742

745743
#[test]

0 commit comments

Comments
 (0)