Skip to content

Commit

Permalink
Adjust documentation of Arc::make_mut
Browse files Browse the repository at this point in the history
Related discussion in the users forum:
https://users.rust-lang.org/t/what-s-this-alleged-difference-between-arc-make-mut-and-rc-make-mut/63747?u=steffahn

Also includes small formatting improvement in the documentation of `Rc::make_mut`.

This commit makes the two documentations in question complete analogs. The previously claimed point in which
one "differs from the behavior of" the other turns out to be incorrect, AFAIK.

One remaining inaccuracy: `Weak` pointers aren't disassociated from the allocation but only from the contained
value, i.e. in case of outstanding `Weak` pointers there still is a new allocation created, just the
call to `.clone()` is avoided, instead the value is moved from one allocation to the other.
  • Loading branch information
steffahn committed Aug 19, 2021
1 parent 4968a8b commit 51d598e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
10 changes: 5 additions & 5 deletions library/alloc/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1115,11 +1115,11 @@ impl<T: Clone> Rc<T> {
///
/// let mut data = Rc::new(5);
///
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
/// let mut other_data = Rc::clone(&data); // Won't clone inner data
/// *Rc::make_mut(&mut data) += 1; // Clones inner data
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
/// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
/// let mut other_data = Rc::clone(&data); // Won't clone inner data
/// *Rc::make_mut(&mut data) += 1; // Clones inner data
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
/// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
///
/// // Now `data` and `other_data` point to different allocations.
/// assert_eq!(*data, 8);
Expand Down
34 changes: 25 additions & 9 deletions library/alloc/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1346,18 +1346,17 @@ impl<T: ?Sized> Receiver for Arc<T> {}
impl<T: Clone> Arc<T> {
/// Makes a mutable reference into the given `Arc`.
///
/// If there are other `Arc` or [`Weak`] pointers to the same allocation,
/// then `make_mut` will create a new allocation and invoke [`clone`][clone] on the inner value
/// to ensure unique ownership. This is also referred to as clone-on-write.
/// If there are other `Arc` pointers to the same allocation, then `make_mut` will
/// [`clone`] the inner value to a new allocation to ensure unique ownership. This is also
/// referred to as clone-on-write.
///
/// Note that this differs from the behavior of [`Rc::make_mut`] which disassociates
/// any remaining `Weak` pointers.
/// If there are no other `Arc` pointers to this allocation, then [`Weak`]
/// pointers to this allocation will be disassociated.
///
/// See also [`get_mut`][get_mut], which will fail rather than cloning.
/// See also [`get_mut`], which will fail rather than cloning.
///
/// [clone]: Clone::clone
/// [get_mut]: Arc::get_mut
/// [`Rc::make_mut`]: super::rc::Rc::make_mut
/// [`clone`]: Clone::clone
/// [`get_mut`]: Arc::get_mut
///
/// # Examples
///
Expand All @@ -1376,6 +1375,23 @@ impl<T: Clone> Arc<T> {
/// assert_eq!(*data, 8);
/// assert_eq!(*other_data, 12);
/// ```
///
/// [`Weak`] pointers will be disassociated:
///
/// ```
/// use std::sync::Arc;
///
/// let mut data = Arc::new(75);
/// let weak = Arc::downgrade(&data);
///
/// assert!(75 == *data);
/// assert!(75 == *weak.upgrade().unwrap());
///
/// *Arc::make_mut(&mut data) += 1;
///
/// assert!(76 == *data);
/// assert!(weak.upgrade().is_none());
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
#[stable(feature = "arc_unique", since = "1.4.0")]
Expand Down

0 comments on commit 51d598e

Please sign in to comment.