@@ -293,6 +293,15 @@ struct RcBox<T: ?Sized> {
293293 value : T ,
294294}
295295
296+ /// Calculate layout for `RcBox<T>` using the inner value's layout
297+ fn rcbox_layout_for_value_layout ( layout : Layout ) -> Layout {
298+ // Calculate layout using the given value layout.
299+ // Previously, layout was calculated on the expression
300+ // `&*(ptr as *const RcBox<T>)`, but this created a misaligned
301+ // reference (see #54908).
302+ Layout :: new :: < RcBox < ( ) > > ( ) . extend ( layout) . unwrap ( ) . 0 . pad_to_align ( )
303+ }
304+
296305/// A single-threaded reference-counting pointer. 'Rc' stands for 'Reference
297306/// Counted'.
298307///
@@ -1334,11 +1343,7 @@ impl<T: ?Sized> Rc<T> {
13341343 allocate : impl FnOnce ( Layout ) -> Result < NonNull < [ u8 ] > , AllocError > ,
13351344 mem_to_rcbox : impl FnOnce ( * mut u8 ) -> * mut RcBox < T > ,
13361345 ) -> * mut RcBox < T > {
1337- // Calculate layout using the given value layout.
1338- // Previously, layout was calculated on the expression
1339- // `&*(ptr as *const RcBox<T>)`, but this created a misaligned
1340- // reference (see #54908).
1341- let layout = Layout :: new :: < RcBox < ( ) > > ( ) . extend ( value_layout) . unwrap ( ) . 0 . pad_to_align ( ) ;
1346+ let layout = rcbox_layout_for_value_layout ( value_layout) ;
13421347 unsafe {
13431348 Rc :: try_allocate_for_layout ( value_layout, allocate, mem_to_rcbox)
13441349 . unwrap_or_else ( |_| handle_alloc_error ( layout) )
@@ -1357,11 +1362,7 @@ impl<T: ?Sized> Rc<T> {
13571362 allocate : impl FnOnce ( Layout ) -> Result < NonNull < [ u8 ] > , AllocError > ,
13581363 mem_to_rcbox : impl FnOnce ( * mut u8 ) -> * mut RcBox < T > ,
13591364 ) -> Result < * mut RcBox < T > , AllocError > {
1360- // Calculate layout using the given value layout.
1361- // Previously, layout was calculated on the expression
1362- // `&*(ptr as *const RcBox<T>)`, but this created a misaligned
1363- // reference (see #54908).
1364- let layout = Layout :: new :: < RcBox < ( ) > > ( ) . extend ( value_layout) . unwrap ( ) . 0 . pad_to_align ( ) ;
1365+ let layout = rcbox_layout_for_value_layout ( value_layout) ;
13651366
13661367 // Allocate for the layout.
13671368 let ptr = allocate ( layout) ?;
@@ -1428,7 +1429,7 @@ impl<T> Rc<[T]> {
14281429 }
14291430 }
14301431
1431- /// Copy elements from slice into newly allocated Rc<\[T\]>
1432+ /// Copy elements from slice into newly allocated ` Rc<[T]>`
14321433 ///
14331434 /// Unsafe because the caller must either take ownership or bind `T: Copy`
14341435 #[ cfg( not( no_global_oom_handling) ) ]
@@ -1440,6 +1441,48 @@ impl<T> Rc<[T]> {
14401441 }
14411442 }
14421443
1444+ /// Create an `Rc<[T]>` by reusing the underlying memory
1445+ /// of a `Vec<T>`. This will return the vector if the existing allocation
1446+ /// is not large enough.
1447+ #[ cfg( not( no_global_oom_handling) ) ]
1448+ fn try_from_vec_in_place ( mut v : Vec < T > ) -> Result < Rc < [ T ] > , Vec < T > > {
1449+ let layout_elements = Layout :: array :: < T > ( v. len ( ) ) . unwrap ( ) ;
1450+ let layout_allocation = Layout :: array :: < T > ( v. capacity ( ) ) . unwrap ( ) ;
1451+ let layout_rcbox = rcbox_layout_for_value_layout ( layout_elements) ;
1452+ let mut ptr = NonNull :: new ( v. as_mut_ptr ( ) ) . expect ( "`Vec<T>` stores `NonNull<T>`" ) ;
1453+ if layout_rcbox. size ( ) > layout_allocation. size ( )
1454+ || layout_rcbox. align ( ) > layout_allocation. align ( )
1455+ {
1456+ // Can't fit - calling `grow` would involve `realloc`
1457+ // (which copies the elements), followed by copying again.
1458+ return Err ( v) ;
1459+ }
1460+ if layout_rcbox. size ( ) < layout_allocation. size ( )
1461+ || layout_rcbox. align ( ) < layout_allocation. align ( )
1462+ {
1463+ // We need to shrink the allocation so that it fits
1464+ // https://doc.rust-lang.org/nightly/std/alloc/trait.Allocator.html#memory-fitting
1465+ // SAFETY:
1466+ // - Vec allocates by requesting `Layout::array::<T>(capacity)`, so this capacity matches
1467+ // - `layout_rcbox` is smaller
1468+ // If this fails, the ownership has not been transferred
1469+ if let Ok ( p) = unsafe { Global . shrink ( ptr. cast ( ) , layout_allocation, layout_rcbox) } {
1470+ ptr = p. cast ( ) ;
1471+ } else {
1472+ return Err ( v) ;
1473+ }
1474+ }
1475+ // Make sure the vec's memory isn't deallocated now
1476+ let v = mem:: ManuallyDrop :: new ( v) ;
1477+ let ptr: * mut RcBox < [ T ] > = ptr:: slice_from_raw_parts_mut ( ptr. as_ptr ( ) , v. len ( ) ) as _ ;
1478+ unsafe {
1479+ ptr:: copy ( ptr. cast :: < T > ( ) , & mut ( * ptr) . value as * mut [ T ] as * mut T , v. len ( ) ) ;
1480+ ptr:: write ( & mut ( * ptr) . strong , Cell :: new ( 1 ) ) ;
1481+ ptr:: write ( & mut ( * ptr) . weak , Cell :: new ( 1 ) ) ;
1482+ Ok ( Self :: from_ptr ( ptr) )
1483+ }
1484+ }
1485+
14431486 /// Constructs an `Rc<[T]>` from an iterator known to be of a certain size.
14441487 ///
14451488 /// Behavior is undefined should the size be wrong.
@@ -1965,14 +2008,17 @@ impl<T> From<Vec<T>> for Rc<[T]> {
19652008 /// assert_eq!(vec![1, 2, 3], *shared);
19662009 /// ```
19672010 #[ inline]
1968- fn from ( mut v : Vec < T > ) -> Rc < [ T ] > {
1969- unsafe {
1970- let rc = Rc :: copy_from_slice ( & v) ;
1971-
1972- // Allow the Vec to free its memory, but not destroy its contents
1973- v. set_len ( 0 ) ;
1974-
1975- rc
2011+ fn from ( v : Vec < T > ) -> Rc < [ T ] > {
2012+ match Rc :: try_from_vec_in_place ( v) {
2013+ Ok ( rc) => rc,
2014+ Err ( mut v) => {
2015+ unsafe {
2016+ let rc = Rc :: copy_from_slice ( & v) ;
2017+ // Allow the Vec to free its memory, but not destroy its contents
2018+ v. set_len ( 0 ) ;
2019+ rc
2020+ }
2021+ }
19762022 }
19772023 }
19782024}
0 commit comments