@@ -348,6 +348,17 @@ impl<T: ?Sized> Rc<T> {
348348 unsafe fn from_ptr ( ptr : * mut RcBox < T > ) -> Self {
349349 unsafe { Self :: from_inner ( NonNull :: new_unchecked ( ptr) ) }
350350 }
351+
352+ // Non-inlined part of `drop`.
353+ #[ inline( never) ]
354+ unsafe fn drop_slow ( & mut self ) {
355+ // Destroy the data at this time, even though we must not free the box
356+ // allocation itself (there might still be weak pointers lying around).
357+ unsafe { ptr:: drop_in_place ( Self :: get_mut_unchecked ( self ) ) } ;
358+
359+ // Drop the weak ref collectively held by all strong references.
360+ drop ( Weak { ptr : self . ptr } ) ;
361+ }
351362}
352363
353364impl < T > Rc < T > {
@@ -1516,20 +1527,19 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Rc<T> {
15161527 /// drop(foo); // Doesn't print anything
15171528 /// drop(foo2); // Prints "dropped!"
15181529 /// ```
1530+ #[ inline]
15191531 fn drop ( & mut self ) {
15201532 unsafe {
15211533 self . inner ( ) . dec_strong ( ) ;
1522- if self . inner ( ) . strong ( ) == 0 {
1523- // destroy the contained object
1524- ptr:: drop_in_place ( Self :: get_mut_unchecked ( self ) ) ;
1525-
1526- // remove the implicit "strong weak" pointer now that we've
1527- // destroyed the contents.
1528- self . inner ( ) . dec_weak ( ) ;
1534+ if self . inner ( ) . strong ( ) != 0 {
1535+ return ;
1536+ }
15291537
1530- if self . inner ( ) . weak ( ) == 0 {
1531- Global . deallocate ( self . ptr . cast ( ) , Layout :: for_value ( self . ptr . as_ref ( ) ) ) ;
1532- }
1538+ if mem:: needs_drop :: < T > ( ) {
1539+ self . drop_slow ( ) ;
1540+ } else {
1541+ // Drop the weak ref collectively held by all strong references.
1542+ drop ( Weak { ptr : self . ptr } ) ;
15331543 }
15341544 }
15351545 }
@@ -2457,6 +2467,7 @@ unsafe impl<#[may_dangle] T: ?Sized> Drop for Weak<T> {
24572467 ///
24582468 /// assert!(other_weak_foo.upgrade().is_none());
24592469 /// ```
2470+ #[ inline]
24602471 fn drop ( & mut self ) {
24612472 let inner = if let Some ( inner) = self . inner ( ) { inner } else { return } ;
24622473
0 commit comments