@@ -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
@@ -1809,7 +1809,9 @@ impl<T: Clone, A: Allocator + Clone> Rc<T, A> {
1809
1809
// reference to the allocation.
1810
1810
unsafe { & mut this. ptr . as_mut ( ) . value }
1811
1811
}
1812
+ }
1812
1813
1814
+ impl < T : Clone , A : Allocator > Rc < T , A > {
1813
1815
/// If we have the only reference to `T` then unwrap it. Otherwise, clone `T` and return the
1814
1816
/// clone.
1815
1817
///
@@ -1845,7 +1847,7 @@ impl<T: Clone, A: Allocator + Clone> Rc<T, A> {
1845
1847
}
1846
1848
}
1847
1849
1848
- impl < A : Allocator + Clone > Rc < dyn Any , A > {
1850
+ impl < A : Allocator > Rc < dyn Any , A > {
1849
1851
/// Attempt to downcast the `Rc<dyn Any>` to a concrete type.
1850
1852
///
1851
1853
/// # Examples
@@ -1869,10 +1871,8 @@ impl<A: Allocator + Clone> Rc<dyn Any, A> {
1869
1871
pub fn downcast < T : Any > ( self ) -> Result < Rc < T , A > , Self > {
1870
1872
if ( * self ) . is :: < T > ( ) {
1871
1873
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) )
1876
1876
}
1877
1877
} else {
1878
1878
Err ( self )
@@ -1909,10 +1909,8 @@ impl<A: Allocator + Clone> Rc<dyn Any, A> {
1909
1909
#[ unstable( feature = "downcast_unchecked" , issue = "90850" ) ]
1910
1910
pub unsafe fn downcast_unchecked < T : Any > ( self ) -> Rc < T , A > {
1911
1911
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)
1916
1914
}
1917
1915
}
1918
1916
}
@@ -2661,12 +2659,13 @@ impl From<Rc<str>> for Rc<[u8]> {
2661
2659
}
2662
2660
2663
2661
#[ 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 > ;
2666
2664
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 > {
2668
2666
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) } )
2670
2669
} else {
2671
2670
Err ( boxed_slice)
2672
2671
}
0 commit comments