Skip to content

Commit 6afb2c4

Browse files
authored
Rollup merge of #41266 - projektir:weak_docs_rc, r=alexcrichton
Updating docs for std::rc::Rc The same changes as PR [#41240 ](#41240), but for [`std::rc::Weak`](https://doc.rust-lang.org/std/rc/struct.Weak.html). At least, as far as I am aware, the Weak pointer is the same for both, and they're basically the same, just one is thread-safe and the other is not. r? @alexcrichton
2 parents 6dfd8f6 + f84cc0c commit 6afb2c4

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

src/liballoc/rc.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -922,18 +922,29 @@ impl<T> From<T> for Rc<T> {
922922
}
923923
}
924924

925-
/// A weak version of [`Rc`][rc].
925+
/// `Weak` is a version of [`Rc`] that holds a non-owning reference to the
926+
/// managed value. The value is accessed by calling [`upgrade`] on the `Weak`
927+
/// pointer, which returns an [`Option`]`<`[`Rc`]`<T>>`.
926928
///
927-
/// `Weak` pointers do not count towards determining if the inner value
928-
/// should be dropped.
929+
/// Since a `Weak` reference does not count towards ownership, it will not
930+
/// prevent the inner value from being dropped, and `Weak` itself makes no
931+
/// guarantees about the value still being present and may return [`None`]
932+
/// when [`upgrade`]d.
929933
///
930-
/// The typical way to obtain a `Weak` pointer is to call
931-
/// [`Rc::downgrade`][downgrade].
934+
/// A `Weak` pointer is useful for keeping a temporary reference to the value
935+
/// within [`Rc`] without extending its lifetime. It is also used to prevent
936+
/// circular references between [`Rc`] pointers, since mutual owning references
937+
/// would never allow either [`Arc`] to be dropped. For example, a tree could
938+
/// have strong [`Rc`] pointers from parent nodes to children, and `Weak`
939+
/// pointers from children back to their parents.
932940
///
933-
/// See the [module-level documentation](./index.html) for more details.
941+
/// The typical way to obtain a `Weak` pointer is to call [`Rc::downgrade`].
934942
///
935-
/// [rc]: struct.Rc.html
936-
/// [downgrade]: struct.Rc.html#method.downgrade
943+
/// [`Rc`]: struct.Rc.html
944+
/// [`Rc::downgrade`]: struct.Rc.html#method.downgrade
945+
/// [`upgrade`]: struct.Weak.html#method.upgrade
946+
/// [`Option`]: ../../std/option/enum.Option.html
947+
/// [`None`]: ../../std/option/enum.Option.html#variant.None
937948
#[stable(feature = "rc_weak", since = "1.4.0")]
938949
pub struct Weak<T: ?Sized> {
939950
ptr: Shared<RcBox<T>>,
@@ -948,14 +959,11 @@ impl<T: ?Sized> !marker::Sync for Weak<T> {}
948959
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
949960

950961
impl<T> Weak<T> {
951-
/// Constructs a new `Weak<T>`, without an accompanying instance of `T`.
952-
///
953-
/// This allocates memory for `T`, but does not initialize it. Calling
954-
/// [`upgrade`][upgrade] on the return value always gives
955-
/// [`None`][option].
962+
/// Constructs a new `Weak<T>`, allocating memory for `T` without initializing
963+
/// it. Calling [`upgrade`] on the return value always gives [`None`].
956964
///
957-
/// [upgrade]: struct.Weak.html#method.upgrade
958-
/// [option]: ../../std/option/enum.Option.html
965+
/// [`upgrade`]: struct.Weak.html#method.upgrade
966+
/// [`None`]: ../../std/option/enum.Option.html
959967
///
960968
/// # Examples
961969
///
@@ -980,13 +988,13 @@ impl<T> Weak<T> {
980988
}
981989

982990
impl<T: ?Sized> Weak<T> {
983-
/// Upgrades the `Weak` pointer to an [`Rc`][rc], if possible.
991+
/// Attempts to upgrade the `Weak` pointer to an [`Rc`], extending
992+
/// the lifetime of the value if successful.
984993
///
985-
/// Returns [`None`][option] if the strong count has reached zero and the
986-
/// inner value was destroyed.
994+
/// Returns [`None`] if the value has since been dropped.
987995
///
988-
/// [rc]: struct.Rc.html
989-
/// [option]: ../../std/option/enum.Option.html
996+
/// [`Rc`]: struct.Rc.html
997+
/// [`None`]: ../../std/option/enum.Option.html
990998
///
991999
/// # Examples
9921000
///
@@ -1021,8 +1029,6 @@ impl<T: ?Sized> Weak<T> {
10211029
impl<T: ?Sized> Drop for Weak<T> {
10221030
/// Drops the `Weak` pointer.
10231031
///
1024-
/// This will decrement the weak reference count.
1025-
///
10261032
/// # Examples
10271033
///
10281034
/// ```
@@ -1061,10 +1067,7 @@ impl<T: ?Sized> Drop for Weak<T> {
10611067

10621068
#[stable(feature = "rc_weak", since = "1.4.0")]
10631069
impl<T: ?Sized> Clone for Weak<T> {
1064-
/// Makes a clone of the `Weak` pointer.
1065-
///
1066-
/// This creates another pointer to the same inner value, increasing the
1067-
/// weak reference count.
1070+
/// Makes a clone of the `Weak` pointer that points to the same value.
10681071
///
10691072
/// # Examples
10701073
///
@@ -1091,14 +1094,11 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
10911094

10921095
#[stable(feature = "downgraded_weak", since = "1.10.0")]
10931096
impl<T> Default for Weak<T> {
1094-
/// Constructs a new `Weak<T>`, without an accompanying instance of `T`.
1095-
///
1096-
/// This allocates memory for `T`, but does not initialize it. Calling
1097-
/// [`upgrade`][upgrade] on the return value always gives
1098-
/// [`None`][option].
1097+
/// Constructs a new `Weak<T>`, allocating memory for `T` without initializing
1098+
/// it. Calling [`upgrade`] on the return value always gives [`None`].
10991099
///
1100-
/// [upgrade]: struct.Weak.html#method.upgrade
1101-
/// [option]: ../../std/option/enum.Option.html
1100+
/// [`upgrade`]: struct.Weak.html#method.upgrade
1101+
/// [`None`]: ../../std/option/enum.Option.html
11021102
///
11031103
/// # Examples
11041104
///

0 commit comments

Comments
 (0)