Skip to content

Commit 546d076

Browse files
Vec & VecDeque use cases have COOP_PREFERRED. NOT COMPILABLE.
1 parent 61e07f5 commit 546d076

File tree

9 files changed

+63
-54
lines changed

9 files changed

+63
-54
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#![feature(global_co_alloc)]
99

1010
#![stable(feature = "rust1", since = "1.0.0")]
11-
use core::alloc;
11+
use core::alloc::{self, GlobalAlloc};
1212
use core::cmp::{self, Ordering};
1313
use core::fmt;
1414
use core::hash::{Hash, Hasher};
@@ -2756,8 +2756,9 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE
27562756
}
27572757

27582758
#[stable(feature = "rust1", since = "1.0.0")]
2759-
impl<T> FromIterator<T> for VecDeque<T> {
2760-
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T> {
2759+
impl<T, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T, GlobalAlloc, COOP_PREFERRED>
2760+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
2761+
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, COOP_PREFERRED> {
27612762
SpecFromIter::spec_from_iter(iter.into_iter())
27622763
}
27632764
}
@@ -2853,7 +2854,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE
28532854
/// conversion. This isn't yet a guarantee though, and
28542855
/// shouldn't be relied on.
28552856
#[inline]
2856-
fn from(other: Vec<T, A>) -> Self {
2857+
fn from<const OTHER_COOP_PREFERRED: bool>(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self
2858+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(OTHER_COOP_PREFERRED)]: {
28572859
let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc();
28582860
Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } }
28592861
}
@@ -2911,7 +2913,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE
29112913
}
29122914

