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