Skip to content

Commit 1b38463

Browse files
committed
do all the same edits with Arc
1 parent 52a31f7 commit 1b38463

File tree

1 file changed

+65
-51
lines changed

1 file changed

+65
-51
lines changed

src/liballoc/sync.rs

+65-51
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
4545
///
4646
/// The type `Arc<T>` provides shared ownership of a value of type `T`,
4747
/// 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
4949
/// 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.
5252
///
5353
/// Shared references in Rust disallow mutation by default, and `Arc` is no
5454
/// exception: you cannot generally obtain a mutable reference to something
@@ -61,7 +61,7 @@ const MAX_REFCOUNT: usize = (isize::MAX) as usize;
6161
/// Unlike [`Rc<T>`], `Arc<T>` uses atomic operations for its reference
6262
/// counting. This means that it is thread-safe. The disadvantage is that
6363
/// 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
6565
/// [`Rc<T>`] for lower overhead. [`Rc<T>`] is a safe default, because the
6666
/// compiler will catch any attempt to send an [`Rc<T>`] between threads.
6767
/// 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;
8585
///
8686
/// The [`downgrade`][downgrade] method can be used to create a non-owning
8787
/// [`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.
9092
///
9193
/// A cycle between `Arc` pointers will never be deallocated. For this reason,
9294
/// [`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;
121123
/// Arc::downgrade(&my_arc);
122124
/// ```
123125
///
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.
126128
///
127129
/// [arc]: struct.Arc.html
128130
/// [weak]: struct.Weak.html
@@ -221,17 +223,18 @@ impl<T: ?Sized> Arc<T> {
221223
}
222224

