Skip to content

Commit a1a13b2

Browse files
committed
Auto merge of rust-lang#78461 - TimDiekmann:vec-alloc, r=Amanieu
Add support for custom allocators in `Vec` This follows the [roadmap](rust-lang/wg-allocators#7) of the allocator WG to add custom allocators to collections. r? `@Amanieu` This pull request requires a crater run. ### Prior work: - rust-lang#71873: Crater-test to solve rust-lang/wg-allocators#1 - [`alloc-wg`](https://github.com/TimDiekmann/alloc-wg)-crate
2 parents da38469 + a600410 commit a1a13b2

File tree

13 files changed

+504
-206
lines changed

13 files changed

+504
-206
lines changed

library/alloc/src/boxed.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1347,9 +1347,10 @@ impl<I> FromIterator<I> for Box<[I]> {
13471347
}
13481348

13491349
#[stable(feature = "box_slice_clone", since = "1.3.0")]
1350-
impl<T: Clone> Clone for Box<[T]> {
1350+
impl<T: Clone, A: AllocRef + Clone> Clone for Box<[T], A> {
13511351
fn clone(&self) -> Self {
1352-
self.to_vec().into_boxed_slice()
1352+
let alloc = Box::alloc_ref(self).clone();
1353+
self.to_vec_in(alloc).into_boxed_slice()
13531354
}
13541355

13551356
fn clone_from(&mut self, other: &Self) {

library/alloc/src/raw_vec.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -233,15 +233,10 @@ impl<T, A: AllocRef> RawVec<T, A> {
233233
}
234234

235235
/// Returns a shared reference to the allocator backing this `RawVec`.
236-
pub fn alloc(&self) -> &A {
236+
pub fn alloc_ref(&self) -> &A {
237237
&self.alloc
238238
}
239239

240-
/// Returns a mutable reference to the allocator backing this `RawVec`.
241-
pub fn alloc_mut(&mut self) -> &mut A {
242-
&mut self.alloc
243-
}
244-
245240
fn current_memory(&self) -> Option<(NonNull<u8>, Layout)> {
246241
if mem::size_of::<T>() == 0 || self.cap == 0 {
247242
None

library/alloc/src/slice.rs

+33-8
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ use core::cmp::Ordering::{self, Less};
8787
use core::mem::{self, size_of};
8888
use core::ptr;
8989

90+
use crate::alloc::{AllocRef, Global};
9091
use crate::borrow::ToOwned;
9192
use crate::boxed::Box;
9293
use 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
139140
mod 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

Comments
 (0)