@@ -15,8 +15,6 @@ use core::num::NonZeroUsize;
1515use core:: ops:: { CoerceUnsized , DispatchFromDyn } ;
1616use core:: pin:: PinCoerceUnsized ;
1717use core:: ptr:: { self , NonNull } ;
18- #[ cfg( not( no_global_oom_handling) ) ]
19- use core:: slice;
2018
2119#[ cfg( not( no_global_oom_handling) ) ]
2220use crate :: alloc;
@@ -165,6 +163,30 @@ where
165163 }
166164}
167165
166+ #[ cfg( not( no_global_oom_handling) ) ]
167+ #[ track_caller]
168+ unsafe fn allocate_for_rc_with_value < T , A , const STRONG_COUNT : usize > (
169+ value : & T ,
170+ alloc : & A ,
171+ ) -> NonNull < T >
172+ where
173+ A : Allocator ,
174+ T : ?Sized ,
175+ {
176+ unsafe {
177+ let rc_layout = RcLayout :: from_value_ptr ( NonNull :: from ( value) ) ;
178+ let ptr = allocate_for_rc :: < A , _ , STRONG_COUNT > ( alloc, A :: allocate, & rc_layout) ;
179+
180+ ptr:: copy_nonoverlapping :: < u8 > (
181+ ptr:: from_ref ( value) . cast ( ) ,
182+ ptr. as_ptr ( ) . cast ( ) ,
183+ mem:: size_of_val ( value) ,
184+ ) ;
185+
186+ NonNull :: new_unchecked ( ptr. as_ptr ( ) . with_metadata_of ( value) )
187+ }
188+ }
189+
168190fn try_allocate_uninit_for_rc < A , const STRONG_COUNT : usize > (
169191 alloc : & A ,
170192 rc_layout : & RcLayout ,
@@ -686,30 +708,6 @@ where
686708 unsafe { Self :: from_weak ( RawWeak :: from_raw_parts ( ptr, alloc) ) }
687709 }
688710
689- #[ cfg( not( no_global_oom_handling) ) ]
690- fn from_box ( value : Box < T , A > ) -> Self
691- where
692- A : Allocator ,
693- {
694- unsafe {
695- let box_ptr = Box :: as_ptr ( & value) ;
696- let rc_layout = RcLayout :: from_value_ptr ( NonNull :: new_unchecked ( box_ptr. cast_mut ( ) ) ) ;
697- let rc_ptr = allocate_uninit_for_rc :: < A , 1 > ( & Box :: allocator ( & value) , & rc_layout) ;
698- let value_size = mem:: size_of_val_raw ( box_ptr) ;
699-
700- ptr:: copy_nonoverlapping :: < u8 > ( box_ptr. cast ( ) , rc_ptr. as_ptr ( ) . cast ( ) , value_size) ;
701-
702- let alloc = Box :: into_non_null_with_allocator ( value) . 1 ;
703-
704- drop ( Box :: from_raw_in ( box_ptr as * mut ManuallyDrop < T > , & alloc) ) ;
705-
706- Self :: from_raw_parts (
707- NonNull :: new_unchecked ( rc_ptr. as_ptr ( ) . with_metadata_of ( box_ptr) ) ,
708- alloc,
709- )
710- }
711- }
712-
713711 pub unsafe fn from_raw ( ptr : NonNull < T > ) -> Self
714712 where
715713 A : Default ,
@@ -859,9 +857,9 @@ where
859857
860858 mem:: forget ( guard) ;
861859
862- * ptr_ref = NonNull :: new_unchecked ( new_ptr. as_ptr ( ) . with_metadata_of ( ptr. as_ptr ( ) ) ) ;
863-
864860 RawRc :: from_raw_parts ( ptr, & * alloc) . drop :: < R > ( ) ;
861+
862+ * ptr_ref = NonNull :: new_unchecked ( new_ptr. as_ptr ( ) . with_metadata_of ( ptr. as_ptr ( ) ) ) ;
865863 }
866864 }
867865
@@ -875,15 +873,11 @@ where
875873 let ptr = * ptr_ref;
876874
877875 unsafe {
878- let rc_layout = RcLayout :: from_value_ptr ( ptr) ;
879- let new_ptr = allocate_uninit_for_rc :: < A , 1 > ( alloc, & rc_layout) ;
880- let length = mem:: size_of_val_raw ( ptr. as_ptr ( ) ) ;
881-
882- ptr:: copy_nonoverlapping :: < u8 > ( ptr. as_ptr ( ) . cast ( ) , new_ptr. as_ptr ( ) . cast ( ) , length) ;
883-
884- * ptr_ref = NonNull :: new_unchecked ( new_ptr. as_ptr ( ) . with_metadata_of ( ptr. as_ptr ( ) ) ) ;
876+ let new_ptr = allocate_for_rc_with_value :: < T , A , 1 > ( ptr. as_ref ( ) , alloc) ;
885877
886878 RawWeak :: from_raw_parts ( ptr, & * alloc) . drop_unchecked :: < R > ( ) ;
879+
880+ * ptr_ref = new_ptr;
887881 }
888882 }
889883
@@ -1168,34 +1162,6 @@ impl<T, A> RawRc<[T], A> {
11681162 uninit_rc. assume_init ( )
11691163 }
11701164 }
1171-
1172- #[ cfg( not( no_global_oom_handling) ) ]
1173- #[ track_caller]
1174- unsafe fn from_slice_copy_in ( slice : & [ T ] , alloc : A ) -> Self
1175- where
1176- A : Allocator ,
1177- {
1178- let uninit_rc = RawRc :: new_uninit_slice_in ( slice. len ( ) , alloc) ;
1179-
1180- unsafe {
1181- ptr:: copy_nonoverlapping :: < T > (
1182- slice. as_ptr ( ) ,
1183- uninit_rc. as_ptr ( ) . as_ptr ( ) . cast ( ) ,
1184- slice. len ( ) ,
1185- ) ;
1186-
1187- uninit_rc. assume_init ( )
1188- }
1189- }
1190-
1191- #[ cfg( not( no_global_oom_handling) ) ]
1192- #[ track_caller]
1193- unsafe fn from_slice_copy ( slice : & [ T ] ) -> Self
1194- where
1195- A : Allocator + Default ,
1196- {
1197- unsafe { Self :: from_slice_copy_in ( slice, A :: default ( ) ) }
1198- }
11991165}
12001166
12011167impl < T , A > RawRc < [ MaybeUninit < T > ] , A > {
@@ -1562,7 +1528,17 @@ where
15621528 A : Allocator ,
15631529{
15641530 fn from ( value : Box < T , A > ) -> Self {
1565- Self :: from_box ( value)
1531+ let value_ref = & * value;
1532+ let alloc_ref = Box :: allocator ( & value) ;
1533+
1534+ unsafe {
1535+ let rc_ptr = allocate_for_rc_with_value :: < T , A , 1 > ( value_ref, alloc_ref) ;
1536+ let ( box_ptr, alloc) = Box :: into_raw_with_allocator ( value) ;
1537+
1538+ drop ( Box :: from_raw_in ( box_ptr as * mut ManuallyDrop < T > , & alloc) ) ;
1539+
1540+ Self :: from_raw_parts ( rc_ptr, alloc)
1541+ }
15661542 }
15671543}
15681544
@@ -1589,7 +1565,12 @@ where
15891565 T : Copy ,
15901566{
15911567 default fn spec_from_slice ( slice : & [ T ] ) -> Self {
1592- unsafe { RawRc :: from_slice_copy ( slice) }
1568+ unsafe {
1569+ let alloc = A :: default ( ) ;
1570+ let ptr = allocate_for_rc_with_value :: < [ T ] , A , 1 > ( slice, & alloc) ;
1571+
1572+ Self :: from_raw_parts ( ptr, alloc)
1573+ }
15931574 }
15941575}
15951576
@@ -1668,12 +1649,17 @@ where
16681649 A : Allocator ,
16691650{
16701651 fn from ( value : Vec < T , A > ) -> Self {
1671- let ( ptr, length, capacity, alloc) = value. into_raw_parts_with_alloc ( ) ;
1672- let rc = unsafe { RawRc :: from_slice_copy_in ( slice:: from_raw_parts ( ptr, length) , alloc) } ;
1652+ let value_ref = & * value;
1653+ let alloc_ref = value. allocator ( ) ;
1654+
1655+ unsafe {
1656+ let rc_ptr = allocate_for_rc_with_value :: < [ T ] , A , 1 > ( value_ref, alloc_ref) ;
1657+ let ( vec_ptr, _length, capacity, alloc) = value. into_raw_parts_with_alloc ( ) ;
16731658
1674- drop ( unsafe { Vec :: from_raw_parts_in ( ptr , 0 , capacity, rc . allocator ( ) ) } ) ;
1659+ drop ( Vec :: from_raw_parts_in ( vec_ptr , 0 , capacity, & alloc ) ) ;
16751660
1676- rc
1661+ Self :: from_raw_parts ( rc_ptr, alloc)
1662+ }
16771663 }
16781664}
16791665
0 commit comments