29132915
#[stable(feature = "std_collections_from_array", since = "1.56.0")]
2914-
impl<T, const N: usize> From<[T; N]> for VecDeque<T> {
2916+
impl<T, const N: usize, const COOP_PREFERRED: bool> From<[T; N]> for VecDeque<T, Global, COOP_PREFERRED>
2917+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
29152918
/// Converts a `[T; N]` into a `VecDeque<T>`.
29162919
///
29172920
/// ```

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
use super::{IntoIter, VecDeque};
2+
use crate::alloc::Allocator;
3+
use core::alloc;
24

35
/// Specialization trait used for `VecDeque::from_iter`
46
pub(super) trait SpecFromIter<T, I> {
57
fn spec_from_iter(iter: I) -> Self;
68
}
79

8-
impl<T, I> SpecFromIter<T, I> for VecDeque<T>
10+
impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, I> for VecDeque<T, A, COOP_PREFERRED>
911
where
1012
I: Iterator<Item = T>,
13+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
1114
{
1215
default fn spec_from_iter(iterator: I) -> Self {
1316
// Since converting is O(1) now, just re-use the `Vec` logic for
@@ -18,14 +21,16 @@ where
1821
}
1922
}
2023

21-
impl<T> SpecFromIter<T, crate::vec::IntoIter<T>> for VecDeque<T> {
24+
impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, crate::vec::IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED>
25+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
2226
#[inline]
2327
fn spec_from_iter(iterator: crate::vec::IntoIter<T>) -> Self {
2428
iterator.into_vecdeque()
2529
}
2630
}
2731

28-
impl<T> SpecFromIter<T, IntoIter<T>> for VecDeque<T> {
32+
impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecFromIter<T, IntoIter<T>> for VecDeque<T, A, COOP_PREFERRED>
33+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
2934
#[inline]
3035
fn spec_from_iter(iterator: IntoIter<T>) -> Self {
3136
iterator.into_vecdeque()

library/alloc/src/raw_vec.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE
6262
pub(crate) metas: [GlobalCoAllocMeta; alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)],
6363
}
6464

65-
impl<T> RawVec<T, Global> {
65+
impl<T, const COOP_PREFERRED: bool> RawVec<T, Global, COOP_PREFERRED>
66+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: {
6667
/// HACK(Centril): This exists because stable `const fn` can only call stable `const fn`, so
6768
/// they cannot call `Self::new()`.
6869
///

library/alloc/src/slice.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ pub(crate) mod hack {
9292
// We shouldn't add inline attribute to this since this is used in
9393
// `vec!` macro mostly and causes perf regression. See #71204 for
9494
// discussion and perf results.
95-
pub fn into_vec<T, A: Allocator>(b: Box<[T], A>) -> Vec<T, A>
96-
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
95+
pub fn into_vec<T, A: Allocator, const COOP_PREFERRED: bool>(b: Box<[T], A>) -> Vec<T, A, COOP_PREFERRED>
96+
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
9797
unsafe {
9898
let len = b.len();
9999
let (b, alloc) = Box::into_raw_with_allocator(b);
@@ -103,31 +103,31 @@ pub(crate) mod hack {
103103

104104
#[cfg(not(no_global_oom_handling))]
105105
#[inline]
106-
pub fn to_vec<T: ConvertVec, A: Allocator>(s: &[T], alloc: A) -> Vec<T, A>
107-
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
106+
pub fn to_vec<T: ConvertVec, A: Allocator, const COOP_PREFERRED: bool>(s: &[T], alloc: A) -> Vec<T, A, COOP_PREFERRED>
107+
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
108108
T::to_vec(s, alloc)
109109
}
110110

111111
#[cfg(not(no_global_oom_handling))]
112112
pub trait ConvertVec {
113-
fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A>
113+
fn to_vec<A: Allocator, const COOP_PREFERRED: bool>(s: &[Self], alloc: A) -> Vec<Self, A, COOP_PREFERRED>
114114
where
115115
Self: Sized,
116-
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:;
116+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:;
117117
}
118118

119119
#[cfg(not(no_global_oom_handling))]
120120
impl<T: Clone> ConvertVec for T {
121121
#[inline]
122-
default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A>
123-
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
124-
struct DropGuard<'a, T, A: Allocator>
125-
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
126-
vec: &'a mut Vec<T, A>,
122+
default fn to_vec<A: Allocator, const COOP_PREFERRED: bool>(s: &[Self], alloc: A) -> Vec<Self, A, COOP_PREFERRED>
123+
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
124+
struct DropGuard<'a, T, A: Allocator, const COOP_PREFERRED: bool>
125+
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
126+
vec: &'a mut Vec<T, A, COOP_PREFERRED>,
127127
num_init: usize,
128128
}
129-
impl<'a, T, A: Allocator> Drop for DropGuard<'a, T, A>
130-
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
129+
impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'a, T, A, COOP_PREFERRED>
130+
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
131131
#[inline]
132132
fn drop(&mut self) {
133133
// SAFETY:
@@ -159,8 +159,8 @@ pub(crate) mod hack {
159159
#[cfg(not(no_global_oom_handling))]
160160
impl<T: Copy> ConvertVec for T {
161161
#[inline]
162-
fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A>
163-
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
162+
fn to_vec<A: Allocator, const COOP_PREFERRED: bool>(s: &[Self], alloc: A) -> Vec<Self, A, COOP_PREFERRED>
163+
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
164164
let mut v = Vec::with_capacity_in(s.len(), alloc);
165165
// SAFETY:
166166
// allocated above with the capacity of `s`, and initialize to `s.len()` in

library/alloc/src/vec/drain.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE
3232
pub(super) tail_len: usize,
3333
/// Current remaining range to remove
3434
pub(super) iter: slice::Iter<'a, T>,
35-
pub(super) vec: NonNull<Vec<T, A>>,
35+
pub(super) vec: NonNull<Vec<T, A, COOP_PREFERRED>>,
3636
}
3737

3838
#[stable(feature = "collection_debug", since = "1.17.0")]

library/alloc/src/vec/into_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRE
144144

145145
#[cfg(not(no_global_oom_handling))]
146146
#[inline]
147-
pub(crate) fn into_vecdeque(self) -> VecDeque<T, A> {
147+
pub(crate) fn into_vecdeque(self) -> VecDeque<T, A, COOP_PREFERRED> {
148148
// Keep our `Drop` impl from dropping the elements and the allocator
149149
let mut this = ManuallyDrop::new(self);
150150

library/alloc/src/vec/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2478,7 +2478,7 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR
24782478
/// assert_eq!(flattened.pop(), Some(6));
24792479
/// ```
24802480
#[unstable(feature = "slice_flatten", issue = "95629")]
2481-
pub fn into_flattened(self) -> Vec<T, A> {
2481+
pub fn into_flattened(self) -> Vec<T, A, COOP_PREFERRED> {
24822482
let (ptr, len, cap, alloc) = self.into_raw_parts_with_alloc();
24832483
let (new_len, new_cap) = if T::IS_ZST {
24842484
(len.checked_mul(N).expect("vec len overflow"), usize::MAX)
@@ -3124,17 +3124,17 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR
31243124
}
31253125

31263126
#[stable(feature = "rust1", since = "1.0.0")]
3127-
impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<Vec<T, A>> for Vec<T, A, COOP_PREFERRED>
3127+
impl<T, A: Allocator, const COOP_PREFERRED: bool> AsRef<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED>
31283128
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
3129-
fn as_ref(&self) -> &Vec<T, A> {
3129+
fn as_ref(&self) -> &Vec<T, A, COOP_PREFERRED> {
31303130
self
31313131
}
31323132
}
31333133

31343134
#[stable(feature = "vec_as_mut", since = "1.5.0")]
3135-
impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<Vec<T, A>> for Vec<T, A, COOP_PREFERRED>
3135+
impl<T, A: Allocator, const COOP_PREFERRED: bool> AsMut<Vec<T, A, COOP_PREFERRED>> for Vec<T, A, COOP_PREFERRED>
31363136
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
3137-
fn as_mut(&mut self) -> &mut Vec<T, A> {
3137+
fn as_mut(&mut self) -> &mut Vec<T, A, COOP_PREFERRED> {
31383138
self
31393139
}
31403140
}
@@ -3286,7 +3286,7 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR
32863286
///
32873287
/// assert_eq!(Box::from(vec), vec![1, 2, 3].into_boxed_slice());
32883288
/// ```
3289-
fn from(v: Vec<T, A>) -> Self {
3289+
fn from(v: Vec<T, A, COOP_PREFERRED>) -> Self {
32903290
v.into_boxed_slice()
32913291
}
32923292
}
@@ -3309,7 +3309,7 @@ impl From<&str> for Vec<u8> {
33093309
#[stable(feature = "array_try_from_vec", since = "1.48.0")]
33103310
impl<T, A: Allocator, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, A, COOP_PREFERRED>> for [T; N]
33113311
where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
3312-
type Error = Vec<T, A>;
3312+
type Error = Vec<T, A, COOP_PREFERRED>;
33133313

33143314
/// Gets the entire contents of the `Vec<T>` as an array,
33153315
/// if its size exactly matches that of the requested array.
@@ -3337,7 +3337,7 @@ where [(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PR
33373337
/// assert_eq!(a, b' ');
33383338
/// assert_eq!(b, b'd');
33393339
/// ```
3340-
fn try_from(mut vec: Vec<T, A>) -> Result<[T; N], Vec<T, A>> {
3340+
fn try_from(mut vec: Vec<T, A, COOP_PREFERRED>) -> Result<[T; N], Vec<T, A, COOP_PREFERRED>> {
33413341
if vec.len() != N {
33423342
return Err(vec);
33433343
}

library/alloc/src/vec/spec_extend.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,28 @@ pub(super) trait SpecExtend<T, I> {
1010
fn spec_extend(&mut self, iter: I);
1111
}
1212

13-
impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
13+
impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED>
1414
where
1515
I: Iterator<Item = T>,
16-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
16+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
1717
{
1818
default fn spec_extend(&mut self, iter: I) {
1919
self.extend_desugared(iter)
2020
}
2121
}
2222

23-
impl<T, I, A: Allocator> SpecExtend<T, I> for Vec<T, A>
23+
impl<T, I, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, I> for Vec<T, A, COOP_PREFERRED>
2424
where
2525
I: TrustedLen<Item = T>,
26-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
26+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
2727
{
2828
default fn spec_extend(&mut self, iterator: I) {
2929
self.extend_trusted(iterator)
3030
}
3131
}
3232

33-
impl<T, A: Allocator> SpecExtend<T, IntoIter<T>> for Vec<T, A>
34-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
33+
impl<T, A: Allocator, const COOP_PREFERRED: bool> SpecExtend<T, IntoIter<T>> for Vec<T, A, COOP_PREFERRED>
34+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
3535
fn spec_extend(&mut self, mut iterator: IntoIter<T>) {
3636
unsafe {
3737
self.append_elements(iterator.as_slice() as _);
@@ -40,21 +40,21 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
4040
}
4141
}
4242

43-
impl<'a, T: 'a, I, A: Allocator + 'a> SpecExtend<&'a T, I> for Vec<T, A>
43+
impl<'a, T: 'a, I, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, I> for Vec<T, A, COOP_PREFERRED>
4444
where
4545
I: Iterator<Item = &'a T>,
4646
T: Clone,
47-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
47+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
4848
{
4949
default fn spec_extend(&mut self, iterator: I) {
5050
self.spec_extend(iterator.cloned())
5151
}
5252
}
5353

54-
impl<'a, T: 'a, A: Allocator + 'a> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A>
54+
impl<'a, T: 'a, A: Allocator + 'a, const COOP_PREFERRED: bool> SpecExtend<&'a T, slice::Iter<'a, T>> for Vec<T, A, COOP_PREFERRED>
5555
where
5656
T: Copy,
57-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
57+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:
5858
{
5959
fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) {
6060
let slice = iterator.as_slice();

library/alloc/src/vec/spec_from_elem.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use super::{ExtendElement, IsZero, Vec};
88

99
// Specialization trait used for Vec::from_elem
1010
pub(super) trait SpecFromElem: Sized {
11-
fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>
12-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: ;
11+
fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: Self, n: usize, alloc: A) -> Vec<Self, A, COOP_PREFERRED>
12+
where [(); alloc::co_alloc_metadata_num_slots::<A>(COOP_PREFERRED)]: ;
1313
}
1414

1515
impl<T: Clone> SpecFromElem for T {
16-
default fn from_elem<A: Allocator>(elem: Self, n: usize, alloc: A) -> Vec<Self, A>
17-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
16+
default fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: Self, n: usize, alloc: A) -> Vec<Self, A, COOP_PREFERRED>
17+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
1818
let mut v = Vec::with_capacity_in(n, alloc);
1919
v.extend_with(n, ExtendElement(elem));
2020
v
@@ -23,8 +23,8 @@ impl<T: Clone> SpecFromElem for T {
2323

2424
impl<T: Clone + IsZero> SpecFromElem for T {
2525
#[inline]
26-
default fn from_elem<A: Allocator>(elem: T, n: usize, alloc: A) -> Vec<T, A>
27-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
26+
default fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: T, n: usize, alloc: A) -> Vec<T, A, COOP_PREFERRED>
27+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
2828
if elem.is_zero() {
2929
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
3030
}
@@ -36,8 +36,8 @@ impl<T: Clone + IsZero> SpecFromElem for T {
3636

3737
impl SpecFromElem for i8 {
3838
#[inline]
39-
fn from_elem<A: Allocator>(elem: i8, n: usize, alloc: A) -> Vec<i8, A>
40-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
39+
fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: i8, n: usize, alloc: A) -> Vec<i8, A, COOP_PREFERRED>
40+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
4141
if elem == 0 {
4242
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
4343
}
@@ -52,8 +52,8 @@ impl SpecFromElem for i8 {
5252

5353
impl SpecFromElem for u8 {
5454
#[inline]
55-
fn from_elem<A: Allocator>(elem: u8, n: usize, alloc: A) -> Vec<u8, A>
56-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
55+
fn from_elem<A: Allocator, const COOP_PREFERRED: bool>(elem: u8, n: usize, alloc: A) -> Vec<u8, A, COOP_PREFERRED>
56+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
5757
if elem == 0 {
5858
return Vec { buf: RawVec::with_capacity_zeroed_in(n, alloc), len: n };
5959
}

0 commit comments

Comments
 (0)