@@ -87,6 +87,7 @@ use core::cmp::Ordering::{self, Less};
87
87
use core:: mem:: { self , size_of} ;
88
88
use core:: ptr;
89
89
90
+ use crate :: alloc:: { AllocRef , Global } ;
90
91
use crate :: borrow:: ToOwned ;
91
92
use crate :: boxed:: Box ;
92
93
use crate :: vec:: Vec ;
@@ -137,26 +138,28 @@ pub use hack::to_vec;
137
138
// `core::slice::SliceExt` - we need to supply these functions for the
138
139
// `test_permutations` test
139
140
mod hack {
141
+ use core:: alloc:: AllocRef ;
142
+
140
143
use crate :: boxed:: Box ;
141
144
use crate :: vec:: Vec ;
142
145
143
146
// We shouldn't add inline attribute to this since this is used in
144
147
// `vec!` macro mostly and causes perf regression. See #71204 for
145
148
// 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 > {
147
150
unsafe {
148
151
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 )
151
154
}
152
155
}
153
156
154
157
#[ 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 >
156
159
where
157
160
T : Clone ,
158
161
{
159
- let mut vec = Vec :: with_capacity ( s. len ( ) ) ;
162
+ let mut vec = Vec :: with_capacity_in ( s. len ( ) , alloc ) ;
160
163
vec. extend_from_slice ( s) ;
161
164
vec
162
165
}
@@ -388,11 +391,33 @@ impl<T> [T] {
388
391
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
389
392
#[ inline]
390
393
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 >
391
416
where
392
417
T : Clone ,
393
418
{
394
419
// N.B., see the `hack` module in this file for more details.
395
- hack:: to_vec ( self )
420
+ hack:: to_vec ( self , alloc )
396
421
}
397
422
398
423
/// Converts `self` into a vector without clones or allocation.
@@ -411,7 +436,7 @@ impl<T> [T] {
411
436
/// ```
412
437
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
413
438
#[ 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 > {
415
440
// N.B., see the `hack` module in this file for more details.
416
441
hack:: into_vec ( self )
417
442
}
@@ -730,7 +755,7 @@ impl<T: Clone> ToOwned for [T] {
730
755
731
756
#[ cfg( test) ]
732
757
fn to_owned ( & self ) -> Vec < T > {
733
- hack:: to_vec ( self )
758
+ hack:: to_vec ( self , Global )
734
759
}
735
760
736
761
fn clone_into ( & self , target : & mut Vec < T > ) {
0 commit comments