Skip to content

Commit ad6d63e

Browse files
committed
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.
1 parent 1454bbd commit ad6d63e

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
@@ -1715,8 +1715,9 @@ impl<T> Weak<T> {
17151715

17161716
/// Consumes the `Weak<T>` and turns it into a raw pointer.
17171717
///
1718-
/// This converts the weak pointer into a raw pointer, preserving the original weak count. It
1719-
/// can be turned back into the `Weak<T>` with [`from_raw`].
1718+
/// This converts the weak pointer into a raw pointer, while still preserving the ownership of
1719+
/// one weak reference (the weak count is not modified by this operation). It can be turned
1720+
/// back into the `Weak<T>` with [`from_raw`].
17201721
///
17211722
/// The same restrictions of accessing the target of the pointer as with
17221723
/// [`as_ptr`] apply.
@@ -1751,17 +1752,18 @@ impl<T> Weak<T> {
17511752
/// This can be used to safely get a strong reference (by calling [`upgrade`]
17521753
/// later) or to deallocate the weak count by dropping the `Weak<T>`.
17531754
///
1754-
/// It takes ownership of one weak count (with the exception of pointers created by [`new`],
1755-
/// as these don't have any corresponding weak count).
1755+
/// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
1756+
/// as these don't own anything; the method still works on them).
17561757
///
17571758
/// # Safety
17581759
///
1759-
/// The pointer must have originated from the [`into_raw`] and must still own its potential
1760-
/// weak reference count.
1760+
/// The pointer must have originated from the [`into_raw`] and must still own its potential
1761+
/// weak reference.
17611762
///
1762-
/// It is allowed for the strong count to be 0 at the time of calling this, but the weak count
1763-
/// must be non-zero or the pointer must have originated from a dangling `Weak<T>` (one created
1764-
/// by [`new`]).
1763+
/// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
1764+
/// takes ownership of one weak reference currently represented as a raw pointer (the weak
1765+
/// count is not modified by this operation) and therefore it must be paired with a previous
1766+
/// call to [`into_raw`].
17651767
///
17661768
/// # Examples
17671769
///

library/alloc/src/sync.rs

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

14861486
/// Consumes the `Weak<T>` and turns it into a raw pointer.
14871487
///
1488-
/// This converts the weak pointer into a raw pointer, preserving the original weak count. It
1489-
/// can be turned back into the `Weak<T>` with [`from_raw`].
1488+
/// This converts the weak pointer into a raw pointer, while still preserving the ownership of
1489+
/// one weak reference (the weak count is not modified by this operation). It can be turned
1490+
/// back into the `Weak<T>` with [`from_raw`].
14901491
///
14911492
/// The same restrictions of accessing the target of the pointer as with
14921493
/// [`as_ptr`] apply.
@@ -1516,24 +1517,23 @@ impl<T> Weak<T> {
15161517
result
15171518
}
15181519

1519-
/// Converts a raw pointer previously created by [`into_raw`] back into
1520-
/// `Weak<T>`.
1520+
/// Converts a raw pointer previously created by [`into_raw`] back into `Weak<T>`.
15211521
///
15221522
/// This can be used to safely get a strong reference (by calling [`upgrade`]
15231523
/// later) or to deallocate the weak count by dropping the `Weak<T>`.
15241524
///
1525-
/// It takes ownership of one weak count (with the exception of pointers created by [`new`],
1526-
/// as these don't have any corresponding weak count).
1525+
/// It takes ownership of one weak reference (with the exception of pointers created by [`new`],
1526+
/// as these don't own anything; the method still works on them).
15271527
///
15281528
/// # Safety
15291529
///
15301530
/// The pointer must have originated from the [`into_raw`] and must still own its potential
1531-
/// weak reference count.
1532-
///
1533-
/// It is allowed for the strong count to be 0 at the time of calling this, but the weak count
1534-
/// must be non-zero or the pointer must have originated from a dangling `Weak<T>` (one created
1535-
/// by [`new`]).
1531+
/// weak reference.
15361532
///
1533+
/// It is allowed for the strong count to be 0 at the time of calling this. Nevertheless, this
1534+
/// takes ownership of one weak reference currently represented as a raw pointer (the weak
1535+
/// count is not modified by this operation) and therefore it must be paired with a previous
1536+
/// call to [`into_raw`].
15371537
/// # Examples
15381538
///
15391539
/// ```

0 commit comments

Comments
 (0)