@@ -87,6 +87,7 @@ use core::cmp::Ordering::{self, Less};
8787use core:: mem:: { self , size_of} ;
8888use core:: ptr;
8989
90+ use crate :: alloc:: { AllocRef , Global } ;
9091use crate :: borrow:: ToOwned ;
9192use crate :: boxed:: Box ;
9293use crate :: vec:: Vec ;
@@ -137,26 +138,28 @@ pub use hack::to_vec;
137138// `core::slice::SliceExt` - we need to supply these functions for the
138139// `test_permutations` test
139140mod hack {
141+ use core:: alloc:: AllocRef ;
142+
140143 use crate :: boxed:: Box ;
141144 use crate :: vec:: Vec ;
142145
143146 // We shouldn't add inline attribute to this since this is used in
144147 // `vec!` macro mostly and causes perf regression. See #71204 for
145148 // discussion and perf results.
146- pub fn into_vec < T > ( b : Box < [ T ] > ) -> Vec < T > {
149+ pub fn into_vec < T , A : AllocRef > ( b : Box < [ T ] , A > ) -> Vec < T , A > {
147150 unsafe {
148151 let len = b. len ( ) ;
149- let b = Box :: into_raw ( b) ;
150- Vec :: from_raw_parts ( b as * mut T , len, len)
152+ let ( b , alloc ) = Box :: into_raw_with_alloc ( b) ;
153+ Vec :: from_raw_parts_in ( b as * mut T , len, len, alloc )
151154 }
152155 }
153156
154157 #[ inline]
155- pub fn to_vec < T > ( s : & [ T ] ) -> Vec < T >
158+ pub fn to_vec < T , A : AllocRef > ( s : & [ T ] , alloc : A ) -> Vec < T , A >
156159 where
157160 T : Clone ,
158161 {
159- let mut vec = Vec :: with_capacity ( s. len ( ) ) ;
162+ let mut vec = Vec :: with_capacity_in ( s. len ( ) , alloc ) ;
160163 vec. extend_from_slice ( s) ;
161164 vec
162165 }
@@ -388,11 +391,33 @@ impl<T> [T] {
388391 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
389392 #[ inline]
390393 pub fn to_vec ( & self ) -> Vec < T >
394+ where
395+ T : Clone ,
396+ {
397+ self . to_vec_in ( Global )
398+ }
399+
400+ /// Copies `self` into a new `Vec` with an allocator.
401+ ///
402+ /// # Examples
403+ ///
404+ /// ```
405+ /// #![feature(allocator_api)]
406+ ///
407+ /// use std::alloc::System;
408+ ///
409+ /// let s = [10, 40, 30];
410+ /// let x = s.to_vec_in(System);
411+ /// // Here, `s` and `x` can be modified independently.
412+ /// ```
413+ #[ inline]
414+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
415+ pub fn to_vec_in < A : AllocRef > ( & self , alloc : A ) -> Vec < T , A >
391416 where
392417 T : Clone ,
393418 {
394419 // N.B., see the `hack` module in this file for more details.
395- hack:: to_vec ( self )
420+ hack:: to_vec ( self , alloc )
396421 }
397422
398423 /// Converts `self` into a vector without clones or allocation.
@@ -411,7 +436,7 @@ impl<T> [T] {
411436 /// ```
412437 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
413438 #[ inline]
414- pub fn into_vec ( self : Box < Self > ) -> Vec < T > {
439+ pub fn into_vec < A : AllocRef > ( self : Box < Self , A > ) -> Vec < T , A > {
415440 // N.B., see the `hack` module in this file for more details.
416441 hack:: into_vec ( self )
417442 }
@@ -730,7 +755,7 @@ impl<T: Clone> ToOwned for [T] {
730755
731756 #[ cfg( test) ]
732757 fn to_owned ( & self ) -> Vec < T > {
733- hack:: to_vec ( self )
758+ hack:: to_vec ( self , Global )
734759 }
735760
736761 fn clone_into ( & self , target : & mut Vec < T > ) {
0 commit comments