@@ -922,18 +922,29 @@ impl<T> From<T> for Rc<T> {
922
922
}
923
923
}
924
924
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>>`.
926
928
///
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.
929
933
///
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.
932
940
///
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`] .
934
942
///
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
937
948
#[ stable( feature = "rc_weak" , since = "1.4.0" ) ]
938
949
pub struct Weak < T : ?Sized > {
939
950
ptr : Shared < RcBox < T > > ,
@@ -948,14 +959,11 @@ impl<T: ?Sized> !marker::Sync for Weak<T> {}
948
959
impl < T : ?Sized + Unsize < U > , U : ?Sized > CoerceUnsized < Weak < U > > for Weak < T > { }
949
960
950
961
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`].
956
964
///
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
959
967
///
960
968
/// # Examples
961
969
///
@@ -980,13 +988,13 @@ impl<T> Weak<T> {
980
988
}
981
989
982
990
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.
984
993
///
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.
987
995
///
988
- /// [rc ]: struct.Rc.html
989
- /// [option ]: ../../std/option/enum.Option.html
996
+ /// [`Rc` ]: struct.Rc.html
997
+ /// [`None` ]: ../../std/option/enum.Option.html
990
998
///
991
999
/// # Examples
992
1000
///
@@ -1021,8 +1029,6 @@ impl<T: ?Sized> Weak<T> {
1021
1029
impl < T : ?Sized > Drop for Weak < T > {
1022
1030
/// Drops the `Weak` pointer.
1023
1031
///
1024
- /// This will decrement the weak reference count.
1025
- ///
1026
1032
/// # Examples
1027
1033
///
1028
1034
/// ```
@@ -1061,10 +1067,7 @@ impl<T: ?Sized> Drop for Weak<T> {
1061
1067
1062
1068
#[ stable( feature = "rc_weak" , since = "1.4.0" ) ]
1063
1069
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.
1068
1071
///
1069
1072
/// # Examples
1070
1073
///
@@ -1091,14 +1094,11 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
1091
1094
1092
1095
#[ stable( feature = "downgraded_weak" , since = "1.10.0" ) ]
1093
1096
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`].
1099
1099
///
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
1102
1102
///
1103
1103
/// # Examples
1104
1104
///
0 commit comments