223225
/// `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`
225227
/// pointer, which returns an [`Option`]`<`[`Arc`]`<T>>`.
226228
///
227229
/// 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.
231234
///
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
235238
/// would never allow either [`Arc`] to be dropped. For example, a tree could
236239
/// have strong [`Arc`] pointers from parent nodes to children, and `Weak`
237240
/// pointers from children back to their parents.
@@ -345,7 +348,7 @@ impl<T> Arc<T> {
345348
unsafe { Pin::new_unchecked(Arc::new(data)) }
346349
}
347350

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.
349352
///
350353
/// Otherwise, an [`Err`][result] is returned with the same `Arc` that was
351354
/// passed in.
@@ -426,7 +429,7 @@ impl<T> Arc<mem::MaybeUninit<T>> {
426429
/// # Safety
427430
///
428431
/// 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
430433
/// really is in an initialized state.
431434
/// Calling this when the content is not yet fully initialized
432435
/// causes immediate undefined behavior.
@@ -465,7 +468,7 @@ impl<T> Arc<[mem::MaybeUninit<T>]> {
465468
/// # Safety
466469
///
467470
/// 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
469472
/// really is in an initialized state.
470473
/// Calling this when the content is not yet fully initialized
471474
/// causes immediate undefined behavior.
@@ -584,7 +587,7 @@ impl<T: ?Sized> Arc<T> {
584587
unsafe { NonNull::new_unchecked(Arc::into_raw(this) as *mut _) }
585588
}
586589

587-
/// Creates a new [`Weak`][weak] pointer to this value.
590+
/// Creates a new [`Weak`][weak] pointer to this allocation.
588591
///
589592
/// [weak]: struct.Weak.html
590593
///
@@ -628,7 +631,7 @@ impl<T: ?Sized> Arc<T> {
628631
}
629632
}
630633

631-
/// Gets the number of [`Weak`][weak] pointers to this value.
634+
/// Gets the number of [`Weak`][weak] pointers to this allocation.
632635
///
633636
/// [weak]: struct.Weak.html
634637
///
@@ -659,7 +662,7 @@ impl<T: ?Sized> Arc<T> {
659662
if cnt == usize::MAX { 0 } else { cnt - 1 }
660663
}
661664

662-
/// Gets the number of strong (`Arc`) pointers to this value.
665+
/// Gets the number of strong (`Arc`) pointers to this allocation.
663666
///
664667
/// # Safety
665668
///
@@ -710,8 +713,8 @@ impl<T: ?Sized> Arc<T> {
710713

711714
#[inline]
712715
#[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`]).
715718
///
716719
/// # Examples
717720
///
@@ -725,14 +728,16 @@ impl<T: ?Sized> Arc<T> {
725728
/// assert!(Arc::ptr_eq(&five, &same_five));
726729
/// assert!(!Arc::ptr_eq(&five, &other_five));
727730
/// ```
731+
///
732+
/// [`ptr::eq`]: ../../std/ptr/fn.eq.html
728733
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
729734
this.ptr.as_ptr() == other.ptr.as_ptr()
730735
}
731736
}
732737

733738
impl<T: ?Sized> Arc<T> {
734739
/// 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.
736741
///
737742
/// The function `mem_to_arcinner` is called with the data pointer
738743
/// and must return back a (potentially fat)-pointer for the `ArcInner<T>`.
@@ -761,7 +766,7 @@ impl<T: ?Sized> Arc<T> {
761766
inner
762767
}
763768

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.
765770
unsafe fn allocate_for_ptr(ptr: *const T) -> *mut ArcInner<T> {
766771
// Allocate for the `ArcInner<T>` using the given value.
767772
Self::allocate_for_layout(
@@ -903,7 +908,7 @@ impl<T: Copy> ArcFromSlice<T> for Arc<[T]> {
903908
impl<T: ?Sized> Clone for Arc<T> {
904909
/// Makes a clone of the `Arc` pointer.
905910
///
906-
/// This creates another pointer to the same inner value, increasing the
911+
/// This creates another pointer to the same allocation, increasing the
907912
/// strong reference count.
908913
///
909914
/// # Examples
@@ -965,15 +970,19 @@ impl<T: ?Sized> Receiver for Arc<T> {}
965970
impl<T: Clone> Arc<T> {
966971
/// Makes a mutable reference into the given `Arc`.
967972
///
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.
971979
///
972980
/// See also [`get_mut`][get_mut], which will fail rather than cloning.
973981
///
974982
/// [weak]: struct.Weak.html
975983
/// [clone]: ../../std/clone/trait.Clone.html#tymethod.clone
976984
/// [get_mut]: struct.Arc.html#method.get_mut
985+
/// [`Rc::make_mut`]: ../rc/struct.Rc.html#method.make_mut
977986
///
978987
/// # Examples
979988
///
@@ -988,7 +997,7 @@ impl<T: Clone> Arc<T> {
988997
/// *Arc::make_mut(&mut data) += 1; // Won't clone anything
989998
/// *Arc::make_mut(&mut other_data) *= 2; // Won't clone anything
990999
///
991-
/// // Now `data` and `other_data` point to different values.
1000+
/// // Now `data` and `other_data` point to different allocations.
9921001
/// assert_eq!(*data, 8);
9931002
/// assert_eq!(*other_data, 12);
9941003
/// ```
@@ -1048,14 +1057,14 @@ impl<T: Clone> Arc<T> {
10481057
}
10491058

10501059
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.
10531062
///
10541063
/// Returns [`None`][option] otherwise, because it is not safe to
10551064
/// mutate a shared value.
10561065
///
10571066
/// 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.
10591068
///
10601069
/// [weak]: struct.Weak.html
10611070
/// [option]: ../../std/option/enum.Option.html
@@ -1091,7 +1100,7 @@ impl<T: ?Sized> Arc<T> {
10911100
}
10921101
}
10931102

1094-
/// Returns a mutable reference to the inner value,
1103+
/// Returns a mutable reference into the given `Arc`,
10951104
/// without any check.
10961105
///
10971106
/// See also [`get_mut`], which is safe and does appropriate checks.
@@ -1100,7 +1109,7 @@ impl<T: ?Sized> Arc<T> {
11001109
///
11011110
/// # Safety
11021111
///
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
11041113
/// for the duration of the returned borrow.
11051114
/// This is trivially the case if no such pointers exist,
11061115
/// for example immediately after `Arc::new`.
@@ -1424,10 +1433,10 @@ impl<T> Weak<T> {
14241433
}
14251434

14261435
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.
14291438
///
1430-
/// Returns [`None`] if the value has since been dropped.
1439+
/// Returns [`None`] if the inner value has since been dropped.
14311440
///
14321441
/// [`Arc`]: struct.Arc.html
14331442
/// [`None`]: ../../std/option/enum.Option.html#variant.None
@@ -1482,7 +1491,7 @@ impl<T: ?Sized> Weak<T> {
14821491
}
14831492
}
14841493

1485-
/// Gets the number of strong (`Arc`) pointers pointing to this value.
1494+
/// Gets the number of strong (`Arc`) pointers pointing to this allocation.
14861495
///
14871496
/// If `self` was created using [`Weak::new`], this will return 0.
14881497
///
@@ -1497,17 +1506,17 @@ impl<T: ?Sized> Weak<T> {
14971506
}
14981507

14991508
/// Gets an approximation of the number of `Weak` pointers pointing to this
1500-
/// value.
1509+
/// allocation.
15011510
///
15021511
/// If `self` was created using [`Weak::new`], this will return 0. If not,
15031512
/// the returned value is at least 1, since `self` still points to the
1504-
/// value.
1513+
/// allocation.
15051514
///
15061515
/// # Accuracy
15071516
///
15081517
/// Due to implementation details, the returned value can be off by 1 in
15091518
/// 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.
15111520
///
15121521
/// [`Weak::new`]: #method.new
15131522
#[unstable(feature = "weak_counts", issue = "57977")]
@@ -1548,14 +1557,14 @@ impl<T: ?Sized> Weak<T> {
15481557
}
15491558
}
15501559

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
15531562
/// (because they were created with `Weak::new()`).
15541563
///
15551564
/// # Notes
15561565
///
15571566
/// 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.
15591568
///
15601569
/// # Examples
15611570
///
@@ -1587,6 +1596,8 @@ impl<T: ?Sized> Weak<T> {
15871596
/// let third = Arc::downgrade(&third_rc);
15881597
/// assert!(!first.ptr_eq(&third));
15891598
/// ```
1599+
///
1600+
/// [`ptr::eq`]: ../../std/ptr/fn.eq.html
15901601
#[inline]
15911602
#[stable(feature = "weak_ptr_eq", since = "1.39.0")]
15921603
pub fn ptr_eq(&self, other: &Self) -> bool {
@@ -1596,7 +1607,7 @@ impl<T: ?Sized> Weak<T> {
15961607

15971608
#[stable(feature = "arc_weak", since = "1.4.0")]
15981609
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.
16001611
///
16011612
/// # Examples
16021613
///
@@ -1726,6 +1737,8 @@ impl<T: ?Sized + PartialEq> ArcEqIdent<T> for Arc<T> {
17261737
/// store large values, that are slow to clone, but also heavy to check for equality, causing this
17271738
/// cost to pay off more easily. It's also more likely to have two `Arc` clones, that point to
17281739
/// the same value, than two `&T`s.
1740+
///
1741+
/// We can only do this when `T: Eq` as a `PartialEq` might be deliberately irreflexive.
17291742
#[stable(feature = "rust1", since = "1.0.0")]
17301743
impl<T: ?Sized + Eq> ArcEqIdent<T> for Arc<T> {
17311744
#[inline]
@@ -1743,10 +1756,11 @@ impl<T: ?Sized + Eq> ArcEqIdent<T> for Arc<T> {
17431756
impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
17441757
/// Equality for two `Arc`s.
17451758
///
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.
17471761
///
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.
17501764
///
17511765
/// # Examples
17521766
///
@@ -1766,8 +1780,8 @@ impl<T: ?Sized + PartialEq> PartialEq for Arc<T> {
17661780
///
17671781
/// Two `Arc`s are unequal if their inner values are unequal.
17681782
///
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.
17711785
///
17721786
/// # Examples
17731787
///

0 commit comments

Comments
 (0)