Rollup merge of #41266 - projektir:weak_docs_rc, r=alexcrichton

Updating docs for std::rc::Rc

The same changes as PR [#41240 ](https://github.com/rust-lang/rust/pull/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
This commit is contained in:
Corey Farwell
2017-04-13 13:04:16 -04:00
committed by GitHub

View File

@@ -922,18 +922,29 @@ impl<T> From<T> for Rc<T> {
} }
} }
/// A weak version of [`Rc`][rc]. /// `Weak` is a version of [`Rc`] that holds a non-owning reference to the
/// managed value. The value is accessed by calling [`upgrade`] on the `Weak`
/// pointer, which returns an [`Option`]`<`[`Rc`]`<T>>`.
/// ///
/// `Weak` pointers do not count towards determining if the inner value /// Since a `Weak` reference does not count towards ownership, it will not
/// should be dropped. /// prevent the inner value from being dropped, and `Weak` itself makes no
/// guarantees about the value still being present and may return [`None`]
/// when [`upgrade`]d.
/// ///
/// The typical way to obtain a `Weak` pointer is to call /// A `Weak` pointer is useful for keeping a temporary reference to the value
/// [`Rc::downgrade`][downgrade]. /// within [`Rc`] without extending its lifetime. It is also used to prevent
/// circular references between [`Rc`] pointers, since mutual owning references
/// would never allow either [`Arc`] to be dropped. For example, a tree could
/// have strong [`Rc`] pointers from parent nodes to children, and `Weak`
/// pointers from children back to their parents.
/// ///
/// See the [module-level documentation](./index.html) for more details. /// The typical way to obtain a `Weak` pointer is to call [`Rc::downgrade`].
/// ///
/// [rc]: struct.Rc.html /// [`Rc`]: struct.Rc.html
/// [downgrade]: struct.Rc.html#method.downgrade /// [`Rc::downgrade`]: struct.Rc.html#method.downgrade
/// [`upgrade`]: struct.Weak.html#method.upgrade
/// [`Option`]: ../../std/option/enum.Option.html
/// [`None`]: ../../std/option/enum.Option.html#variant.None
#[stable(feature = "rc_weak", since = "1.4.0")] #[stable(feature = "rc_weak", since = "1.4.0")]
pub struct Weak<T: ?Sized> { pub struct Weak<T: ?Sized> {
ptr: Shared<RcBox<T>>, ptr: Shared<RcBox<T>>,
@@ -948,14 +959,11 @@ impl<T: ?Sized> !marker::Sync for Weak<T> {}
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {} impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Weak<U>> for Weak<T> {}
impl<T> Weak<T> { impl<T> Weak<T> {
/// Constructs a new `Weak<T>`, without an accompanying instance of `T`. /// Constructs a new `Weak<T>`, allocating memory for `T` without initializing
/// it. Calling [`upgrade`] on the return value always gives [`None`].
/// ///
/// This allocates memory for `T`, but does not initialize it. Calling /// [`upgrade`]: struct.Weak.html#method.upgrade
/// [`upgrade`][upgrade] on the return value always gives /// [`None`]: ../../std/option/enum.Option.html
/// [`None`][option].
///
/// [upgrade]: struct.Weak.html#method.upgrade
/// [option]: ../../std/option/enum.Option.html
/// ///
/// # Examples /// # Examples
/// ///
@@ -980,13 +988,13 @@ impl<T> Weak<T> {
} }
impl<T: ?Sized> Weak<T> { impl<T: ?Sized> Weak<T> {
/// Upgrades the `Weak` pointer to an [`Rc`][rc], if possible. /// Attempts to upgrade the `Weak` pointer to an [`Rc`], extending
/// the lifetime of the value if successful.
/// ///
/// Returns [`None`][option] if the strong count has reached zero and the /// Returns [`None`] if the value has since been dropped.
/// inner value was destroyed.
/// ///
/// [rc]: struct.Rc.html /// [`Rc`]: struct.Rc.html
/// [option]: ../../std/option/enum.Option.html /// [`None`]: ../../std/option/enum.Option.html
/// ///
/// # Examples /// # Examples
/// ///
@@ -1021,8 +1029,6 @@ impl<T: ?Sized> Weak<T> {
impl<T: ?Sized> Drop for Weak<T> { impl<T: ?Sized> Drop for Weak<T> {
/// Drops the `Weak` pointer. /// Drops the `Weak` pointer.
/// ///
/// This will decrement the weak reference count.
///
/// # Examples /// # Examples
/// ///
/// ``` /// ```
@@ -1061,10 +1067,7 @@ impl<T: ?Sized> Drop for Weak<T> {
#[stable(feature = "rc_weak", since = "1.4.0")] #[stable(feature = "rc_weak", since = "1.4.0")]
impl<T: ?Sized> Clone for Weak<T> { impl<T: ?Sized> Clone for Weak<T> {
/// Makes a clone of the `Weak` pointer. /// Makes a clone of the `Weak` pointer that points to the same value.
///
/// This creates another pointer to the same inner value, increasing the
/// weak reference count.
/// ///
/// # Examples /// # Examples
/// ///
@@ -1091,14 +1094,11 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
#[stable(feature = "downgraded_weak", since = "1.10.0")] #[stable(feature = "downgraded_weak", since = "1.10.0")]
impl<T> Default for Weak<T> { impl<T> Default for Weak<T> {
/// Constructs a new `Weak<T>`, without an accompanying instance of `T`. /// Constructs a new `Weak<T>`, allocating memory for `T` without initializing
/// it. Calling [`upgrade`] on the return value always gives [`None`].
/// ///
/// This allocates memory for `T`, but does not initialize it. Calling /// [`upgrade`]: struct.Weak.html#method.upgrade
/// [`upgrade`][upgrade] on the return value always gives /// [`None`]: ../../std/option/enum.Option.html
/// [`None`][option].
///
/// [upgrade]: struct.Weak.html#method.upgrade
/// [option]: ../../std/option/enum.Option.html
/// ///
/// # Examples /// # Examples
/// ///