Skip to content

Commit 0f301e8

Browse files
committed
Removed [inline] and copied over comments from Arc::new_cyclic
1 parent 42fb270 commit 0f301e8

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Diff for: library/alloc/src/rc.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,10 @@ impl<T> Rc<T> {
329329
/// to upgrade the weak reference before this function returns will result
330330
/// in a `None` value. However, the weak reference may be cloned freely and
331331
/// stored for use at a later time.
332-
#[inline]
333332
#[unstable(feature = "arc_new_cyclic", issue = "75861")]
334333
pub fn new_cyclic(data_fn: impl FnOnce(&Weak<T>) -> T) -> Rc<T> {
334+
// Construct the inner in the "uninitialized" state with a single
335+
// weak reference.
335336
let uninit_ptr: NonNull<_> = Box::leak(box RcBox {
336337
strong: Cell::new(0),
337338
weak: Cell::new(1),
@@ -343,6 +344,12 @@ impl<T> Rc<T> {
343344

344345
let weak = Weak { ptr: init_ptr };
345346

347+
// It's important we don't give up ownership of the weak pointer, or
348+
// else the memory might be freed by the time `data_fn` returns. If
349+
// we really wanted to pass ownership, we could create an additional
350+
// weak pointer for ourselves, but this would result in additional
351+
// updates to the weak reference count which might not be necessary
352+
// otherwise.
346353
let data = data_fn(&weak);
347354

348355
unsafe {
@@ -355,6 +362,9 @@ impl<T> Rc<T> {
355362
}
356363

357364
let strong = Rc::from_inner(init_ptr);
365+
366+
// Strong references should collectively own a shared weak reference,
367+
// so don't run the destructor for our old weak reference.
358368
mem::forget(weak);
359369
strong
360370
}

0 commit comments

Comments
 (0)