@@ -365,6 +365,12 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
365
365
unsafe { self . ptr . as_ref ( ) }
366
366
}
367
367
368
+ #[ inline]
369
+ fn into_inner_with_allocator ( this : Self ) -> ( NonNull < RcBox < T > > , A ) {
370
+ let this = mem:: ManuallyDrop :: new ( this) ;
371
+ ( this. ptr , unsafe { ptr:: read ( & this. alloc ) } )
372
+ }
373
+
368
374
#[ inline]
369
375
unsafe fn from_inner_in ( ptr : NonNull < RcBox < T > > , alloc : A ) -> Self {
370
376
Self { ptr, phantom : PhantomData , alloc }
@@ -1145,12 +1151,9 @@ impl<T, A: Allocator> Rc<mem::MaybeUninit<T>, A> {
1145
1151
/// ```
1146
1152
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
1147
1153
#[ inline]
1148
- pub unsafe fn assume_init ( self ) -> Rc < T , A >
1149
- where
1150
- A : Clone ,
1151
- {
1152
- let md_self = mem:: ManuallyDrop :: new ( self ) ;
1153
- unsafe { Rc :: from_inner_in ( md_self. ptr . cast ( ) , md_self. alloc . clone ( ) ) }
1154
+ pub unsafe fn assume_init ( self ) -> Rc < T , A > {
1155
+ let ( ptr, alloc) = Rc :: into_inner_with_allocator ( self ) ;
1156
+ unsafe { Rc :: from_inner_in ( ptr. cast ( ) , alloc) }
1154
1157
}
1155
1158
}
1156
1159
@@ -1189,12 +1192,9 @@ impl<T, A: Allocator> Rc<[mem::MaybeUninit<T>], A> {
1189
1192
/// ```
1190
1193
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
1191
1194
#[ inline]
1192
- pub unsafe fn assume_init ( self ) -> Rc < [ T ] , A >
1193
- where
1194
- A : Clone ,
1195
- {
1196
- let md_self = mem:: ManuallyDrop :: new ( self ) ;
1197
- unsafe { Rc :: from_ptr_in ( md_self. ptr . as_ptr ( ) as _ , md_self. alloc . clone ( ) ) }
1195
+ pub unsafe fn assume_init ( self ) -> Rc < [ T ] , A > {
1196
+ let ( ptr, alloc) = Rc :: into_inner_with_allocator ( self ) ;
1197
+ unsafe { Rc :: from_ptr_in ( ptr. as_ptr ( ) as _ , alloc) }
1198
1198
}
1199
1199
}
1200
1200
@@ -1845,7 +1845,7 @@ impl<T: Clone, A: Allocator + Clone> Rc<T, A> {
1845
1845
}
1846
1846
}
1847
1847
1848
- impl < A : Allocator + Clone > Rc < dyn Any , A > {
1848
+ impl < A : Allocator > Rc < dyn Any , A > {
1849
1849
/// Attempt to downcast the `Rc<dyn Any>` to a concrete type.
1850
1850
///
1851
1851
/// # Examples
@@ -1869,10 +1869,8 @@ impl<A: Allocator + Clone> Rc<dyn Any, A> {
1869
1869
pub fn downcast < T : Any > ( self ) -> Result < Rc < T , A > , Self > {
1870
1870
if ( * self ) . is :: < T > ( ) {
1871
1871
unsafe {
1872
- let ptr = self . ptr . cast :: < RcBox < T > > ( ) ;
1873
- let alloc = self . alloc . clone ( ) ;
1874
- forget ( self ) ;
1875
- Ok ( Rc :: from_inner_in ( ptr, alloc) )
1872
+ let ( ptr, alloc) = Rc :: into_inner_with_allocator ( self ) ;
1873
+ Ok ( Rc :: from_inner_in ( ptr. cast ( ) , alloc) )
1876
1874
}
1877
1875
} else {
1878
1876
Err ( self )
@@ -1909,10 +1907,8 @@ impl<A: Allocator + Clone> Rc<dyn Any, A> {
1909
1907
#[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
1910
1908
pub unsafe fn downcast_unchecked < T : Any > ( self ) -> Rc < T , A > {
1911
1909
unsafe {
1912
- let ptr = self . ptr . cast :: < RcBox < T > > ( ) ;
1913
- let alloc = self . alloc . clone ( ) ;
1914
- mem:: forget ( self ) ;
1915
- Rc :: from_inner_in ( ptr, alloc)
1910
+ let ( ptr, alloc) = Rc :: into_inner_with_allocator ( self ) ;
1911
+ Rc :: from_inner_in ( ptr. cast ( ) , alloc)
1916
1912
}
1917
1913
}
1918
1914
}
@@ -2661,12 +2657,13 @@ impl From<Rc<str>> for Rc<[u8]> {
2661
2657
}
2662
2658
2663
2659
#[ stable( feature = "boxed_slice_try_from" , since = "1.43.0" ) ]
2664
- impl < T , const N : usize > TryFrom < Rc < [ T ] > > for Rc < [ T ; N ] > {
2665
- type Error = Rc < [ T ] > ;
2660
+ impl < T , A : Allocator , const N : usize > TryFrom < Rc < [ T ] , A > > for Rc < [ T ; N ] , A > {
2661
+ type Error = Rc < [ T ] , A > ;
2666
2662
2667
- fn try_from ( boxed_slice : Rc < [ T ] > ) -> Result < Self , Self :: Error > {
2663
+ fn try_from ( boxed_slice : Rc < [ T ] , A > ) -> Result < Self , Self :: Error > {
2668
2664
if boxed_slice. len ( ) == N {
2669
- Ok ( unsafe { Rc :: from_raw ( Rc :: into_raw ( boxed_slice) as * mut [ T ; N ] ) } )
2665
+ let ( ptr, alloc) = Rc :: into_inner_with_allocator ( boxed_slice) ;
2666
+ Ok ( unsafe { Rc :: from_inner_in ( ptr. cast ( ) , alloc) } )
2670
2667
} else {
2671
2668
Err ( boxed_slice)
2672
2669
}
0 commit comments