@@ -243,27 +243,29 @@ pub fn weak_count<T>(this: &Arc<T>) -> usize { this.inner().weak.load(SeqCst) -
243
243
pub fn strong_count < T > ( this : & Arc < T > ) -> usize { this. inner ( ) . strong . load ( SeqCst ) }
244
244
245
245
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 .
247
247
///
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.
250
249
///
251
250
/// # Examples
252
251
///
253
252
/// ```
254
253
/// # #![feature(alloc)]
255
254
/// extern crate alloc;
256
255
/// # fn main() {
257
- /// use alloc::arc;
256
+ /// use alloc::arc::{Arc, get_mut} ;
258
257
///
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);
260
261
///
261
- /// arc::unique(&mut four).map(|num| *num = 5);
262
+ /// let _y = x.clone();
263
+ /// assert!(get_mut(&mut x).is_none());
262
264
/// # }
263
265
/// ```
264
266
#[ inline]
265
267
#[ 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 > {
267
269
if strong_count ( this) == 1 && weak_count ( this) == 0 {
268
270
// This unsafety is ok because we're guaranteed that the pointer
269
271
// returned is the *only* pointer that will ever be returned to T. Our
@@ -347,7 +349,7 @@ impl<T: Clone> Arc<T> {
347
349
self . inner ( ) . weak . load ( SeqCst ) != 1 {
348
350
* self = Arc :: new ( ( * * self ) . clone ( ) )
349
351
}
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
351
353
// either unique to begin with, or became one upon cloning the contents.
352
354
let inner = unsafe { & mut * * self . _ptr } ;
353
355
& mut inner. data
@@ -691,7 +693,7 @@ mod tests {
691
693
use std:: sync:: atomic:: Ordering :: { Acquire , SeqCst } ;
692
694
use std:: thread;
693
695
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 } ;
695
697
use std:: sync:: Mutex ;
696
698
697
699
struct Canary ( * mut atomic:: AtomicUsize ) ;
@@ -728,18 +730,16 @@ mod tests {
728
730
}
729
731
730
732
#[ 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( ) ) ;
743
743
}
744
744
745
745
#[ test]
0 commit comments