@@ -1651,6 +1651,21 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
1651
1651
pub fn ptr_eq ( this : & Self , other : & Self ) -> bool {
1652
1652
this. ptr . as_ptr ( ) as * const ( ) == other. ptr . as_ptr ( ) as * const ( )
1653
1653
}
1654
+
1655
+ unsafe fn drop_dealloc ( & mut self ) {
1656
+ unsafe {
1657
+ // destroy the contained object
1658
+ ptr:: drop_in_place ( Self :: get_mut_unchecked ( self ) ) ;
1659
+
1660
+ // remove the implicit "strong weak" pointer now that we've
1661
+ // destroyed the contents.
1662
+ self . inner ( ) . dec_weak ( ) ;
1663
+
1664
+ if self . inner ( ) . weak ( ) == 0 {
1665
+ self . alloc . deallocate ( self . ptr . cast ( ) , Layout :: for_value ( self . ptr . as_ref ( ) ) ) ;
1666
+ }
1667
+ }
1668
+ }
1654
1669
}
1655
1670
1656
1671
impl < T : Clone , A : Allocator + Clone > Rc < T , A > {
@@ -2085,19 +2100,10 @@ unsafe impl<#[may_dangle] T: ?Sized, A: Allocator> Drop for Rc<T, A> {
2085
2100
/// drop(foo2); // Prints "dropped!"
2086
2101
/// ```
2087
2102
fn drop ( & mut self ) {
2088
- unsafe {
2089
- self . inner ( ) . dec_strong ( ) ;
2090
- if self . inner ( ) . strong ( ) == 0 {
2091
- // destroy the contained object
2092
- ptr:: drop_in_place ( Self :: get_mut_unchecked ( self ) ) ;
2093
-
2094
- // remove the implicit "strong weak" pointer now that we've
2095
- // destroyed the contents.
2096
- self . inner ( ) . dec_weak ( ) ;
2097
-
2098
- if self . inner ( ) . weak ( ) == 0 {
2099
- self . alloc . deallocate ( self . ptr . cast ( ) , Layout :: for_value ( self . ptr . as_ref ( ) ) ) ;
2100
- }
2103
+ self . inner ( ) . dec_strong ( ) ;
2104
+ if self . inner ( ) . strong ( ) == 0 {
2105
+ unsafe {
2106
+ self . drop_dealloc ( ) ;
2101
2107
}
2102
2108
}
2103
2109
}
0 commit comments