@@ -250,9 +250,6 @@ pub fn strong_count<T: ?Sized>(this: &Arc<T>) -> usize { this.inner().strong.loa
250
250
///
251
251
/// Returns `None` if the `Arc<T>` is not unique.
252
252
///
253
- /// This function is marked **unsafe** because it is racy if weak pointers
254
- /// are active.
255
- ///
256
253
/// # Examples
257
254
///
258
255
/// ```
@@ -261,27 +258,24 @@ pub fn strong_count<T: ?Sized>(this: &Arc<T>) -> usize { this.inner().strong.loa
261
258
/// # fn main() {
262
259
/// use alloc::arc::{Arc, get_mut};
263
260
///
264
- /// # unsafe {
265
261
/// let mut x = Arc::new(3);
266
262
/// *get_mut(&mut x).unwrap() = 4;
267
263
/// assert_eq!(*x, 4);
268
264
///
269
265
/// let _y = x.clone();
270
266
/// assert!(get_mut(&mut x).is_none());
271
267
/// # }
272
- /// # }
273
268
/// ```
274
269
#[ inline]
275
270
#[ unstable( feature = "alloc" ) ]
276
- pub unsafe fn get_mut < T : ?Sized > ( this : & mut Arc < T > ) -> Option < & mut T > {
277
- // FIXME(#24880) potential race with upgraded weak pointers here
271
+ pub fn get_mut < T : ?Sized > ( this : & mut Arc < T > ) -> Option < & mut T > {
278
272
if strong_count ( this) == 1 && weak_count ( this) == 0 {
279
273
// This unsafety is ok because we're guaranteed that the pointer
280
274
// returned is the *only* pointer that will ever be returned to T. Our
281
275
// reference count is guaranteed to be 1 at this point, and we required
282
276
// the Arc itself to be `mut`, so we're returning the only possible
283
277
// reference to the inner data.
284
- let inner = & mut * * this. _ptr ;
278
+ let inner = unsafe { & mut * * this. _ptr } ;
285
279
Some ( & mut inner. data )
286
280
} else {
287
281
None
@@ -338,26 +332,19 @@ impl<T: Clone> Arc<T> {
338
332
/// This is also referred to as a copy-on-write operation because the inner
339
333
/// data is cloned if the reference count is greater than one.
340
334
///
341
- /// This method is marked **unsafe** because it is racy if weak pointers
342
- /// are active.
343
- ///
344
335
/// # Examples
345
336
///
346
337
/// ```
347
338
/// # #![feature(alloc)]
348
339
/// use std::sync::Arc;
349
340
///
350
- /// # unsafe {
351
341
/// let mut five = Arc::new(5);
352
342
///
353
343
/// let mut_five = five.make_unique();
354
- /// # }
355
344
/// ```
356
345
#[ inline]
357
346
#[ unstable( feature = "alloc" ) ]
358
- pub unsafe fn make_unique ( & mut self ) -> & mut T {
359
- // FIXME(#24880) potential race with upgraded weak pointers here
360
- //
347
+ pub fn make_unique ( & mut self ) -> & mut T {
361
348
// Note that we hold a strong reference, which also counts as a weak
362
349
// reference, so we only clone if there is an additional reference of
363
350
// either kind.
@@ -367,7 +354,7 @@ impl<T: Clone> Arc<T> {
367
354
}
368
355
// As with `get_mut()`, the unsafety is ok because our reference was
369
356
// either unique to begin with, or became one upon cloning the contents.
370
- let inner = & mut * * self . _ptr ;
357
+ let inner = unsafe { & mut * * self . _ptr } ;
371
358
& mut inner. data
372
359
}
373
360
}
@@ -757,43 +744,39 @@ mod tests {
757
744
758
745
#[ test]
759
746
fn test_arc_get_mut ( ) {
760
- unsafe {
761
- let mut x = Arc :: new ( 3 ) ;
762
- * get_mut ( & mut x) . unwrap ( ) = 4 ;
763
- assert_eq ! ( * x, 4 ) ;
764
- let y = x. clone ( ) ;
765
- assert ! ( get_mut( & mut x) . is_none( ) ) ;
766
- drop ( y) ;
767
- assert ! ( get_mut( & mut x) . is_some( ) ) ;
768
- let _w = x. downgrade ( ) ;
769
- assert ! ( get_mut( & mut x) . is_none( ) ) ;
770
- }
747
+ let mut x = Arc :: new ( 3 ) ;
748
+ * get_mut ( & mut x) . unwrap ( ) = 4 ;
749
+ assert_eq ! ( * x, 4 ) ;
750
+ let y = x. clone ( ) ;
751
+ assert ! ( get_mut( & mut x) . is_none( ) ) ;
752
+ drop ( y) ;
753
+ assert ! ( get_mut( & mut x) . is_some( ) ) ;
754
+ let _w = x. downgrade ( ) ;
755
+ assert ! ( get_mut( & mut x) . is_none( ) ) ;
771
756
}
772
757
773
758
#[ test]
774
759
fn test_cowarc_clone_make_unique ( ) {
775
- unsafe {
776
- let mut cow0 = Arc :: new ( 75 ) ;
777
- let mut cow1 = cow0. clone ( ) ;
778
- let mut cow2 = cow1. clone ( ) ;
779
-
780
- assert ! ( 75 == * cow0. make_unique( ) ) ;
781
- assert ! ( 75 == * cow1. make_unique( ) ) ;
782
- assert ! ( 75 == * cow2. make_unique( ) ) ;
783
-
784
- * cow0. make_unique ( ) += 1 ;
785
- * cow1. make_unique ( ) += 2 ;
786
- * cow2. make_unique ( ) += 3 ;
787
-
788
- assert ! ( 76 == * cow0) ;
789
- assert ! ( 77 == * cow1) ;
790
- assert ! ( 78 == * cow2) ;
791
-
792
- // none should point to the same backing memory
793
- assert ! ( * cow0 != * cow1) ;
794
- assert ! ( * cow0 != * cow2) ;
795
- assert ! ( * cow1 != * cow2) ;
796
- }
760
+ let mut cow0 = Arc :: new ( 75 ) ;
761
+ let mut cow1 = cow0. clone ( ) ;
762
+ let mut cow2 = cow1. clone ( ) ;
763
+
764
+ assert ! ( 75 == * cow0. make_unique( ) ) ;
765
+ assert ! ( 75 == * cow1. make_unique( ) ) ;
766
+ assert ! ( 75 == * cow2. make_unique( ) ) ;
767
+
768
+ * cow0. make_unique ( ) += 1 ;
769
+ * cow1. make_unique ( ) += 2 ;
770
+ * cow2. make_unique ( ) += 3 ;
771
+
772
+ assert ! ( 76 == * cow0) ;
773
+ assert ! ( 77 == * cow1) ;
774
+ assert ! ( 78 == * cow2) ;
775
+
776
+ // none should point to the same backing memory
777
+ assert ! ( * cow0 != * cow1) ;
778
+ assert ! ( * cow0 != * cow2) ;
779
+ assert ! ( * cow1 != * cow2) ;
797
780
}
798
781
799
782
#[ test]
@@ -806,9 +789,7 @@ mod tests {
806
789
assert ! ( 75 == * cow1) ;
807
790
assert ! ( 75 == * cow2) ;
808
791
809
- unsafe {
810
- * cow0. make_unique ( ) += 1 ;
811
- }
792
+ * cow0. make_unique ( ) += 1 ;
812
793
813
794
assert ! ( 76 == * cow0) ;
814
795
assert ! ( 75 == * cow1) ;
@@ -829,9 +810,7 @@ mod tests {
829
810
assert ! ( 75 == * cow0) ;
830
811
assert ! ( 75 == * cow1_weak. upgrade( ) . unwrap( ) ) ;
831
812
832
- unsafe {
833
- * cow0. make_unique ( ) += 1 ;
834
- }
813
+ * cow0. make_unique ( ) += 1 ;
835
814
836
815
assert ! ( 76 == * cow0) ;
837
816
assert ! ( cow1_weak. upgrade( ) . is_none( ) ) ;
0 commit comments