@@ -91,6 +91,7 @@ use core::cmp::Ordering::{self, Less};
91
91
use core:: mem:: { self , size_of} ;
92
92
use core:: ptr;
93
93
94
+ use crate :: alloc:: AllocRef ;
94
95
use crate :: borrow:: ToOwned ;
95
96
use crate :: boxed:: Box ;
96
97
use crate :: vec:: Vec ;
@@ -128,13 +129,14 @@ pub use hack::into_vec;
128
129
// HACK(japaric) needed for the implementation of `Vec::clone` during testing
129
130
// N.B., see the `hack` module in this file for more details.
130
131
#[ cfg( test) ]
131
- pub use hack:: to_vec;
132
+ pub use hack:: { to_vec, to_vec_in } ;
132
133
133
134
// HACK(japaric): With cfg(test) `impl [T]` is not available, these three
134
135
// functions are actually methods that are in `impl [T]` but not in
135
136
// `core::slice::SliceExt` - we need to supply these functions for the
136
137
// `test_permutations` test
137
138
mod hack {
139
+ use crate :: alloc:: { AllocRef , Global } ;
138
140
use crate :: boxed:: Box ;
139
141
#[ cfg( test) ]
140
142
use crate :: string:: ToString ;
@@ -143,11 +145,11 @@ mod hack {
143
145
// We shouldn't add inline attribute to this since this is used in
144
146
// `vec!` macro mostly and causes perf regression. See #71204 for
145
147
// 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 > {
147
149
unsafe {
148
150
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 )
151
153
}
152
154
}
153
155
@@ -156,7 +158,15 @@ mod hack {
156
158
where
157
159
T : Clone ,
158
160
{
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) ;
160
170
vector. extend_from_slice ( s) ;
161
171
vector
162
172
}
@@ -395,6 +405,29 @@ impl<T> [T] {
395
405
hack:: to_vec ( self )
396
406
}
397
407
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
+
398
431
/// Converts `self` into a vector without clones or allocation.
399
432
///
400
433
/// The resulting vector can be converted back into a box via
@@ -411,7 +444,7 @@ impl<T> [T] {
411
444
/// ```
412
445
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
413
446
#[ 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 > {
415
448
// N.B., see the `hack` module in this file for more details.
416
449
hack:: into_vec ( self )
417
450
}
0 commit comments