From c1d716ed4b98457fe6b713085ec3598c36aadadd Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 4 Apr 2015 02:54:49 -0400 Subject: [PATCH 1/2] Renamed Arc::try_unique to get_mut --- src/liballoc/arc.rs | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index a39f125e01cd3..0a66327b5acec 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -243,27 +243,27 @@ pub fn weak_count(this: &Arc) -> usize { this.inner().weak.load(SeqCst) - pub fn strong_count(this: &Arc) -> usize { this.inner().strong.load(SeqCst) } -/// Try accessing a mutable reference to the contents behind an unique `Arc`. +/// Returns a mutable reference to the contained value if the `Arc` is unique. /// -/// The access is granted only if this is the only reference to the object. -/// Otherwise, `None` is returned. +/// Returns `None` if the `Arc` is not unique. /// /// # Examples /// /// ``` /// # #![feature(alloc)] /// extern crate alloc; -/// # fn main() { -/// use alloc::arc; +/// use alloc::arc::{Arc, get_mut}; /// -/// let mut four = arc::Arc::new(4); +/// let mut x = Arc::new(3); +/// *get_mut(&mut x).unwrap() = 4; +/// assert_eq!(*x, 4); /// -/// arc::unique(&mut four).map(|num| *num = 5); -/// # } +/// let _y = x.clone(); +/// assert!(get_mut(&mut x).is_none()); /// ``` #[inline] #[unstable(feature = "alloc")] -pub fn unique(this: &mut Arc) -> Option<&mut T> { +pub fn get_mut<'a, T>(this: &'a mut Arc) -> Option<&'a mut T> { if strong_count(this) == 1 && weak_count(this) == 0 { // This unsafety is ok because we're guaranteed that the pointer // returned is the *only* pointer that will ever be returned to T. Our @@ -347,7 +347,7 @@ impl Arc { self.inner().weak.load(SeqCst) != 1 { *self = Arc::new((**self).clone()) } - // As with `unique()`, the unsafety is ok because our reference was + // As with `get_mut()`, the unsafety is ok because our reference was // either unique to begin with, or became one upon cloning the contents. let inner = unsafe { &mut **self._ptr }; &mut inner.data @@ -691,7 +691,7 @@ mod tests { use std::sync::atomic::Ordering::{Acquire, SeqCst}; use std::thread; use std::vec::Vec; - use super::{Arc, Weak, weak_count, strong_count, unique}; + use super::{Arc, Weak, get_mut, weak_count, strong_count}; use std::sync::Mutex; struct Canary(*mut atomic::AtomicUsize); @@ -728,18 +728,16 @@ mod tests { } #[test] - fn test_arc_unique() { - let mut x = Arc::new(10); - assert!(unique(&mut x).is_some()); - { - let y = x.clone(); - assert!(unique(&mut x).is_none()); - } - { - let z = x.downgrade(); - assert!(unique(&mut x).is_none()); - } - assert!(unique(&mut x).is_some()); + fn test_arc_get_mut() { + let mut x = Arc::new(3); + *get_mut(&mut x).unwrap() = 4; + assert_eq!(*x, 4); + let y = x.clone(); + assert!(get_mut(&mut x).is_none()); + drop(y); + assert!(get_mut(&mut x).is_some()); + let _w = x.downgrade(); + assert!(get_mut(&mut x).is_none()); } #[test] From bc1aef3e7b46db3d3eee4eca80f6462f8a56bbeb Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Sat, 4 Apr 2015 18:54:23 -0400 Subject: [PATCH 2/2] Removed explicit lifetimes for `get_mut`. Fixed the doc test. --- src/liballoc/arc.rs | 4 +++- src/liballoc/rc.rs | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/liballoc/arc.rs b/src/liballoc/arc.rs index 0a66327b5acec..68bde1476118d 100644 --- a/src/liballoc/arc.rs +++ b/src/liballoc/arc.rs @@ -252,6 +252,7 @@ pub fn strong_count(this: &Arc) -> usize { this.inner().strong.load(SeqCst /// ``` /// # #![feature(alloc)] /// extern crate alloc; +/// # fn main() { /// use alloc::arc::{Arc, get_mut}; /// /// let mut x = Arc::new(3); @@ -260,10 +261,11 @@ pub fn strong_count(this: &Arc) -> usize { this.inner().strong.load(SeqCst /// /// let _y = x.clone(); /// assert!(get_mut(&mut x).is_none()); +/// # } /// ``` #[inline] #[unstable(feature = "alloc")] -pub fn get_mut<'a, T>(this: &'a mut Arc) -> Option<&'a mut T> { +pub fn get_mut(this: &mut Arc) -> Option<&mut T> { if strong_count(this) == 1 && weak_count(this) == 0 { // This unsafety is ok because we're guaranteed that the pointer // returned is the *only* pointer that will ever be returned to T. Our diff --git a/src/liballoc/rc.rs b/src/liballoc/rc.rs index 7cdd488842621..56822cfe28a35 100644 --- a/src/liballoc/rc.rs +++ b/src/liballoc/rc.rs @@ -324,7 +324,7 @@ pub fn try_unwrap(rc: Rc) -> Result> { /// ``` #[inline] #[unstable(feature = "alloc")] -pub fn get_mut<'a, T>(rc: &'a mut Rc) -> Option<&'a mut T> { +pub fn get_mut(rc: &mut Rc) -> Option<&mut T> { if is_unique(rc) { let inner = unsafe { &mut **rc._ptr }; Some(&mut inner.value)