Skip to content

Commit e0004d9

Browse files
Vec, VecDeque, RawVec with COOP_PREFERRED. WIP: probably NOT compilable.
1 parent a9f5e3f commit e0004d9

File tree

14 files changed

+209
-96
lines changed

14 files changed

+209
-96
lines changed

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

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::iter::FusedIterator;
22
use core::marker::PhantomData;
33
use core::mem::{self, SizedTypeProperties};
44
use core::ptr::NonNull;
5-
use core::{fmt, ptr};
5+
use core::{alloc, fmt, ptr};
66

77
use crate::alloc::{Allocator, Global};
88

@@ -19,7 +19,8 @@ pub struct Drain<
1919
'a,
2020
T: 'a,
2121
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
22-
> {
22+
>
23+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
2324
// We can't just use a &mut VecDeque<T, A>, as that would make Drain invariant over T
2425
// and we want it to be covariant instead
2526
deque: NonNull<VecDeque<T, A>>,
@@ -34,7 +35,8 @@ pub struct Drain<
3435
_marker: PhantomData<&'a T>,
3536
}
3637

37-
impl<'a, T, A: Allocator> Drain<'a, T, A> {
38+
impl<'a, T, A: Allocator> Drain<'a, T, A>
39+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
3840
pub(super) unsafe fn new(
3941
deque: &'a mut VecDeque<T, A>,
4042
drain_start: usize,
@@ -88,7 +90,8 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> {
8890
}
8991

9092
#[stable(feature = "collection_debug", since = "1.17.0")]
91-
impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
93+
impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A>
94+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
9295
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9396
f.debug_tuple("Drain")
9497
.field(&self.drain_len)
@@ -100,16 +103,21 @@ impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
100103
}
101104

102105
#[stable(feature = "drain", since = "1.6.0")]
103-
unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A> {}
106+
unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A>
107+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
104108
#[stable(feature = "drain", since = "1.6.0")]
105-
unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A> {}
109+
unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A>
110+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
106111

107112
#[stable(feature = "drain", since = "1.6.0")]
108-
impl<T, A: Allocator> Drop for Drain<'_, T, A> {
113+
impl<T, A: Allocator> Drop for Drain<'_, T, A>
114+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
109115
fn drop(&mut self) {
110-
struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>);
116+
struct DropGuard<'r, 'a, T, A: Allocator> (&'r mut Drain<'a, T, A>)
117+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:;
111118

112-
impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> {
119+
impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A>
120+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
113121
fn drop(&mut self) {
114122
if self.0.remaining != 0 {
115123
unsafe {
@@ -190,7 +198,8 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
190198
}
191199

192200
#[stable(feature = "drain", since = "1.6.0")]
193-
impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
201+
impl<T, A: Allocator> Iterator for Drain<'_, T, A>
202+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
194203
type Item = T;
195204

196205
#[inline]
@@ -212,7 +221,8 @@ impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
212221
}
213222

214223
#[stable(feature = "drain", since = "1.6.0")]
215-
impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
224+
impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A>
225+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
216226
#[inline]
217227
fn next_back(&mut self) -> Option<T> {
218228
if self.remaining == 0 {
@@ -225,7 +235,9 @@ impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
225235
}
226236

227237
#[stable(feature = "drain", since = "1.6.0")]
228-
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {}
238+
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A>
239+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
229240

230241
#[stable(feature = "fused", since = "1.26.0")]
231-
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}
242+
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A>
243+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}

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

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::fmt;
1+
use core::{alloc, fmt};
22
use core::iter::{FusedIterator, TrustedLen};
33

44
use crate::alloc::{Allocator, Global};
@@ -17,11 +17,13 @@ use super::VecDeque;
1717
pub struct IntoIter<
1818
T,
1919
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
20-
> {
20+
>
21+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
2122
inner: VecDeque<T, A>,
2223
}
2324

24-
impl<T, A: Allocator> IntoIter<T, A> {
25+
impl<T, A: Allocator> IntoIter<T, A>
26+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
2527
pub(super) fn new(inner: VecDeque<T, A>) -> Self {
2628
IntoIter { inner }
2729
}
@@ -32,14 +34,16 @@ impl<T, A: Allocator> IntoIter<T, A> {
3234
}
3335

3436
#[stable(feature = "collection_debug", since = "1.17.0")]
35-
impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> {
37+
impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A>
38+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
3639
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3740
f.debug_tuple("IntoIter").field(&self.inner).finish()
3841
}
3942
}
4043

4144
#[stable(feature = "rust1", since = "1.0.0")]
42-
impl<T, A: Allocator> Iterator for IntoIter<T, A> {
45+
impl<T, A: Allocator> Iterator for IntoIter<T, A>
46+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
4347
type Item = T;
4448

4549
#[inline]
@@ -55,22 +59,26 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
5559
}
5660

5761
#[stable(feature = "rust1", since = "1.0.0")]
58-
impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
62+
impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A>
63+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
5964
#[inline]
6065
fn next_back(&mut self) -> Option<T> {
6166
self.inner.pop_back()
6267
}
6368
}
6469

6570
#[stable(feature = "rust1", since = "1.0.0")]
66-
impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
71+
impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A>
72+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
6773
fn is_empty(&self) -> bool {
6874
self.inner.is_empty()
6975
}
7076
}
7177

