@@ -136,8 +136,7 @@ impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Arc<U>> for Arc<T> {}
136
136
/// Weak pointers will not keep the data inside of the `Arc` alive, and can be
137
137
/// used to break cycles between `Arc` pointers.
138
138
#[ unsafe_no_drop_flag]
139
- #[ unstable( feature = "arc_weak" ,
140
- reason = "Weak pointers may not belong in this module." ) ]
139
+ #[ stable( feature = "arc_weak" , since = "1.4.0" ) ]
141
140
pub struct Weak < T : ?Sized > {
142
141
// FIXME #12808: strange name to try to avoid interfering with
143
142
// field accesses of the contained type via Deref
@@ -161,7 +160,7 @@ struct ArcInner<T: ?Sized> {
161
160
162
161
// the value usize::MAX acts as a sentinel for temporarily "locking" the
163
162
// ability to upgrade weak pointers or downgrade strong ones; this is used
164
- // to avoid races in `make_unique ` and `get_mut`.
163
+ // to avoid races in `make_mut ` and `get_mut`.
165
164
weak : atomic:: AtomicUsize ,
166
165
167
166
data : T ,
@@ -200,16 +199,13 @@ impl<T: ?Sized> Arc<T> {
200
199
/// # Examples
201
200
///
202
201
/// ```
203
- /// #![feature(arc_weak)]
204
- ///
205
202
/// use std::sync::Arc;
206
203
///
207
204
/// let five = Arc::new(5);
208
205
///
209
206
/// let weak_five = five.downgrade();
210
207
/// ```
211
- #[ unstable( feature = "arc_weak" ,
212
- reason = "Weak pointers may not belong in this module." ) ]
208
+ #[ stable( feature = "arc_weak" , since = "1.4.0" ) ]
213
209
pub fn downgrade ( & self ) -> Weak < T > {
214
210
loop {
215
211
// This Relaxed is OK because we're checking the value in the CAS
@@ -234,14 +230,14 @@ impl<T: ?Sized> Arc<T> {
234
230
235
231
/// Get the number of weak references to this value.
236
232
#[ inline]
237
- #[ unstable( feature = "arc_counts" ) ]
233
+ #[ unstable( feature = "arc_counts" , reason = "not clearly useful, and racy" , issue = "27718" ) ]
238
234
pub fn weak_count ( this : & Arc < T > ) -> usize {
239
235
this. inner ( ) . weak . load ( SeqCst ) - 1
240
236
}
241
237
242
238
/// Get the number of strong references to this value.
243
239
#[ inline]
244
- #[ unstable( feature = "arc_counts" ) ]
240
+ #[ unstable( feature = "arc_counts" , reason = "not clearly useful, and racy" , issue = "27718" ) ]
245
241
pub fn strong_count ( this : & Arc < T > ) -> usize {
246
242
this. inner ( ) . strong . load ( SeqCst )
247
243
}
@@ -330,27 +326,40 @@ impl<T: ?Sized> Deref for Arc<T> {
330
326
}
331
327
332
328
impl < T : Clone > Arc < T > {
333
- /// Make a mutable reference from the given `Arc<T>`.
329
+
330
+ #[ unstable( feature = "renamed_arc_unique" , reason = "renamed to make_mut" , issue = "27718" ) ]
331
+ #[ deprecated( since = "1.4.0" , reason = "renamed to make_mut" ) ]
332
+ pub fn make_unique ( this : & mut Arc < T > ) -> & mut T {
333
+ Arc :: make_mut ( this)
334
+ }
335
+
336
+ /// Make a mutable reference into the given `Arc<T>` by cloning the inner
337
+ /// data if the `Arc<T>` doesn't have one strong reference and no weak
338
+ /// references.
334
339
///
335
- /// This is also referred to as a copy-on-write operation because the inner
336
- /// data is cloned if the (strong) reference count is greater than one. If
337
- /// we hold the only strong reference, any existing weak references will no
338
- /// longer be upgradeable.
340
+ /// This is also referred to as a copy-on-write.
339
341
///
340
342
/// # Examples
341
343
///
342
344
/// ```
343
- /// #![feature(arc_unique)]
344
- ///
345
345
/// use std::sync::Arc;
346
346
///
347
- /// let mut five = Arc::new(5);
347
+ /// let mut data = Arc::new(5);
348
+ ///
349
+ /// *data.make_mut() += 1; // Won't clone anything
350
+ /// let mut other_data = data.clone(); // Won't clone inner data
351
+ /// *data.make_mut() += 1; // Clones inner data
352
+ /// *data.make_mut() += 1; // Won't clone anything
353
+ /// *other_data.make_mut() *= 2; // Won't clone anything
354
+ ///
355
+ /// // Note: data and other_data now point to different numbers
356
+ /// assert_eq!(*data, 8);
357
+ /// assert_eq!(*other_data, 12);
348
358
///
349
- /// let mut_five = Arc::make_unique(&mut five);
350
359
/// ```
351
360
#[ inline]
352
- #[ unstable ( feature = "arc_unique" ) ]
353
- pub fn make_unique ( this : & mut Arc < T > ) -> & mut T {
361
+ #[ stable ( feature = "arc_unique" , since = "1.4.0 ") ]
362
+ pub fn make_mut ( this : & mut Arc < T > ) -> & mut T {
354
363
// Note that we hold both a strong reference and a weak reference.
355
364
// Thus, releasing our strong reference only will not, by itself, cause
356
365
// the memory to be deallocated.
@@ -405,29 +414,23 @@ impl<T: Clone> Arc<T> {
405
414
}
406
415
407
416
impl < T : ?Sized > Arc < T > {
408
- /// Returns a mutable reference to the contained value if the `Arc<T>` is unique.
409
- ///
410
- /// Returns `None` if the `Arc<T>` is not unique.
417
+ /// Returns a mutable reference to the contained value if the `Arc<T>` has
418
+ /// one strong reference and no weak references.
411
419
///
412
420
/// # Examples
413
421
///
414
422
/// ```
415
- /// #![feature(arc_unique, alloc)]
416
- ///
417
- /// extern crate alloc;
418
- /// # fn main() {
419
- /// use alloc::arc::Arc;
423
+ /// use std::sync::Arc;
420
424
///
421
425
/// let mut x = Arc::new(3);
422
426
/// *Arc::get_mut(&mut x).unwrap() = 4;
423
427
/// assert_eq!(*x, 4);
424
428
///
425
429
/// let _y = x.clone();
426
430
/// assert!(Arc::get_mut(&mut x).is_none());
427
- /// # }
428
431
/// ```
429
432
#[ inline]
430
- #[ unstable ( feature = "arc_unique" ) ]
433
+ #[ stable ( feature = "arc_unique" , since = "1.4.0 ") ]
431
434
pub fn get_mut ( this : & mut Arc < T > ) -> Option < & mut T > {
432
435
if this. is_unique ( ) {
433
436
// This unsafety is ok because we're guaranteed that the pointer
@@ -540,8 +543,6 @@ impl<T: ?Sized> Drop for Arc<T> {
540
543
}
541
544
}
542
545
543
- #[ unstable( feature = "arc_weak" ,
544
- reason = "Weak pointers may not belong in this module." ) ]
545
546
impl < T : ?Sized > Weak < T > {
546
547
/// Upgrades a weak reference to a strong reference.
547
548
///
@@ -553,8 +554,6 @@ impl<T: ?Sized> Weak<T> {
553
554
/// # Examples
554
555
///
555
556
/// ```
556
- /// #![feature(arc_weak)]
557
- ///
558
557
/// use std::sync::Arc;
559
558
///
560
559
/// let five = Arc::new(5);
@@ -563,6 +562,7 @@ impl<T: ?Sized> Weak<T> {
563
562
///
564
563
/// let strong_five: Option<Arc<_>> = weak_five.upgrade();
565
564
/// ```
565
+ #[ stable( feature = "arc_weak" , since = "1.4.0" ) ]
566
566
pub fn upgrade ( & self ) -> Option < Arc < T > > {
567
567
// We use a CAS loop to increment the strong count instead of a
568
568
// fetch_add because once the count hits 0 it must never be above 0.
@@ -588,8 +588,7 @@ impl<T: ?Sized> Weak<T> {
588
588
}
589
589
}
590
590
591
- #[ unstable( feature = "arc_weak" ,
592
- reason = "Weak pointers may not belong in this module." ) ]
591
+ #[ stable( feature = "arc_weak" , since = "1.4.0" ) ]
593
592
impl < T : ?Sized > Clone for Weak < T > {
594
593
/// Makes a clone of the `Weak<T>`.
595
594
///
@@ -598,8 +597,6 @@ impl<T: ?Sized> Clone for Weak<T> {
598
597
/// # Examples
599
598
///
600
599
/// ```
601
- /// #![feature(arc_weak)]
602
- ///
603
600
/// use std::sync::Arc;
604
601
///
605
602
/// let weak_five = Arc::new(5).downgrade();
@@ -632,8 +629,6 @@ impl<T: ?Sized> Drop for Weak<T> {
632
629
/// # Examples
633
630
///
634
631
/// ```
635
- /// #![feature(arc_weak)]
636
- ///
637
632
/// use std::sync::Arc;
638
633
///
639
634
/// {
@@ -891,18 +886,18 @@ mod tests {
891
886
}
892
887
893
888
#[ test]
894
- fn test_cowarc_clone_make_unique ( ) {
889
+ fn test_cowarc_clone_make_mut ( ) {
895
890
let mut cow0 = Arc :: new ( 75 ) ;
896
891
let mut cow1 = cow0. clone ( ) ;
897
892
let mut cow2 = cow1. clone ( ) ;
898
893
899
- assert ! ( 75 == * Arc :: make_unique ( & mut cow0) ) ;
900
- assert ! ( 75 == * Arc :: make_unique ( & mut cow1) ) ;
901
- assert ! ( 75 == * Arc :: make_unique ( & mut cow2) ) ;
894
+ assert ! ( 75 == * Arc :: make_mut ( & mut cow0) ) ;
895
+ assert ! ( 75 == * Arc :: make_mut ( & mut cow1) ) ;
896
+ assert ! ( 75 == * Arc :: make_mut ( & mut cow2) ) ;
902
897
903
- * Arc :: make_unique ( & mut cow0) += 1 ;
904
- * Arc :: make_unique ( & mut cow1) += 2 ;
905
- * Arc :: make_unique ( & mut cow2) += 3 ;
898
+ * Arc :: make_mut ( & mut cow0) += 1 ;
899
+ * Arc :: make_mut ( & mut cow1) += 2 ;
900
+ * Arc :: make_mut ( & mut cow2) += 3 ;
906
901
907
902
assert ! ( 76 == * cow0) ;
908
903
assert ! ( 77 == * cow1) ;
@@ -924,8 +919,7 @@ mod tests {
924
919
assert ! ( 75 == * cow1) ;
925
920
assert ! ( 75 == * cow2) ;
926
921
927
- * Arc :: make_unique ( & mut cow0) += 1 ;
928
-
922
+ * Arc :: make_mut ( & mut cow0) += 1 ;
929
923
assert ! ( 76 == * cow0) ;
930
924
assert ! ( 75 == * cow1) ;
931
925
assert ! ( 75 == * cow2) ;
@@ -945,7 +939,7 @@ mod tests {
945
939
assert ! ( 75 == * cow0) ;
946
940
assert ! ( 75 == * cow1_weak. upgrade( ) . unwrap( ) ) ;
947
941
948
- * Arc :: make_unique ( & mut cow0) += 1 ;
942
+ * Arc :: make_mut ( & mut cow0) += 1 ;
949
943
950
944
assert ! ( 76 == * cow0) ;
951
945
assert ! ( cow1_weak. upgrade( ) . is_none( ) ) ;
0 commit comments