@@ -250,7 +250,7 @@ use core::pin::Pin;
250250use core:: ptr:: { self , NonNull } ;
251251use core:: slice:: from_raw_parts_mut;
252252
253- use crate :: alloc:: { box_free, handle_alloc_error, AllocRef , Global , Layout } ;
253+ use crate :: alloc:: { box_free, handle_alloc_error, AllocErr , AllocRef , Global , Layout } ;
254254use crate :: borrow:: { Cow , ToOwned } ;
255255use crate :: string:: String ;
256256use crate :: vec:: Vec ;
@@ -352,9 +352,11 @@ impl<T> Rc<T> {
352352 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
353353 pub fn new_uninit ( ) -> Rc < mem:: MaybeUninit < T > > {
354354 unsafe {
355- Rc :: from_ptr ( Rc :: allocate_for_layout ( Layout :: new :: < T > ( ) , |mem| {
356- mem as * mut RcBox < mem:: MaybeUninit < T > >
357- } ) )
355+ Rc :: from_ptr ( Rc :: allocate_for_layout (
356+ Layout :: new :: < T > ( ) ,
357+ |layout| Global . alloc ( layout) ,
358+ |mem| mem as * mut RcBox < mem:: MaybeUninit < T > > ,
359+ ) )
358360 }
359361 }
360362
@@ -381,9 +383,11 @@ impl<T> Rc<T> {
381383 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
382384 pub fn new_zeroed ( ) -> Rc < mem:: MaybeUninit < T > > {
383385 unsafe {
384- let mut uninit = Self :: new_uninit ( ) ;
385- ptr:: write_bytes :: < T > ( Rc :: get_mut_unchecked ( & mut uninit) . as_mut_ptr ( ) , 0 , 1 ) ;
386- uninit
386+ Rc :: from_ptr ( Rc :: allocate_for_layout (
387+ Layout :: new :: < T > ( ) ,
388+ |layout| Global . alloc_zeroed ( layout) ,
389+ |mem| mem as * mut RcBox < mem:: MaybeUninit < T > > ,
390+ ) )
387391 }
388392 }
389393
@@ -919,6 +923,7 @@ impl<T: ?Sized> Rc<T> {
919923 /// and must return back a (potentially fat)-pointer for the `RcBox<T>`.
920924 unsafe fn allocate_for_layout (
921925 value_layout : Layout ,
926+ allocate : impl FnOnce ( Layout ) -> Result < NonNull < [ u8 ] > , AllocErr > ,
922927 mem_to_rcbox : impl FnOnce ( * mut u8 ) -> * mut RcBox < T > ,
923928 ) -> * mut RcBox < T > {
924929 // Calculate layout using the given value layout.
@@ -928,7 +933,7 @@ impl<T: ?Sized> Rc<T> {
928933 let layout = Layout :: new :: < RcBox < ( ) > > ( ) . extend ( value_layout) . unwrap ( ) . 0 . pad_to_align ( ) ;
929934
930935 // Allocate for the layout.
931- let ptr = Global . alloc ( layout) . unwrap_or_else ( |_| handle_alloc_error ( layout) ) ;
936+ let ptr = allocate ( layout) . unwrap_or_else ( |_| handle_alloc_error ( layout) ) ;
932937
933938 // Initialize the RcBox
934939 let inner = mem_to_rcbox ( ptr. as_non_null_ptr ( ) . as_ptr ( ) ) ;
@@ -946,9 +951,11 @@ impl<T: ?Sized> Rc<T> {
946951 unsafe fn allocate_for_ptr ( ptr : * const T ) -> * mut RcBox < T > {
947952 // Allocate for the `RcBox<T>` using the given value.
948953 unsafe {
949- Self :: allocate_for_layout ( Layout :: for_value ( & * ptr) , |mem| {
950- set_data_ptr ( ptr as * mut T , mem) as * mut RcBox < T >
951- } )
954+ Self :: allocate_for_layout (
955+ Layout :: for_value ( & * ptr) ,
956+ |layout| Global . alloc ( layout) ,
957+ |mem| set_data_ptr ( ptr as * mut T , mem) as * mut RcBox < T > ,
958+ )
952959 }
953960 }
954961
@@ -979,9 +986,11 @@ impl<T> Rc<[T]> {
979986 /// Allocates an `RcBox<[T]>` with the given length.
980987 unsafe fn allocate_for_slice ( len : usize ) -> * mut RcBox < [ T ] > {
981988 unsafe {
982- Self :: allocate_for_layout ( Layout :: array :: < T > ( len) . unwrap ( ) , |mem| {
983- ptr:: slice_from_raw_parts_mut ( mem as * mut T , len) as * mut RcBox < [ T ] >
984- } )
989+ Self :: allocate_for_layout (
990+ Layout :: array :: < T > ( len) . unwrap ( ) ,
991+ |layout| Global . alloc ( layout) ,
992+ |mem| ptr:: slice_from_raw_parts_mut ( mem as * mut T , len) as * mut RcBox < [ T ] > ,
993+ )
985994 }
986995 }
987996}
@@ -2090,7 +2099,7 @@ impl<T: ?Sized> AsRef<T> for Rc<T> {
20902099#[ stable( feature = "pin" , since = "1.33.0" ) ]
20912100impl < T : ?Sized > Unpin for Rc < T > { }
20922101
2093- /// Get the offset within an `ArcInner ` for
2102+ /// Get the offset within an `RcBoRcBox ` for
20942103/// a payload of type described by a pointer.
20952104///
20962105/// # Safety
0 commit comments