Skip to content

Commit 27a545d

Browse files
Vec, VecDeque, RawVec etc. with COOP_PREFERRED. WIP: probably NOT compilable. Triggers an internal compiler error from cargo 1.68.0-nightly (f6e737b1e 2022-12-02)
1 parent 7e84033 commit 27a545d

File tree

15 files changed

+339
-311
lines changed

15 files changed

+339
-311
lines changed

library/alloc/src/boxed.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@
146146
147147
#![stable(feature = "rust1", since = "1.0.0")]
148148

149+
use core::alloc;
149150
use core::any::Any;
150151
use core::async_iter::AsyncIterator;
151152
use core::borrow;
@@ -699,7 +700,7 @@ impl<T> Box<[T]> {
699700
Err(_) => return Err(AllocError),
700701
};
701702
let ptr = Global.allocate(layout)?;
702-
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
703+
Ok(RawVec::<T, Global, false>::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
703704
}
704705
}
705706

@@ -731,12 +732,13 @@ impl<T> Box<[T]> {
731732
Err(_) => return Err(AllocError),
732733
};
733734
let ptr = Global.allocate_zeroed(layout)?;
734-
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
735+
Ok(RawVec::<T, Global, false>::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
735736
}
736737
}
737738
}
738739

739-
impl<T, A: Allocator> Box<[T], A> {
740+
impl<T, A: Allocator> Box<[T], A>
741+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
740742
/// Constructs a new boxed slice with uninitialized contents in the provided allocator.
741743
///
742744
/// # Examples
@@ -764,7 +766,7 @@ impl<T, A: Allocator> Box<[T], A> {
764766
// #[unstable(feature = "new_uninit", issue = "63291")]
765767
#[must_use]
766768
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
767-
unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
769+
unsafe { RawVec::<T, A, {alloc::SHORT_TERM_VEC_PREFERS_COOP}>::with_capacity_in(len, alloc).into_box(len) }
768770
}
769771

770772
/// Constructs a new boxed slice with uninitialized contents in the provided allocator,
@@ -792,7 +794,7 @@ impl<T, A: Allocator> Box<[T], A> {
792794
// #[unstable(feature = "new_uninit", issue = "63291")]
793795
#[must_use]
794796
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
795-
unsafe { RawVec::with_capacity_zeroed_in(len, alloc).into_box(len) }
797+
unsafe { RawVec::<T, A, {alloc::SHORT_TERM_VEC_PREFERS_COOP}>::with_capacity_zeroed_in(len, alloc).into_box(len) }
796798
}
797799
}
798800

@@ -2049,7 +2051,8 @@ impl<I> FromIterator<I> for Box<[I]> {
20492051

20502052
#[cfg(not(no_global_oom_handling))]
20512053
#[stable(feature = "box_slice_clone", since = "1.3.0")]
2052-
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
2054+
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A>
2055+
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
20532056
fn clone(&self) -> Self {
20542057
let alloc = Box::allocator(self).clone();
20552058
self.to_vec_in(alloc).into_boxed_slice()

library/alloc/src/collections/binary_heap.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,12 +143,14 @@
143143
#![allow(missing_docs)]
144144
#![stable(feature = "rust1", since = "1.0.0")]
145145

146-
use core::fmt;
146+
use core::{alloc, fmt};
147147
use core::iter::{FromIterator, FusedIterator, InPlaceIterable, SourceIter, TrustedLen};
148148
use core::mem::{self, swap, ManuallyDrop};
149149
use core::ops::{Deref, DerefMut};
150150
use core::ptr;
151151

152+
use crate::alloc::Global;
153+
152154
use crate::collections::TryReserveError;
153155
use crate::slice;
154156
use crate::vec::{self, AsVecIntoIter, Vec};
@@ -1196,7 +1198,7 @@ impl<T> BinaryHeap<T> {
11961198
/// ```
11971199
#[inline]
11981200
#[stable(feature = "drain", since = "1.6.0")]
1199-
pub fn drain(&mut self) -> Drain<'_, T> {
1201+
pub fn drain(&mut self) -> Drain<'_, T, {alloc::SHORT_TERM_VEC_PREFERS_COOP}> {
12001202
Drain { iter: self.data.drain(..) }
12011203
}
12021204

@@ -1476,12 +1478,14 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
14761478
/// [`drain`]: BinaryHeap::drain
14771479
#[stable(feature = "drain", since = "1.6.0")]
14781480
#[derive(Debug)]
1479-
pub struct Drain<'a, T: 'a> {
1480-
iter: vec::Drain<'a, T>,
1481+
pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool>
1482+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: {
1483+
iter: vec::Drain<'a, T, Global, COOP_PREFERRED>,
14811484
}
14821485

14831486
#[stable(feature = "drain", since = "1.6.0")]
1484-
impl<T> Iterator for Drain<'_, T> {
1487+
impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED>
1488+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]: {
14851489
type Item = T;
14861490

14871491
#[inline]
@@ -1496,22 +1500,22 @@ impl<T> Iterator for Drain<'_, T> {
14961500
}
14971501

14981502
#[stable(feature = "drain", since = "1.6.0")]
1499-
impl<T> DoubleEndedIterator for Drain<'_, T> {
1503+
impl<T, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, COOP_PREFERRED> {
15001504
#[inline]
15011505
fn next_back(&mut self) -> Option<T> {
15021506
self.iter.next_back()
15031507
}
15041508
}
15051509

15061510
#[stable(feature = "drain", since = "1.6.0")]
1507-
impl<T> ExactSizeIterator for Drain<'_, T> {
1511+
impl<T, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, COOP_PREFERRED> {
15081512
fn is_empty(&self) -> bool {
15091513
self.iter.is_empty()
15101514
}
15111515
}
15121516

15131517
#[stable(feature = "fused", since = "1.26.0")]
1514-
impl<T> FusedIterator for Drain<'_, T> {}
1518+
impl<T, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, COOP_PREFERRED> {}
15151519

15161520
/// A draining iterator over the elements of a `BinaryHeap`.
15171521
///

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

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@ pub struct Drain<
1919
'a,
2020
T: 'a,
2121
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
22+
const COOP_PREFERRED: bool = {alloc::SHORT_TERM_VEC_PREFERS_COOP}
2223
>
23-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
24+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
2425
// We can't just use a &mut VecDeque<T, A>, as that would make Drain invariant over T
2526
// and we want it to be covariant instead
26-
deque: NonNull<VecDeque<T, A>>,
27+
deque: NonNull<VecDeque<T, A, COOP_PREFERRED>>,
2728
// drain_start is stored in deque.len
2829
drain_len: usize,
2930
// index into the logical array, not the physical one (always lies in [0..deque.len))
@@ -35,10 +36,10 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
3536
_marker: PhantomData<&'a T>,
3637
}
3738

38-
impl<'a, T, A: Allocator> Drain<'a, T, A>
39-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
39+
impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED>
40+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
4041
pub(super) unsafe fn new(
41-
deque: &'a mut VecDeque<T, A>,
42+
deque: &'a mut VecDeque<T, A, COOP_PREFERRED>,
4243
drain_start: usize,
4344
drain_len: usize,
4445
) -> Self {
@@ -90,8 +91,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
9091
}
9192

9293
#[stable(feature = "collection_debug", since = "1.17.0")]
93-
impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A>
94-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
94+
impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for Drain<'_, T, A, COOP_PREFERRED>
95+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
9596
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9697
f.debug_tuple("Drain")
9798
.field(&self.drain_len)
@@ -103,21 +104,21 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
103104
}
104105

