@@ -45,10 +45,10 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
45
45
///
46
46
/// The type `Arc<T>` provides shared ownership of a value of type `T`,
47
47
/// allocated in the heap. Invoking [`clone`][clone] on `Arc` produces
48
- /// a new `Arc` instance, which points to the same value on the heap as the
48
+ /// a new `Arc` instance, which points to the same allocation on the heap as the
49
49
/// source `Arc`, while increasing a reference count. When the last `Arc`
50
- /// pointer to a given value is destroyed, the pointed-to value is also
51
- /// destroyed .
50
+ /// pointer to a given allocation is destroyed, the value stored in that allocation (often
51
+ /// referred to as "inner value") is also dropped .
52
52
///
53
53
/// Shared references in Rust disallow mutation by default, and `Arc` is no
54
54
/// exception: you cannot generally obtain a mutable reference to something
@@ -61,7 +61,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
61
61
/// Unlike [`Rc<T>`], `Arc<T>` uses atomic operations for its reference
62
62
/// counting. This means that it is thread-safe. The disadvantage is that
63
63
/// atomic operations are more expensive than ordinary memory accesses. If you
64
- /// are not sharing reference-counted values between threads, consider using
64
+ /// are not sharing reference-counted allocations between threads, consider using
65
65
/// [`Rc<T>`] for lower overhead. [`Rc<T>`] is a safe default, because the
66
66
/// compiler will catch any attempt to send an [`Rc<T>`] between threads.
67
67
/// However, a library might choose `Arc<T>` in order to give library consumers
@@ -85,8 +85,10 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
85
85
///
86
86
/// The [`downgrade`][downgrade] method can be used to create a non-owning
87
87
/// [`Weak`][weak] pointer. A [`Weak`][weak] pointer can be [`upgrade`][upgrade]d
88
- /// to an `Arc`, but this will return [`None`] if the value has already been
89
- /// dropped.
88
+ /// to an `Arc`, but this will return [`None`] if the value stored in the allocation has
89
+ /// already been dropped. In other words, `Weak` pointers do not keep the value
90
+ /// inside the allocation alive; however, they *do* keep the allocation
91
+ /// (the backing store for the value) alive.
90
92
///
91
93
/// A cycle between `Arc` pointers will never be deallocated. For this reason,
92
94
/// [`Weak`][weak] is used to break cycles. For example, a tree could have
@@ -121,8 +123,8 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
121
123
/// Arc::downgrade(&my_arc);
122
124
/// ```
123
125
///
124
- /// [`Weak<T>`][weak] does not auto-dereference to `T`, because the value may have
125
- /// already been destroyed .
126
+ /// [`Weak<T>`][weak] does not auto-dereference to `T`, because the inner value may have
127
+ /// already been dropped .
126
128
///
127
129
/// [arc]: struct.Arc.html
128
130
/// [weak]: struct.Weak.html
@@ -221,17 +223,18 @@ impl<T: ?Sized> Arc<T> {
221
223
}
222
224
223
225
/// `Weak` is a version of [`Arc`] that holds a non-owning reference to the
224
- /// managed value . The value is accessed by calling [`upgrade`] on the `Weak`
226
+ /// managed allocation . The allocation is accessed by calling [`upgrade`] on the `Weak`
225
227
/// pointer, which returns an [`Option`]`<`[`Arc`]`<T>>`.
226
228
///
227
229
/// Since a `Weak` reference does not count towards ownership, it will not
228
- /// prevent the inner value from being dropped, and `Weak` itself makes no
229
- /// guarantees about the value still being present and may return [`None`]
230
- /// when [`upgrade`]d.
230
+ /// prevent the value stored in the allocation from being dropped, and `Weak` itself makes no
231
+ /// guarantees about the value still being present. Thus it may return [`None`]
232
+ /// when [`upgrade`]d. Note however that a `Weak` reference *does* prevent the allocation
233
+ /// itself (the backing store) from being deallocated.
231
234
///
232
- /// A `Weak` pointer is useful for keeping a temporary reference to the value
233
- /// within [`Arc`] without extending its lifetime . It is also used to prevent
234
- /// circular references between [`Arc`] pointers, since mutual owning references
235
+ /// A `Weak` pointer is useful for keeping a temporary reference to the allocation
236
+ /// managed by [`Arc`] without preventing its inner value from being dropped . It is also used to
237
+ /// prevent circular references between [`Arc`] pointers, since mutual owning references
235
238
/// would never allow either [`Arc`] to be dropped. For example, a tree could
236
239
/// have strong [`Arc`] pointers from parent nodes to children, and `Weak`
237
240
/// pointers from children back to their parents.
@@ -345,7 +348,7 @@ impl<T> Arc<T> {
345
348
unsafe { Pin :: new_unchecked ( Arc :: new ( data) ) }
346
349
}
347
350
348
- /// Returns the contained value, if the `Arc` has exactly one strong reference.
351
+ /// Returns the inner value, if the `Arc` has exactly one strong reference.
349
352
///
350
353
/// Otherwise, an [`Err`][result] is returned with the same `Arc` that was
351
354
/// passed in.
@@ -426,7 +429,7 @@ impl<T> Arc<mem::MaybeUninit<T>> {
426
429
/// # Safety
427
430
///
428
431
/// As with [`MaybeUninit::assume_init`],
429
- /// it is up to the caller to guarantee that the value
432
+ /// it is up to the caller to guarantee that the inner value
430
433
/// really is in an initialized state.
431
434
/// Calling this when the content is not yet fully initialized
432
435
/// causes immediate undefined behavior.
@@ -465,7 +468,7 @@ impl<T> Arc<[mem::MaybeUninit<T>]> {
465
468
/// # Safety
466
469
///
467
470
/// As with [`MaybeUninit::assume_init`],
468
- /// it is up to the caller to guarantee that the value
471
+ /// it is up to the caller to guarantee that the inner value
469
472
/// really is in an initialized state.
470
473
/// Calling this when the content is not yet fully initialized
471
474
/// causes immediate undefined behavior.
@@ -584,7 +587,7 @@ impl<T: ?Sized> Arc<T> {
584
587
unsafe { NonNull :: new_unchecked ( Arc :: into_raw ( this) as * mut _ ) }
585
588
}
586
589
587
- /// Creates a new [`Weak`][weak] pointer to this value .
590
+ /// Creates a new [`Weak`][weak] pointer to this allocation .
588
591
///
589
592
/// [weak]: struct.Weak.html
590
593
///
@@ -628,7 +631,7 @@ impl<T: ?Sized> Arc<T> {
628
631
}
629
632
}
630
633
631
- /// Gets the number of [`Weak`][weak] pointers to this value .
634
+ /// Gets the number of [`Weak`][weak] pointers to this allocation .
632
635
///
633
636
/// [weak]: struct.Weak.html
634
637
///
@@ -659,7 +662,7 @@ impl<T: ?Sized> Arc<T> {
659
662
if cnt == usize:: MAX { 0 } else { cnt - 1 }
660
663
}
661
664
662
- /// Gets the number of strong (`Arc`) pointers to this value .
665
+ /// Gets the number of strong (`Arc`) pointers to this allocation .
663
666
///
664
667
/// # Safety
665
668
///
@@ -710,8 +713,8 @@ impl<T: ?Sized> Arc<T> {
710
713
711
714
#[ inline]
712
715
#[ stable( feature = "ptr_eq" , since = "1.17.0" ) ]
713
- /// Returns `true` if the two `Arc`s point to the same value (not
714
- /// just values that compare as equal ).
716
+ /// Returns `true` if the two `Arc`s point to the same allocation
717
+ /// (in a vein similar to [`ptr::eq`] ).
715
718
///
716
719
/// # Examples
717
720
///
@@ -725,14 +728,16 @@ impl<T: ?Sized> Arc<T> {
725
728
/// assert!(Arc::ptr_eq(&five, &same_five));
726
729
/// assert!(!Arc::ptr_eq(&five, &other_five));
727
730
/// ```
731
+ ///
732
+ /// [`ptr::eq`]: ../../std/ptr/fn.eq.html
728
733
pub fn ptr_eq ( this : & Self , other : & Self ) -> bool {
729
734
this. ptr . as_ptr ( ) == other. ptr . as_ptr ( )
730
735
}
731
736
}
732
737
733
738
impl < T : ?Sized > Arc < T > {
734
739
/// Allocates an `ArcInner<T>` with sufficient space for
735
- /// a possibly-unsized value where the value has the layout provided.
740
+ /// a possibly-unsized inner value where the value has the layout provided.
736
741
///
737
742
/// The function `mem_to_arcinner` is called with the data pointer
738
743
/// and must return back a (potentially fat)-pointer for the `ArcInner<T>`.
@@ -761,7 +766,7 @@ impl<T: ?Sized> Arc<T> {
761
766
inner
762
767
}
763
768
764
- /// Allocates an `ArcInner<T>` with sufficient space for an unsized value.
769
+ /// Allocates an `ArcInner<T>` with sufficient space for an unsized inner value.
765
770
unsafe fn allocate_for_ptr ( ptr : * const T ) -> * mut ArcInner < T > {
766
771
// Allocate for the `ArcInner<T>` using the given value.
767
772
Self :: allocate_for_layout (
@@ -903,7 +908,7 @@ impl<T: Copy> ArcFromSlice<T> for Arc<[T]> {
903
908
impl < T : ?Sized > Clone for Arc < T > {
904
909
/// Makes a clone of the `Arc` pointer.
905
910
///
906
- /// This creates another pointer to the same inner value , increasing the
911
+ /// This creates another pointer to the same allocation , increasing the
907
912
/// strong reference count.
908
913
///
909
914
/// # Examples
@@ -965,15 +970,19 @@ impl<T: ?Sized> Receiver for Arc<T> {}
965
970
impl < T : Clone > Arc < T > {
966
971
/// Makes a mutable reference into the given `Arc`.
967
972
///
968
- /// If there are other `Arc` or [`Weak`][weak] pointers to the same value,
969
- /// then `make_mut` will invoke [`clone`][clone] on the inner value to
970
- /// ensure unique ownership. This is also referred to as clone-on-write.
973
+ /// If there are other `Arc` or [`Weak`][weak] pointers to the same allocation,
974
+ /// then `make_mut` will create a new allocation and invoke [`clone`][clone] on the inner value
975
+ /// to ensure unique ownership. This is also referred to as clone-on-write.
976
+ ///
977
+ /// Note that this differs from the behavior of [`Rc::make_mut`] which disassociates
978
+ /// any remaining `Weak` pointers.
971
979
///
972
980
/// See also [`get_mut`][get_mut], which will fail rather than cloning.
973
981
///
974
982
/// [weak]: struct.Weak.html
975
983
/// [clone]: ../../std/clone/trait.Clone.html#tymethod.clone
976
984
/// [get_mut]: struct.Arc.html#method.get_mut
985
+ /// [`Rc::make_mut`]: ../rc/struct.Rc.html#method.make_mut
977
986
///
978
987
/// # Examples
979
988
///
@@ -988,7 +997,7 @@ impl<T: Clone> Arc<T> {
988
997
/// *Arc::make_mut(&mut data) += 1; // Won't clone anything
989
998
/// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything
990
999
///
991
- /// // Now `data` and `other_data` point to different values .
1000
+ /// // Now `data` and `other_data` point to different allocations .
992
1001
/// assert_eq!(*data, 8);
993
1002
/// assert_eq!(*other_data, 12);
994
1003
/// ```
@@ -1048,14 +1057,14 @@ impl<T: Clone> Arc<T> {
1048
1057
}
1049
1058
1050
1059
impl < T : ?Sized > Arc < T > {
1051
- /// Returns a mutable reference to the inner value , if there are
1052
- /// no other `Arc` or [`Weak`][weak] pointers to the same value .
1060
+ /// Returns a mutable reference into the given `Arc` , if there are
1061
+ /// no other `Arc` or [`Weak`][weak] pointers to the same allocation .
1053
1062
///
1054
1063
/// Returns [`None`][option] otherwise, because it is not safe to
1055
1064
/// mutate a shared value.
1056
1065
///
1057
1066
/// See also [`make_mut`][make_mut], which will [`clone`][clone]
1058
- /// the inner value when it's shared .
1067
+ /// the inner value when there are other pointers .
1059
1068
///
1060
1069
/// [weak]: struct.Weak.html
1061
1070
/// [option]: ../../std/option/enum.Option.html
@@ -1091,7 +1100,7 @@ impl<T: ?Sized> Arc<T> {
1091
1100
}
1092
1101
}
1093
1102
1094
- /// Returns a mutable reference to the inner value ,
1103
+ /// Returns a mutable reference into the given `Arc` ,
1095
1104
/// without any check.
1096
1105
///
1097
1106
/// See also [`get_mut`], which is safe and does appropriate checks.
@@ -1100,7 +1109,7 @@ impl<T: ?Sized> Arc<T> {
1100
1109
///
1101
1110
/// # Safety
1102
1111
///
1103
- /// Any other `Arc` or [`Weak`] pointers to the same value must not be dereferenced
1112
+ /// Any other `Arc` or [`Weak`] pointers to the same allocation must not be dereferenced
1104
1113
/// for the duration of the returned borrow.
1105
1114
/// This is trivially the case if no such pointers exist,
1106
1115
/// for example immediately after `Arc::new`.
@@ -1424,10 +1433,10 @@ impl<T> Weak<T> {
1424
1433
}
1425
1434
1426
1435
impl < T : ?Sized > Weak < T > {
1427
- /// Attempts to upgrade the `Weak` pointer to an [`Arc`], extending
1428
- /// the lifetime of the value if successful.
1436
+ /// Attempts to upgrade the `Weak` pointer to an [`Arc`], delaying
1437
+ /// dropping of the inner value if successful.
1429
1438
///
1430
- /// Returns [`None`] if the value has since been dropped.
1439
+ /// Returns [`None`] if the inner value has since been dropped.
1431
1440
///
1432
1441
/// [`Arc`]: struct.Arc.html
1433
1442
/// [`None`]: ../../std/option/enum.Option.html#variant.None
@@ -1482,7 +1491,7 @@ impl<T: ?Sized> Weak<T> {
1482
1491
}
1483
1492
}
1484
1493
1485
- /// Gets the number of strong (`Arc`) pointers pointing to this value .
1494
+ /// Gets the number of strong (`Arc`) pointers pointing to this allocation .
1486
1495
///
1487
1496
/// If `self` was created using [`Weak::new`], this will return 0.
1488
1497
///
@@ -1497,17 +1506,17 @@ impl<T: ?Sized> Weak<T> {
1497
1506
}
1498
1507
1499
1508
/// Gets an approximation of the number of `Weak` pointers pointing to this
1500
- /// value .
1509
+ /// allocation .
1501
1510
///
1502
1511
/// If `self` was created using [`Weak::new`], this will return 0. If not,
1503
1512
/// the returned value is at least 1, since `self` still points to the
1504
- /// value .
1513
+ /// allocation .
1505
1514
///
1506
1515
/// # Accuracy
1507
1516
///
1508
1517
/// Due to implementation details, the returned value can be off by 1 in
1509
1518
/// either direction when other threads are manipulating any `Arc`s or
1510
- /// `Weak`s pointing to the same value .
1519
+ /// `Weak`s pointing to the same allocation .
1511
1520
///
1512
1521
/// [`Weak::new`]: #method.new
1513
1522
#[ unstable( feature = "weak_counts" , issue = "57977" ) ]
@@ -1548,14 +1557,14 @@ impl<T: ?Sized> Weak<T> {
1548
1557
}
1549
1558
}
1550
1559
1551
- /// Returns `true` if the two `Weak`s point to the same value (not just
1552
- /// values that compare as equal ), or if both don't point to any value
1560
+ /// Returns `true` if the two `Weak`s point to the same allocation (similar to
1561
+ /// [`ptr::eq`] ), or if both don't point to any allocation
1553
1562
/// (because they were created with `Weak::new()`).
1554
1563
///
1555
1564
/// # Notes
1556
1565
///
1557
1566
/// Since this compares pointers it means that `Weak::new()` will equal each
1558
- /// other, even though they don't point to any value .
1567
+ /// other, even though they don't point to any allocation .
1559
1568
///
1560
1569
/// # Examples
1561
1570
///
@@ -1587,6 +1596,8 @@ impl<T: ?Sized> Weak<T> {
1587
1596
/// let third = Arc::downgrade(&third_rc);
1588
1597
/// assert!(!first.ptr_eq(&third));
1589
1598
/// ```
1599
+ ///
1600
+ /// [`ptr::eq`]: ../../std/ptr/fn.eq.html
1590
1601
#[ inline]
1591
1602
#[ stable( feature = "weak_ptr_eq" , since = "1.39.0" ) ]
1592
1603
pub fn ptr_eq ( & self , other : & Self ) -> bool {
@@ -1596,7 +1607,7 @@ impl<T: ?Sized> Weak<T> {
1596
1607
1597
1608
#[ stable( feature = "arc_weak" , since = "1.4.0" ) ]
1598
1609
impl < T : ?Sized > Clone for Weak < T > {
1599
- /// Makes a clone of the `Weak` pointer that points to the same value .
1610
+ /// Makes a clone of the `Weak` pointer that points to the same allocation .
1600
1611
///
1601
1612
/// # Examples
1602
1613
///
@@ -1726,6 +1737,8 @@ impl<T: ?Sized + PartialEq> ArcEqIdent<T> for Arc<T> {
1726
1737
/// store large values, that are slow to clone, but also heavy to check for equality, causing this
1727
1738
/// cost to pay off more easily. It's also more likely to have two `Arc` clones, that point to
1728
1739
/// the same value, than two `&T`s.
1740
+ ///
1741
+ /// We can only do this when `T: Eq` as a `PartialEq` might be deliberately irreflexive.
1729
1742
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1730
1743
impl < T : ?Sized + Eq > ArcEqIdent < T > for Arc < T > {
1731
1744
#[ inline]
@@ -1743,10 +1756,11 @@ impl<T: ?Sized + Eq> ArcEqIdent<T> for Arc<T> {
1743
1756
impl < T : ?Sized + PartialEq > PartialEq for Arc < T > {
1744
1757
/// Equality for two `Arc`s.
1745
1758
///
1746
- /// Two `Arc`s are equal if their inner values are equal.
1759
+ /// Two `Arc`s are equal if their inner values are equal, even if they are
1760
+ /// stored in different allocation.
1747
1761
///
1748
- /// If `T` also implements `Eq`, two `Arc`s that point to the same value are
1749
- /// always equal.
1762
+ /// If `T` also implements `Eq` (implying reflexivity of equality),
1763
+ /// two `Arc`s that point to the same allocation are always equal.
1750
1764
///
1751
1765
/// # Examples
1752
1766
///
@@ -1766,8 +1780,8 @@ impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
1766
1780
///
1767
1781
/// Two `Arc`s are unequal if their inner values are unequal.
1768
1782
///
1769
- /// If `T` also implements `Eq`, two `Arc`s that point to the same value are
1770
- /// never unequal.
1783
+ /// If `T` also implements `Eq` (implying reflexivity of equality),
1784
+ /// two `Arc`s that point to the same value are never unequal.
1771
1785
///
1772
1786
/// # Examples
1773
1787
///
0 commit comments