Skip to content

Commit 5a1f3bd

Browse files
CoAlloc: Separated Vec::from_raw_parts_co and similar (and also in VecDeque). Mostly compiling.
1 parent 5e0d7df commit 5a1f3bd

File tree

5 files changed

+105
-15
lines changed

5 files changed

+105
-15
lines changed

library/alloc/src/collections/vec_deque/mod.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ use self::spec_extend::SpecExtend;
5757

5858
mod spec_extend;
5959

60-
use self::spec_from_iter::SpecFromIter;
60+
use self::spec_from_iter::SpecFromIterCo;
6161

6262
mod spec_from_iter;
6363

@@ -2809,7 +2809,7 @@ where
28092809
[(); {crate::meta_num_slots_global!(CO_ALLOC_PREF)}]:,
28102810
{
28112811
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, CO_ALLOC_PREF> {
2812-
SpecFromIter::spec_from_iter(iter.into_iter())
2812+
SpecFromIterCo::spec_from_iter_co(iter.into_iter())
28132813
}
28142814
}
28152815

@@ -2911,6 +2911,33 @@ where
29112911
}
29122912
}
29132913

2914+
#[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")]
2915+
#[allow(unused_braces)]
2916+
impl<T, A: Allocator, /*const CO_ALLOC_PREF: CoAllocPref,*/ const OTHER_CO_ALLOC_PREF: CoAllocPref>
2917+
From<Vec<T, A, OTHER_CO_ALLOC_PREF>> for VecDeque<T, A>//, CO_ALLOC_PREF>
2918+
where
2919+
//[(); {crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
2920+
[(); {crate::meta_num_slots!(A, OTHER_CO_ALLOC_PREF)}]:,
2921+
{
2922+
/// Turn a [`Vec<T>`] into a [`VecDeque<T>`].
2923+
///
2924+
/// [`Vec<T>`]: crate::vec::Vec
2925+
/// [`VecDeque<T>`]: crate::collections::VecDeque
2926+
///
2927+
/// This conversion is guaranteed to run in *O*(1) time
2928+
/// and to not re-allocate the `Vec`'s buffer or allocate
2929+
/// any additional memory.
2930+
#[inline]
2931+
default fn from(other: Vec<T, A, OTHER_CO_ALLOC_PREF>) -> Self {
2932+
let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc();
2933+
Self {
2934+
head: 0,
2935+
len,
2936+
buf: unsafe { RawVec::<T, A/*, CO_ALLOC_PREF*/>::from_raw_parts_in(ptr, cap, alloc) },
2937+
}
2938+
}
2939+
}
2940+
29142941
#[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")]
29152942
#[allow(unused_braces)]
29162943
impl<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref, const OTHER_CO_ALLOC_PREF: CoAllocPref>
@@ -2928,7 +2955,7 @@ where
29282955
/// and to not re-allocate the `Vec`'s buffer or allocate
29292956
/// any additional memory.
29302957
#[inline]
2931-
fn from(other: Vec<T, A, OTHER_CO_ALLOC_PREF>) -> Self {
2958+
default fn from(other: Vec<T, A, OTHER_CO_ALLOC_PREF>) -> Self {
29322959
let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc();
29332960
Self {
29342961
head: 0,

library/alloc/src/collections/vec_deque/spec_from_iter.rs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,81 @@ pub(super) trait SpecFromIter<T, I> {
77
fn spec_from_iter(iter: I) -> Self;
88
}
99

10+
/// Specialization trait used for `VecDeque::from_iter_co`
11+
pub(super) trait SpecFromIterCo<T, I> {
12+
fn spec_from_iter_co(iter: I) -> Self;
13+
}
14+
1015
#[allow(unused_braces)]
11-
impl<T, I, const CO_ALLOC_PREF: CoAllocPref> SpecFromIter<T, I> for VecDeque<T, Global, CO_ALLOC_PREF>
16+
impl<T, I> SpecFromIter<T, I> for VecDeque<T>
1217
where
1318
I: Iterator<Item = T>,
14-
[(); {crate::meta_num_slots_global!(CO_ALLOC_PREF)}]:,
1519
{
1620
default fn spec_from_iter(iterator: I) -> Self {
1721
// Since converting is O(1) now, just re-use the `Vec` logic for
1822
// anything where we can't do something extra-special for `VecDeque`,
1923
// especially as that could save us some monomorphiziation work
2024
// if one uses the same iterators (like slice ones) with both.
21-
crate::vec::Vec::<T, Global, CO_ALLOC_PREF>::from_iter(iterator).into()
25+
crate::vec::Vec::from_iter(iterator).into()
26+
}
27+
}
28+
29+
#[allow(unused_braces)]
30+
impl<T> SpecFromIter<T, crate::vec::IntoIter<T>>
31+
for VecDeque<T>
32+
{
33+
#[inline]
34+
fn spec_from_iter(iterator: crate::vec::IntoIter<T>) -> Self {
35+
iterator.into_vecdeque()
36+
}
37+
}
38+
39+
#[allow(unused_braces)]
40+
impl<T> SpecFromIter<T, IntoIter<T>>
41+
for VecDeque<T>
42+
{
43+
#[inline]
44+
fn spec_from_iter(iterator: IntoIter<T>) -> Self {
45+
iterator.into_vecdeque()
46+
}
47+
}
48+
// ----
49+
50+
#[allow(unused_braces)]
51+
impl<T, I, const CO_ALLOC_PREF: CoAllocPref> SpecFromIterCo<T, I> for VecDeque<T, Global, CO_ALLOC_PREF>
52+
where
53+
I: Iterator<Item = T>,
54+
[(); {crate::meta_num_slots_global!(CO_ALLOC_PREF)}]:,
55+
{
56+
default fn spec_from_iter_co(iterator: I) -> Self {
57+
// Since converting is O(1) now, just re-use the `Vec` logic for
58+
// anything where we can't do something extra-special for `VecDeque`,
59+
// especially as that could save us some monomorphiziation work
60+
// if one uses the same iterators (like slice ones) with both.
61+
crate::vec::Vec::<T, Global, CO_ALLOC_PREF>::from_iter_co(iterator).into()
2262
}
2363
}
2464

2565
#[allow(unused_braces)]
26-
impl<T, const CO_ALLOC_PREF: CoAllocPref> SpecFromIter<T, crate::vec::IntoIter<T, Global, CO_ALLOC_PREF>>
66+
impl<T, const CO_ALLOC_PREF: CoAllocPref> SpecFromIterCo<T, crate::vec::IntoIter<T, Global, CO_ALLOC_PREF>>
2767
for VecDeque<T, Global, CO_ALLOC_PREF>
2868
where
2969
[(); {crate::meta_num_slots_global!(CO_ALLOC_PREF)}]:,
3070
{
3171
#[inline]
32-
fn spec_from_iter(iterator: crate::vec::IntoIter<T, Global, CO_ALLOC_PREF>) -> Self {
72+
fn spec_from_iter_co(iterator: crate::vec::IntoIter<T, Global, CO_ALLOC_PREF>) -> Self {
3373
iterator.into_vecdeque()
3474
}
3575
}
3676

3777
#[allow(unused_braces)]
38-
impl<T, const CO_ALLOC_PREF: CoAllocPref> SpecFromIter<T, IntoIter<T, Global, CO_ALLOC_PREF>>
78+
impl<T, const CO_ALLOC_PREF: CoAllocPref> SpecFromIterCo<T, IntoIter<T, Global, CO_ALLOC_PREF>>
3979
for VecDeque<T, Global, CO_ALLOC_PREF>
4080
where
4181
[(); {crate::meta_num_slots_global!(CO_ALLOC_PREF)}]:,
4282
{
4383
#[inline]
44-
fn spec_from_iter(iterator: IntoIter<T, Global, CO_ALLOC_PREF>) -> Self {
84+
fn spec_from_iter_co(iterator: IntoIter<T, Global, CO_ALLOC_PREF>) -> Self {
4585
iterator.into_vecdeque()
4686
}
47-
}
87+
}

library/alloc/src/vec/in_place_collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ where
210210
src.forget_allocation_drop_remaining();
211211
mem::forget(dst_guard);
212212

213-
let vec = unsafe { Vec::from_raw_parts(dst_buf, len, cap) };
213+
let vec = unsafe { Vec::from_raw_parts_co(dst_buf, len, cap) };
214214

215215
vec
216216
}

library/alloc/src/vec/mod.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,17 @@ where
588588
Self::with_capacity_in(capacity, Global)
589589
}
590590

591+
/// Coallocation-aware alternative to `from_row_parts`.
592+
#[inline]
593+
#[unstable(feature = "global_co_alloc", issue = "none")]
594+
pub unsafe fn from_raw_parts_co(ptr: *mut T, length: usize, capacity: usize) -> Self {
595+
unsafe { Self::from_raw_parts_in(ptr, length, capacity, Global) }
596+
}
597+
598+
}
599+
600+
impl<T> Vec<T>
601+
{
591602
/// Creates a `Vec<T>` directly from a pointer, a capacity, and a length.
592603
///
593604
/// # Safety
@@ -2906,12 +2917,24 @@ where
29062917
#[cfg(not(no_global_oom_handling))]
29072918
#[stable(feature = "rust1", since = "1.0.0")]
29082919
#[allow(unused_braces)]
2909-
impl<T, const CO_ALLOC_PREF: CoAllocPref> FromIterator<T> for Vec<T, Global, CO_ALLOC_PREF>
2920+
impl<T> FromIterator<T> for Vec<T>
2921+
{
2922+
#[inline]
2923+
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> {
2924+
<Self as SpecFromIter<T, I::IntoIter>>::from_iter(iter.into_iter())
2925+
}
2926+
}
2927+
2928+
#[cfg(not(no_global_oom_handling))]
2929+
#[unstable(feature = "global_co_alloc", issue="none")]
2930+
#[allow(unused_braces)]
2931+
impl<T, const CO_ALLOC_PREF: CoAllocPref> Vec<T, Global, CO_ALLOC_PREF>
29102932
where
29112933
[(); {crate::meta_num_slots_global!(CO_ALLOC_PREF)}]:,
29122934
{
2935+
/// Coallocation-aware alternative to `from_iter`.
29132936
#[inline]
2914-
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T, Global, CO_ALLOC_PREF> {
2937+
pub fn from_iter_co<I: IntoIterator<Item = T>>(iter: I) -> Vec<T, Global, CO_ALLOC_PREF> {
29152938
<Self as SpecFromIter<T, I::IntoIter>>::from_iter(iter.into_iter())
29162939
}
29172940
}

library/alloc/src/vec/spec_from_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ where
5959
if has_advanced {
6060
ptr::copy(it.ptr, it.buf.as_ptr(), it.len());
6161
}
62-
return Vec::from_raw_parts(it.buf.as_ptr(), it.len(), it.cap);
62+
return Vec::from_raw_parts_co(it.buf.as_ptr(), it.len(), it.cap);
6363
}
6464
}
6565

0 commit comments

Comments
 (0)