105106
#[stable(feature = "drain", since = "1.6.0")]
106-
unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A>
107-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
107+
unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED>
108+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {}
108109
#[stable(feature = "drain", since = "1.6.0")]
109-
unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A>
110-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
110+
unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED>
111+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {}
111112

112113
#[stable(feature = "drain", since = "1.6.0")]
113-
impl<T, A: Allocator> Drop for Drain<'_, T, A>
114-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
114+
impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED>
115+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
115116
fn drop(&mut self) {
116-
struct DropGuard<'r, 'a, T, A: Allocator> (&'r mut Drain<'a, T, A>)
117-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:;
117+
struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> (&'r mut Drain<'a, T, A, COOP_PREFERRED>)
118+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:;
118119

119-
impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A>
120-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
120+
impl<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> Drop for DropGuard<'r, 'a, T, A, COOP_PREFERRED>
121+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
121122
fn drop(&mut self) {
122123
if self.0.remaining != 0 {
123124
unsafe {
@@ -198,8 +199,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
198199
}
199200

200201
#[stable(feature = "drain", since = "1.6.0")]
201-
impl<T, A: Allocator> Iterator for Drain<'_, T, A>
202-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
202+
impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, A, COOP_PREFERRED>
203+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
203204
type Item = T;
204205

205206
#[inline]
@@ -221,8 +222,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
221222
}
222223

223224
#[stable(feature = "drain", since = "1.6.0")]
224-
impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A>
225-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
225+
impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, A, COOP_PREFERRED>
226+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
226227
#[inline]
227228
fn next_back(&mut self) -> Option<T> {
228229
if self.remaining == 0 {
@@ -235,9 +236,9 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
235236
}
236237

237238
#[stable(feature = "drain", since = "1.6.0")]
238-
impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A>
239-
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
239+
impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, A, COOP_PREFERRED>
240+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {}
240241

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
macro_rules! __impl_slice_eq1 {
22
([$($vars:tt)*] $lhs:ty, $rhs:ty, $($constraints:tt)*) => {
33
#[stable(feature = "vec_deque_partial_eq_slice", since = "1.17.0")]
4-
impl<T, U, A: Allocator, $($vars)*> PartialEq<$rhs> for $lhs
4+
impl<T, U, A: Allocator, const COOP_PREFERRED: bool, $($vars)*> PartialEq<$rhs> for $lhs
55
where
66
T: PartialEq<U>,
7-
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:,
7+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
88
$($constraints)*
99
{
1010
fn eq(&self, other: &$rhs) -> bool {

0 commit comments

Comments
 (0)