@@ -247,7 +247,7 @@ use core::pin::Pin;
247247use core:: ptr:: { self , NonNull } ;
248248use core:: slice:: from_raw_parts_mut;
249249
250- use crate :: alloc:: { box_free, handle_alloc_error, AllocRef , Global , Layout } ;
250+ use crate :: alloc:: { box_free, handle_alloc_error, AllocErr , AllocRef , Global , Layout } ;
251251use crate :: borrow:: { Cow , ToOwned } ;
252252use crate :: string:: String ;
253253use crate :: vec:: Vec ;
@@ -349,9 +349,11 @@ impl<T> Rc<T> {
349349 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
350350 pub fn new_uninit ( ) -> Rc < mem:: MaybeUninit < T > > {
351351 unsafe {
352- Rc :: from_ptr ( Rc :: allocate_for_layout ( Layout :: new :: < T > ( ) , |mem| {
353- mem as * mut RcBox < mem:: MaybeUninit < T > >
354- } ) )
352+ Rc :: from_ptr ( Rc :: allocate_for_layout (
353+ Layout :: new :: < T > ( ) ,
354+ |layout| Global . alloc ( layout) ,
355+ |mem| mem as * mut RcBox < mem:: MaybeUninit < T > > ,
356+ ) )
355357 }
356358 }
357359
@@ -378,9 +380,11 @@ impl<T> Rc<T> {
378380 #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
379381 pub fn new_zeroed ( ) -> Rc < mem:: MaybeUninit < T > > {
380382 unsafe {
381- let mut uninit = Self :: new_uninit ( ) ;
382- ptr:: write_bytes :: < T > ( Rc :: get_mut_unchecked ( & mut uninit) . as_mut_ptr ( ) , 0 , 1 ) ;
383- uninit
383+ Rc :: from_ptr ( Rc :: allocate_for_layout (
384+ Layout :: new :: < T > ( ) ,
385+ |layout| Global . alloc_zeroed ( layout) ,
386+ |mem| mem as * mut RcBox < mem:: MaybeUninit < T > > ,
387+ ) )
384388 }
385389 }
386390
@@ -460,6 +464,40 @@ impl<T> Rc<[T]> {
460464 pub fn new_uninit_slice ( len : usize ) -> Rc < [ mem:: MaybeUninit < T > ] > {
461465 unsafe { Rc :: from_ptr ( Rc :: allocate_for_slice ( len) ) }
462466 }
467+
468+ /// Constructs a new reference-counted slice with uninitialized contents, with the memory being
469+ /// filled with `0` bytes.
470+ ///
471+ /// See [`MaybeUninit::zeroed`][zeroed] for examples of correct and
472+ /// incorrect usage of this method.
473+ ///
474+ /// # Examples
475+ ///
476+ /// ```
477+ /// #![feature(new_uninit)]
478+ ///
479+ /// use std::rc::Rc;
480+ ///
481+ /// let values = Rc::<[u32]>::new_zeroed_slice(3);
482+ /// let values = unsafe { values.assume_init() };
483+ ///
484+ /// assert_eq!(*values, [0, 0, 0])
485+ /// ```
486+ ///
487+ /// [zeroed]: ../../std/mem/union.MaybeUninit.html#method.zeroed
488+ #[ unstable( feature = "new_uninit" , issue = "63291" ) ]
489+ pub fn new_zeroed_slice ( len : usize ) -> Rc < [ mem:: MaybeUninit < T > ] > {
490+ unsafe {
491+ Rc :: from_ptr ( Rc :: allocate_for_layout (
492+ Layout :: array :: < T > ( len) . unwrap ( ) ,
493+ |layout| Global . alloc_zeroed ( layout) ,
494+ |mem| {
495+ ptr:: slice_from_raw_parts_mut ( mem as * mut T , len)
496+ as * mut RcBox < [ mem:: MaybeUninit < T > ] >
497+ } ,
498+ ) )
499+ }
500+ }
463501}
464502
465503impl < T > Rc < mem:: MaybeUninit < T > > {
@@ -905,6 +943,7 @@ impl<T: ?Sized> Rc<T> {
905943 /// and must return back a (potentially fat)-pointer for the `RcBox<T>`.
906944 unsafe fn allocate_for_layout (
907945 value_layout : Layout ,
946+ allocate : impl FnOnce ( Layout ) -> Result < NonNull < [ u8 ] > , AllocErr > ,
908947 mem_to_rcbox : impl FnOnce ( * mut u8 ) -> * mut RcBox < T > ,
909948 ) -> * mut RcBox < T > {
910949 // Calculate layout using the given value layout.
@@ -914,7 +953,7 @@ impl<T: ?Sized> Rc<T> {
914953 let layout = Layout :: new :: < RcBox < ( ) > > ( ) . extend ( value_layout) . unwrap ( ) . 0 . pad_to_align ( ) ;
915954
916955 // Allocate for the layout.
917- let ptr = Global . alloc ( layout) . unwrap_or_else ( |_| handle_alloc_error ( layout) ) ;
956+ let ptr = allocate ( layout) . unwrap_or_else ( |_| handle_alloc_error ( layout) ) ;
918957
919958 // Initialize the RcBox
920959 let inner = mem_to_rcbox ( ptr. as_non_null_ptr ( ) . as_ptr ( ) ) ;
@@ -932,9 +971,11 @@ impl<T: ?Sized> Rc<T> {
932971 unsafe fn allocate_for_ptr ( ptr : * const T ) -> * mut RcBox < T > {
933972 // Allocate for the `RcBox<T>` using the given value.
934973 unsafe {
935- Self :: allocate_for_layout ( Layout :: for_value ( & * ptr) , |mem| {
936- set_data_ptr ( ptr as * mut T , mem) as * mut RcBox < T >
937- } )
974+ Self :: allocate_for_layout (
975+ Layout :: for_value ( & * ptr) ,
976+ |layout| Global . alloc ( layout) ,
977+ |mem| set_data_ptr ( ptr as * mut T , mem) as * mut RcBox < T > ,
978+ )
938979 }
939980 }
940981
@@ -965,9 +1006,11 @@ impl<T> Rc<[T]> {
9651006 /// Allocates an `RcBox<[T]>` with the given length.
9661007 unsafe fn allocate_for_slice ( len : usize ) -> * mut RcBox < [ T ] > {
9671008 unsafe {
968- Self :: allocate_for_layout ( Layout :: array :: < T > ( len) . unwrap ( ) , |mem| {
969- ptr:: slice_from_raw_parts_mut ( mem as * mut T , len) as * mut RcBox < [ T ] >
970- } )
1009+ Self :: allocate_for_layout (
1010+ Layout :: array :: < T > ( len) . unwrap ( ) ,
1011+ |layout| Global . alloc ( layout) ,
1012+ |mem| ptr:: slice_from_raw_parts_mut ( mem as * mut T , len) as * mut RcBox < [ T ] > ,
1013+ )
9711014 }
9721015 }
9731016}
@@ -2061,7 +2104,7 @@ impl<T: ?Sized> AsRef<T> for Rc<T> {
20612104#[ stable( feature = "pin" , since = "1.33.0" ) ]
20622105impl < T : ?Sized > Unpin for Rc < T > { }
20632106
2064- /// Get the offset within an `ArcInner ` for
2107+ /// Get the offset within an `RcBox ` for
20652108/// a payload of type described by a pointer.
20662109///
20672110/// # Safety
0 commit comments