diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index 0b57c36247e43..c5b5ef684efa5 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -374,19 +374,26 @@ impl Rc { } } - /// Constructs a new `Rc` using a closure `data_fn` that has access to a - /// weak reference to the constructing `Rc`. + /// Constructs a new `Rc` while giving you a `Weak` to the allocation, + /// to allow you to construct a `T` which holds a weak pointer to itself. /// /// Generally, a structure circularly referencing itself, either directly or - /// indirectly, should not hold a strong reference to prevent a memory leak. - /// In `data_fn`, initialization of `T` can make use of the weak reference - /// by cloning and storing it inside `T` for use at a later time. + /// indirectly, should not hold a strong reference to itself to prevent a memory leak. + /// Using this function, you get access to the weak pointer during the + /// initialization of `T`, before the `Rc` is created, such that you can + /// clone and store it inside the `T`. + /// + /// `new_cyclic` first allocates the managed allocation for the `Rc`, + /// then calls your closure, giving it a `Weak` to this allocation, + /// and only afterwards completes the construction of the `Rc` by placing + /// the `T` returned from your closure into the allocation. /// /// Since the new `Rc` is not fully-constructed until `Rc::new_cyclic` - /// returns, calling [`upgrade`] on the weak reference inside `data_fn` will + /// returns, calling [`upgrade`] on the weak reference inside your closure will /// fail and result in a `None` value. /// /// # Panics + /// /// If `data_fn` panics, the panic is propagated to the caller, and the /// temporary [`Weak`] is dropped normally. /// @@ -403,7 +410,12 @@ impl Rc { /// impl Gadget { /// /// Construct a reference counted Gadget. /// fn new() -> Rc { - /// Rc::new_cyclic(|me| Gadget { me: me.clone() }) + /// // `me` is a `Weak` pointing at the new allocation of the + /// // `Rc` we're constructing. + /// Rc::new_cyclic(|me| { + /// // Create the actual struct here. + /// Gadget { me: me.clone() } + /// }) /// } /// /// /// Return a reference counted pointer to Self. diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index f8b4d46ac105d..83c92aaddca18 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -351,23 +351,31 @@ impl Arc { unsafe { Self::from_inner(Box::leak(x).into()) } } - /// Constructs a new `Arc` using a closure `data_fn` that has access to - /// a weak reference to the constructing `Arc`. + /// Constructs a new `Arc` while giving you a `Weak` to the allocation, + /// to allow you to construct a `T` which holds a weak pointer to itself. /// /// Generally, a structure circularly referencing itself, either directly or - /// indirectly, should not hold a strong reference to prevent a memory leak. - /// In `data_fn`, initialization of `T` can make use of the weak reference - /// by cloning and storing it inside `T` for use at a later time. + /// indirectly, should not hold a strong reference to itself to prevent a memory leak. + /// Using this function, you get access to the weak pointer during the + /// initialization of `T`, before the `Arc` is created, such that you can + /// clone and store it inside the `T`. /// - /// Since the new `Arc` is not fully-constructed until - /// `Arc::new_cyclic` returns, calling [`upgrade`] on the weak - /// reference inside `data_fn` will fail and result in a `None` value. + /// `new_cyclic` first allocates the managed allocation for the `Arc`, + /// then calls your closure, giving it a `Weak` to this allocation, + /// and only afterwards completes the construction of the `Arc` by placing + /// the `T` returned from your closure into the allocation. + /// + /// Since the new `Arc` is not fully-constructed until `Arc::new_cyclic` + /// returns, calling [`upgrade`] on the weak reference inside your closure will + /// fail and result in a `None` value. /// /// # Panics + /// /// If `data_fn` panics, the panic is propagated to the caller, and the /// temporary [`Weak`] is dropped normally. /// /// # Example + /// /// ``` /// #![allow(dead_code)] /// use std::sync::{Arc, Weak}; @@ -379,7 +387,12 @@ impl Arc { /// impl Gadget { /// /// Construct a reference counted Gadget. /// fn new() -> Arc { - /// Arc::new_cyclic(|me| Gadget { me: me.clone() }) + /// // `me` is a `Weak` pointing at the new allocation of the + /// // `Arc` we're constructing. + /// Arc::new_cyclic(|me| { + /// // Create the actual struct here. + /// Gadget { me: me.clone() } + /// }) /// } /// /// /// Return a reference counted pointer to Self.