Skip to content

Commit 51d598e

Browse files
committed
Adjust documentation of Arc::make_mut
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.
1 parent 4968a8b commit 51d598e

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

library/alloc/src/rc.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1115,11 +1115,11 @@ impl<T: Clone> Rc<T> {
11151115
///
11161116
/// let mut data = Rc::new(5);
11171117
///
1118-
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
1119-
/// let mut other_data = Rc::clone(&data); // Won't clone inner data
1120-
/// *Rc::make_mut(&mut data) += 1; // Clones inner data
1121-
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
1122-
/// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
1118+
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
1119+
/// let mut other_data = Rc::clone(&data); // Won't clone inner data
1120+
/// *Rc::make_mut(&mut data) += 1; // Clones inner data
1121+
/// *Rc::make_mut(&mut data) += 1; // Won't clone anything
1122+
/// *Rc::make_mut(&mut other_data) *= 2; // Won't clone anything
11231123
///
11241124
/// // Now `data` and `other_data` point to different allocations.
11251125
/// assert_eq!(*data, 8);

library/alloc/src/sync.rs

+25-9
Original file line numberDiff line numberDiff line change
@@ -1346,18 +1346,17 @@ impl<T: ?Sized> Receiver for Arc<T> {}
13461346
impl<T: Clone> Arc<T> {
13471347
/// Makes a mutable reference into the given `Arc`.
13481348
///
1349-
/// If there are other `Arc` or [`Weak`] pointers to the same allocation,
1350-
/// then `make_mut` will create a new allocation and invoke [`clone`][clone] on the inner value
1351-
/// to ensure unique ownership. This is also referred to as clone-on-write.
1349+
/// If there are other `Arc` pointers to the same allocation, then `make_mut` will
1350+
/// [`clone`] the inner value to a new allocation to ensure unique ownership. This is also
1351+
/// referred to as clone-on-write.
13521352
///
1353-
/// Note that this differs from the behavior of [`Rc::make_mut`] which disassociates
1354-
/// any remaining `Weak` pointers.
1353+
/// If there are no other `Arc` pointers to this allocation, then [`Weak`]
1354+
/// pointers to this allocation will be disassociated.
13551355
///
1356-
/// See also [`get_mut`][get_mut], which will fail rather than cloning.
1356+
/// See also [`get_mut`], which will fail rather than cloning.
13571357
///
1358-
/// [clone]: Clone::clone
1359-
/// [get_mut]: Arc::get_mut
1360-
/// [`Rc::make_mut`]: super::rc::Rc::make_mut
1358+
/// [`clone`]: Clone::clone
1359+
/// [`get_mut`]: Arc::get_mut
13611360
///
13621361
/// # Examples
13631362
///
@@ -1376,6 +1375,23 @@ impl<T: Clone> Arc<T> {
13761375
/// assert_eq!(*data, 8);
13771376
/// assert_eq!(*other_data, 12);
13781377
/// ```
1378+
///
1379+
/// [`Weak`] pointers will be disassociated:
1380+
///
1381+
/// ```
1382+
/// use std::sync::Arc;
1383+
///
1384+
/// let mut data = Arc::new(75);
1385+
/// let weak = Arc::downgrade(&data);
1386+
///
1387+
/// assert!(75 == *data);
1388+
/// assert!(75 == *weak.upgrade().unwrap());
1389+
///
1390+
/// *Arc::make_mut(&mut data) += 1;
1391+
///
1392+
/// assert!(76 == *data);
1393+
/// assert!(weak.upgrade().is_none());
1394+
/// ```
13791395
#[cfg(not(no_global_oom_handling))]
13801396
#[inline]
13811397
#[stable(feature = "arc_unique", since = "1.4.0")]

0 commit comments

Comments
 (0)