Skip to content

Commit 4637968

Browse files
authored
Rollup merge of #74782 - vorner:weak-into-raw-cnt-doc, r=dtolnay
Don't use "weak count" around Weak::from_raw_ptr As `Rc/Arc::weak_count` returns 0 when having no strong counts, this could be confusing and it's better to avoid using that completely. Closes #73840.
2 parents 7e86c8e + ad6d63e commit 4637968

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

library/alloc/src/rc.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1692,8 +1692,9 @@ impl<T> Weak<T> {
16921692

16931693
/// Consumes the `Weak<T>` and turns it into a raw pointer.
16941694
///
1695-
/// This converts the weak pointer into a raw pointer, preserving the original weak count. It
1696-
/// can be turned back into the `Weak<T>` with [`from_raw`].
1695+
/// This converts the weak pointer into a raw pointer, while still preserving the ownership of
1696+
/// one weak reference (the weak count is not modified by this operation). It can be turned
1697+
/// back into the `Weak<T>` with [`from_raw`].
16971698
///
16981699
/// The same restrictions of accessing the target of the pointer as with
16991700
/// [`as_ptr`] apply.
@@ -1728,17 +1729,18 @@ impl<T> Weak<T> {
17281729
/// This can be used to safely get a strong reference (by calling [`upgrade`]
17291730
/// later) or to deallocate the weak count by dropping the `Weak<T>`.
17301731
///
1731-
/// It takes ownership of one weak count (with the exception of pointers created by [`new`],
1732-
/// as these don't have any corresponding weak count).
1732+
/// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
1733+
/// as these don't own anything; the method still works on them).
17331734
///
17341735
/// # Safety
17351736
///
1736-
/// The pointer must have originated from the [`into_raw`] and must still own its potential
1737-
/// weak reference count.
1737+
/// The pointer must have originated from the [`into_raw`] and must still own its potential
1738+
/// weak reference.
17381739
///
1739-
/// It is allowed for the strong count to be 0 at the time of calling this, but the weak count
1740-
/// must be non-zero or the pointer must have originated from a dangling `Weak<T>` (one created
1741-
/// by [`new`]).
1740+
/// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
1741+
/// takes ownership of one weak reference currently represented as a raw pointer (the weak
1742+
/// count is not modified by this operation) and therefore it must be paired with a previous
1743+
/// call to [`into_raw`].
17421744
///
17431745
/// # Examples
17441746
///

library/alloc/src/sync.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1462,8 +1462,9 @@ impl<T> Weak<T> {
14621462

14631463
/// Consumes the `Weak<T>` and turns it into a raw pointer.
14641464
///
1465-
/// This converts the weak pointer into a raw pointer, preserving the original weak count. It
1466-
/// can be turned back into the `Weak<T>` with [`from_raw`].
1465+
/// This converts the weak pointer into a raw pointer, while still preserving the ownership of
1466+
/// one weak reference (the weak count is not modified by this operation). It can be turned
1467+
/// back into the `Weak<T>` with [`from_raw`].
14671468
///
14681469
/// The same restrictions of accessing the target of the pointer as with
14691470
/// [`as_ptr`] apply.
@@ -1493,24 +1494,23 @@ impl<T> Weak<T> {
14931494
result
14941495
}
14951496

1496-
/// Converts a raw pointer previously created by [`into_raw`] back into
1497-
/// `Weak<T>`.
1497+
/// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
14981498
///
14991499
/// This can be used to safely get a strong reference (by calling [`upgrade`]
15001500
/// later) or to deallocate the weak count by dropping the `Weak<T>`.
15011501
///
1502-
/// It takes ownership of one weak count (with the exception of pointers created by [`new`],
1503-
/// as these don't have any corresponding weak count).
1502+
/// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
1503+
/// as these don't own anything; the method still works on them).
15041504
///
15051505
/// # Safety
15061506
///
15071507
/// The pointer must have originated from the [`into_raw`] and must still own its potential
1508-
/// weak reference count.
1509-
///
1510-
/// It is allowed for the strong count to be 0 at the time of calling this, but the weak count
1511-
/// must be non-zero or the pointer must have originated from a dangling `Weak<T>` (one created
1512-
/// by [`new`]).
1508+
/// weak reference.
15131509
///
1510+
/// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
1511+
/// takes ownership of one weak reference currently represented as a raw pointer (the weak
1512+
/// count is not modified by this operation) and therefore it must be paired with a previous
1513+
/// call to [`into_raw`].
15141514
/// # Examples
15151515
///
15161516
/// ```

0 commit comments

Comments
 (0)