7278
#[stable(feature = "fused", since = "1.26.0")]
73-
impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}
79+
impl<T, A: Allocator> FusedIterator for IntoIter<T, A>
80+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
7481

7582
#[unstable(feature = "trusted_len", issue = "37572")]
76-
unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {}
83+
unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A>
84+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
456456
written: usize,
457457
}
458458

459-
impl<'a, T, A: Allocator> Drop for Guard<'a, T, A> {
459+
impl<'a, T, A: Allocator> Drop for Guard<'a, T, A>
460+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
460461
fn drop(&mut self) {
461462
self.deque.len += self.written;
462463
}

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
#![feature(min_specialization)]
2+
13
use crate::alloc::Allocator;
24
use crate::vec;
5+
use core::alloc;
36
use core::iter::TrustedLen;
47
use core::slice;
58

@@ -13,6 +16,7 @@ pub(super) trait SpecExtend<T, I> {
1316
impl<T, I, A: Allocator> SpecExtend<T, I> for VecDeque<T, A>
1417
where
1518
I: Iterator<Item = T>,
19+
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
1620
{
1721
default fn spec_extend(&mut self, mut iter: I) {
1822
// This function should be the moral equivalent of:
@@ -22,7 +26,8 @@ where
2226
// }
2327

2428
// May only be called if `deque.len() < deque.capacity()`
25-
unsafe fn push_unchecked<T, A: Allocator>(deque: &mut VecDeque<T, A>, element: T) {
29+
unsafe fn push_unchecked<T, A: Allocator>(deque: &mut VecDeque<T, A>, element: T)
30+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
2631
// SAFETY: Because of the precondition, it's guaranteed that there is space
2732
// in the logical array after the last element.
2833
unsafe { deque.buffer_write(deque.to_physical_idx(deque.len), element) };
@@ -52,6 +57,7 @@ where
5257
impl<T, I, A: Allocator> SpecExtend<T, I> for VecDeque<T, A>
5358
where
5459
I: TrustedLen<Item = T>,
60+
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
5561
{
5662
default fn spec_extend(&mut self, iter: I) {
5763
// This is the case for a TrustedLen iterator.
@@ -84,7 +90,8 @@ where
8490
}
8591
}
8692

87-
impl<T, A: Allocator> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A> {
93+
impl<T, A: Allocator> SpecExtend<T, vec::IntoIter<T>> for VecDeque<T, A>
94+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
8895
fn spec_extend(&mut self, mut iterator: vec::IntoIter<T>) {
8996
let slice = iterator.as_slice();
9097
self.reserve(slice.len());
@@ -101,6 +108,7 @@ impl<'a, T: 'a, I, A: Allocator> SpecExtend<&'a T, I> for VecDeque<T, A>
101108
where
102109
I: Iterator<Item = &'a T>,
103110
T: Copy,
111+
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
104112
{
105113
default fn spec_extend(&mut self, iterator: I) {
106114
self.spec_extend(iterator.copied())
@@ -110,6 +118,7 @@ where
110118
impl<'a, T: 'a, A: Allocator> SpecExtend<&'a T, slice::Iter<'a, T>> for VecDeque<T, A>
111119
where
112120
T: Copy,
121+
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
113122
{
114123
fn spec_extend(&mut self, iterator: slice::Iter<'a, T>) {
115124
let slice = iterator.as_slice();

library/alloc/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,12 @@
182182
#![feature(global_co_alloc)]
183183
#![feature(hashmap_internals)]
184184
#![feature(lang_items)]
185-
#![feature(min_specialization)]
185+
// When we used min_specialization instead of specialization, library/alloc/src/vec/mod.rs was failing with:
186+
// - cannot specialize on predicate `the constant `core::alloc::co_alloc_metadata_num_slots::<A>()` can be evaluated`
187+
// - cannot specialize on predicate `[(); _] well-formed`
188+
// - cannot specialize on predicate `the constant `core::alloc::co_alloc_metadata_num_slots::<A>()` can be evaluated`
189+
//#![feature(min_specialization)]
190+
#![feature(specialization)]
186191
#![feature(negative_impls)]
187192
#![feature(never_type)]
188193
#![feature(rustc_allow_const_fn_unstable)]

library/alloc/src/slice.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,13 @@ pub(crate) mod hack {
121121
#[inline]
122122
default fn to_vec<A: Allocator>(s: &[Self], alloc: A) -> Vec<Self, A>
123123
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
124-
struct DropGuard<'a, T, A: Allocator> {
124+
struct DropGuard<'a, T, A: Allocator>
125+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
125126
vec: &'a mut Vec<T, A>,
126127
num_init: usize,
127128
}
128-
impl<'a, T, A: Allocator> Drop for DropGuard<'a, T, A> {
129+
impl<'a, T, A: Allocator> Drop for DropGuard<'a, T, A>
130+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
129131
#[inline]
130132
fn drop(&mut self) {
131133
// SAFETY:

library/alloc/src/vec/drain.rs

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::fmt;
33
use core::iter::{FusedIterator, TrustedLen};
44
use core::mem::{self, ManuallyDrop, SizedTypeProperties};
55
use core::ptr::{self, NonNull};
6-
use core::slice::{self};
6+
use core::{alloc, slice};
77

88
use super::Vec;
99

@@ -23,7 +23,8 @@ pub struct Drain<
2323
'a,
2424
T: 'a,
2525
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global,
26-
> {
26+
>
27+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
2728
/// Index of tail to preserve
2829
pub(super) tail_start: usize,
2930
/// Length of tail
@@ -34,13 +35,15 @@ pub struct Drain<
3435
}
3536

3637
#[stable(feature = "collection_debug", since = "1.17.0")]
37-
impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
38+
impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A>
39+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
3840
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3941
f.debug_tuple("Drain").field(&self.iter.as_slice()).finish()
4042
}
4143
}
4244

43-
impl<'a, T, A: Allocator> Drain<'a, T, A> {
45+
impl<'a, T, A: Allocator> Drain<'a, T, A>
46+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
4447
/// Returns the remaining items of this iterator as a slice.
4548
///
4649
/// # Examples
@@ -139,19 +142,23 @@ impl<'a, T, A: Allocator> Drain<'a, T, A> {
139142
}
140143

141144
#[stable(feature = "vec_drain_as_slice", since = "1.46.0")]
142-
impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A> {
145+
impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A>
146+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
143147
fn as_ref(&self) -> &[T] {
144148
self.as_slice()
145149
}
146150
}
147151

148152
#[stable(feature = "drain", since = "1.6.0")]
149-
unsafe impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A> {}
153+
unsafe impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A>
154+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
150155
#[stable(feature = "drain", since = "1.6.0")]
151-
unsafe impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A> {}
156+
unsafe impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A>
157+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
152158

153159
#[stable(feature = "drain", since = "1.6.0")]
154-
impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
160+
impl<T, A: Allocator> Iterator for Drain<'_, T, A>
161+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
155162
type Item = T;
156163

157164
#[inline]
@@ -165,20 +172,24 @@ impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
165172
}
166173

167174
#[stable(feature = "drain", since = "1.6.0")]
168-
impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
175+
impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A>
176+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
169177
#[inline]
170178
fn next_back(&mut self) -> Option<T> {
171179
self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) })
172180
}
173181
}
174182

175183
#[stable(feature = "drain", since = "1.6.0")]
176-
impl<T, A: Allocator> Drop for Drain<'_, T, A> {
184+
impl<T, A: Allocator> Drop for Drain<'_, T, A>
185+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
177186
fn drop(&mut self) {
178187
/// Moves back the un-`Drain`ed elements to restore the original `Vec`.
179-
struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>);
188+
struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>)
189+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: ;
180190

181-
impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> {
191+
impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A>
192+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
182193
fn drop(&mut self) {
183194
if self.0.tail_len > 0 {
184195
unsafe {
@@ -242,14 +253,17 @@ impl<T, A: Allocator> Drop for Drain<'_, T, A> {
242253
}
243254

244255
#[stable(feature = "drain", since = "1.6.0")]
245-
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {
256+
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A>
257+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
246258
fn is_empty(&self) -> bool {
247259
self.iter.is_empty()
248260
}
249261
}
250262

251263
#[unstable(feature = "trusted_len", issue = "37572")]
252-
unsafe impl<T, A: Allocator> TrustedLen for Drain<'_, T, A> {}
264+
unsafe impl<T, A: Allocator> TrustedLen for Drain<'_, T, A>
265+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
253266

254267
#[stable(feature = "fused", since = "1.26.0")]
255-
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}
268+
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A>
269+
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}

0 commit comments

Comments
 (0)