@@ -365,6 +365,12 @@ impl<T: ?Sized, A: Allocator> Rc<T, A> {
365365 unsafe { self . ptr . as_ref ( ) }
366366 }
367367
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+
368374 #[ inline]
369375 unsafe fn from_inner_in ( ptr : NonNull < RcBox < T > > , alloc : A ) -> Self {
370376 Self { ptr, phantom : PhantomData , alloc }
@@ -1145,12 +1151,9 @@ impl<T, A: Allocator> Rc<mem::MaybeUninit<T>, A> {
11451151 /// ```
11461152 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
11471153 #[ 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) }
11541157 }
11551158}
11561159
@@ -1189,12 +1192,9 @@ impl<T, A: Allocator> Rc<[mem::MaybeUninit<T>], A> {
11891192 /// ```
11901193 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
11911194 #[ 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) }
11981198 }
11991199}
12001200
@@ -1845,7 +1845,7 @@ impl<T: Clone, A: Allocator + Clone> Rc<T, A> {
18451845 }
18461846}
18471847
1848- impl < A : Allocator + Clone > Rc < dyn Any , A > {
1848+ impl < A : Allocator > Rc < dyn Any , A > {
18491849 /// Attempt to downcast the `Rc<dyn Any>` to a concrete type.
18501850 ///
18511851 /// # Examples
@@ -1869,10 +1869,8 @@ impl<A: Allocator + Clone> Rc<dyn Any, A> {
18691869 pub fn downcast < T : Any > ( self ) -> Result < Rc < T , A > , Self > {
18701870 if ( * self ) . is :: < T > ( ) {
18711871 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) )
18761874 }
18771875 } else {
18781876 Err ( self )
@@ -1909,10 +1907,8 @@ impl<A: Allocator + Clone> Rc<dyn Any, A> {
19091907 #[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
19101908 pub unsafe fn downcast_unchecked < T : Any > ( self ) -> Rc < T , A > {
19111909 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)
19161912 }
19171913 }
19181914}
@@ -2661,12 +2657,13 @@ impl From<Rc<str>> for Rc<[u8]> {
26612657}
26622658
26632659#[ 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 > ;
26662662
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 > {
26682664 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) } )
26702667 } else {
26712668 Err ( boxed_slice)
26722669 }
0 commit comments