@@ -91,6 +91,7 @@ use core::cmp::Ordering::{self, Less};
9191use core:: mem:: { self , size_of} ;
9292use core:: ptr;
9393
94+ use crate :: alloc:: AllocRef ;
9495use crate :: borrow:: ToOwned ;
9596use crate :: boxed:: Box ;
9697use crate :: vec:: Vec ;
@@ -128,13 +129,14 @@ pub use hack::into_vec;
128129// HACK(japaric) needed for the implementation of `Vec::clone` during testing
129130// N.B., see the `hack` module in this file for more details.
130131#[ cfg( test) ]
131- pub use hack:: to_vec;
132+ pub use hack:: { to_vec, to_vec_in } ;
132133
133134// HACK(japaric): With cfg(test) `impl [T]` is not available, these three
134135// functions are actually methods that are in `impl [T]` but not in
135136// `core::slice::SliceExt` - we need to supply these functions for the
136137// `test_permutations` test
137138mod hack {
139+ use crate :: alloc:: { AllocRef , Global } ;
138140 use crate :: boxed:: Box ;
139141 #[ cfg( test) ]
140142 use crate :: string:: ToString ;
@@ -143,11 +145,11 @@ mod hack {
143145 // We shouldn't add inline attribute to this since this is used in
144146 // `vec!` macro mostly and causes perf regression. See #71204 for
145147 // discussion and perf results.
146- pub fn into_vec < T > ( b : Box < [ T ] > ) -> Vec < T > {
148+ pub fn into_vec < T , A : AllocRef > ( b : Box < [ T ] , A > ) -> Vec < T , A > {
147149 unsafe {
148150 let len = b. len ( ) ;
149- let b = Box :: into_raw ( b) ;
150- Vec :: from_raw_parts ( b as * mut T , len, len)
151+ let ( b , a ) = Box :: into_raw_with_alloc ( b) ;
152+ Vec :: from_raw_parts_in ( b as * mut T , len, len, a )
151153 }
152154 }
153155
@@ -156,7 +158,15 @@ mod hack {
156158 where
157159 T : Clone ,
158160 {
159- let mut vector = Vec :: with_capacity ( s. len ( ) ) ;
161+ to_vec_in ( s, Global )
162+ }
163+
164+ #[ inline]
165+ pub fn to_vec_in < T , A : AllocRef > ( s : & [ T ] , alloc : A ) -> Vec < T , A >
166+ where
167+ T : Clone ,
168+ {
169+ let mut vector = Vec :: with_capacity_in ( s. len ( ) , alloc) ;
160170 vector. extend_from_slice ( s) ;
161171 vector
162172 }
@@ -395,6 +405,29 @@ impl<T> [T] {
395405 hack:: to_vec ( self )
396406 }
397407
408+ /// Copies `self` into a new `Vec`.
409+ ///
410+ /// # Examples
411+ ///
412+ /// ```
413+ /// #![feature(allocator_api)]
414+ ///
415+ /// use std::alloc::System;
416+ ///
417+ /// let s = [10, 40, 30];
418+ /// let x = s.to_vec_in(System);
419+ /// // Here, `s` and `x` can be modified independently.
420+ /// ```
421+ #[ inline]
422+ #[ unstable( feature = "allocator_api" , issue = "32838" ) ]
423+ pub fn to_vec_in < A : AllocRef > ( & self , alloc : A ) -> Vec < T , A >
424+ where
425+ T : Clone ,
426+ {
427+ // N.B., see the `hack` module in this file for more details.
428+ hack:: to_vec_in ( self , alloc)
429+ }
430+
398431 /// Converts `self` into a vector without clones or allocation.
399432 ///
400433 /// The resulting vector can be converted back into a box via
@@ -411,7 +444,7 @@ impl<T> [T] {
411444 /// ```
412445 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
413446 #[ inline]
414- pub fn into_vec ( self : Box < Self > ) -> Vec < T > {
447+ pub fn into_vec < A : AllocRef > ( self : Box < Self , A > ) -> Vec < T , A > {
415448 // N.B., see the `hack` module in this file for more details.
416449 hack:: into_vec ( self )
417450 }
0 commit comments