@@ -250,7 +250,7 @@ use core::pin::Pin;
250
250
use core:: ptr:: { self , NonNull } ;
251
251
use core:: slice:: from_raw_parts_mut;
252
252
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 } ;
254
254
use crate :: borrow:: { Cow , ToOwned } ;
255
255
use crate :: string:: String ;
256
256
use crate :: vec:: Vec ;
@@ -352,9 +352,11 @@ impl<T> Rc<T> {
352
352
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
353
353
pub fn new_uninit ( ) -> Rc < mem:: MaybeUninit < T > > {
354
354
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
+ ) )
358
360
}
359
361
}
360
362
@@ -381,9 +383,11 @@ impl<T> Rc<T> {
381
383
#[ unstable( feature = "new_uninit" , issue = "63291" ) ]
382
384
pub fn new_zeroed ( ) -> Rc < mem:: MaybeUninit < T > > {
383
385
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
+ ) )
387
391
}
388
392
}
389
393
@@ -919,6 +923,7 @@ impl<T: ?Sized> Rc<T> {
919
923
/// and must return back a (potentially fat)-pointer for the `RcBox<T>`.
920
924
unsafe fn allocate_for_layout (
921
925
value_layout : Layout ,
926
+ allocate : impl FnOnce ( Layout ) -> Result < NonNull < [ u8 ] > , AllocErr > ,
922
927
mem_to_rcbox : impl FnOnce ( * mut u8 ) -> * mut RcBox < T > ,
923
928
) -> * mut RcBox < T > {
924
929
// Calculate layout using the given value layout.
@@ -928,7 +933,7 @@ impl<T: ?Sized> Rc<T> {
928
933
let layout = Layout :: new :: < RcBox < ( ) > > ( ) . extend ( value_layout) . unwrap ( ) . 0 . pad_to_align ( ) ;
929
934
930
935
// 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) ) ;
932
937
933
938
// Initialize the RcBox
934
939
let inner = mem_to_rcbox ( ptr. as_non_null_ptr ( ) . as_ptr ( ) ) ;
@@ -946,9 +951,11 @@ impl<T: ?Sized> Rc<T> {
946
951
unsafe fn allocate_for_ptr ( ptr : * const T ) -> * mut RcBox < T > {
947
952
// Allocate for the `RcBox<T>` using the given value.
948
953
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
+ )
952
959
}
953
960
}
954
961
@@ -979,9 +986,11 @@ impl<T> Rc<[T]> {
979
986
/// Allocates an `RcBox<[T]>` with the given length.
980
987
unsafe fn allocate_for_slice ( len : usize ) -> * mut RcBox < [ T ] > {
981
988
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
+ )
985
994
}
986
995
}
987
996
}
@@ -2090,7 +2099,7 @@ impl<T: ?Sized> AsRef<T> for Rc<T> {
2090
2099
#[ stable( feature = "pin" , since = "1.33.0" ) ]
2091
2100
impl < T : ?Sized > Unpin for Rc < T > { }
2092
2101
2093
- /// Get the offset within an `ArcInner ` for
2102
+ /// Get the offset within an `RcBoRcBox ` for
2094
2103
/// a payload of type described by a pointer.
2095
2104
///
2096
2105
/// # Safety
0 commit comments