@@ -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
@@ -1809,7 +1809,9 @@ impl<T: Clone, A: Allocator + Clone> Rc<T, A> {
18091809 // reference to the allocation.
18101810 unsafe { & mut this. ptr . as_mut ( ) . value }
18111811 }
1812+ }
18121813
1814+ impl < T : Clone , A : Allocator > Rc < T , A > {
18131815 /// If we have the only reference to `T` then unwrap it. Otherwise, clone `T` and return the
18141816 /// clone.
18151817 ///
@@ -1845,7 +1847,7 @@ impl<T: Clone, A: Allocator + Clone> Rc<T, A> {
18451847 }
18461848}
18471849
1848- impl < A : Allocator + Clone > Rc < dyn Any , A > {
1850+ impl < A : Allocator > Rc < dyn Any , A > {
18491851 /// Attempt to downcast the `Rc<dyn Any>` to a concrete type.
18501852 ///
18511853 /// # Examples
@@ -1869,10 +1871,8 @@ impl<A: Allocator + Clone> Rc<dyn Any, A> {
18691871 pub fn downcast < T : Any > ( self ) -> Result < Rc < T , A > , Self > {
18701872 if ( * self ) . is :: < T > ( ) {
18711873 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) )
1874+ let ( ptr, alloc) = Rc :: into_inner_with_allocator ( self ) ;
1875+ Ok ( Rc :: from_inner_in ( ptr. cast ( ) , alloc) )
18761876 }
18771877 } else {
18781878 Err ( self )
@@ -1909,10 +1909,8 @@ impl<A: Allocator + Clone> Rc<dyn Any, A> {
19091909 #[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
19101910 pub unsafe fn downcast_unchecked < T : Any > ( self ) -> Rc < T , A > {
19111911 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)
1912+ let ( ptr, alloc) = Rc :: into_inner_with_allocator ( self ) ;
1913+ Rc :: from_inner_in ( ptr. cast ( ) , alloc)
19161914 }
19171915 }
19181916}
@@ -2661,12 +2659,13 @@ impl From<Rc<str>> for Rc<[u8]> {
26612659}
26622660
26632661#[ 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 ] > ;
2662+ impl < T , A : Allocator , const N : usize > TryFrom < Rc < [ T ] , A > > for Rc < [ T ; N ] , A > {
2663+ type Error = Rc < [ T ] , A > ;
26662664
2667- fn try_from ( boxed_slice : Rc < [ T ] > ) -> Result < Self , Self :: Error > {
2665+ fn try_from ( boxed_slice : Rc < [ T ] , A > ) -> Result < Self , Self :: Error > {
26682666 if boxed_slice. len ( ) == N {
2669- Ok ( unsafe { Rc :: from_raw ( Rc :: into_raw ( boxed_slice) as * mut [ T ; N ] ) } )
2667+ let ( ptr, alloc) = Rc :: into_inner_with_allocator ( boxed_slice) ;
2668+ Ok ( unsafe { Rc :: from_inner_in ( ptr. cast ( ) , alloc) } )
26702669 } else {
26712670 Err ( boxed_slice)
26722671 }
0 commit comments