Skip to content

Commit 20b1238

Browse files
Fixing COOP_PREFERRED (and similar). WIP.
1 parent 4e14196 commit 20b1238

File tree

16 files changed

+174
-142
lines changed

16 files changed

+174
-142
lines changed

library/alloc/src/boxed.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,8 @@ impl<T> Box<[T]> {
641641
#[unstable(feature = "new_uninit", issue = "63291")]
642642
#[must_use]
643643
pub fn new_uninit_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
644-
unsafe { RawVec::with_capacity(len).into_box(len) }
644+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
645+
unsafe { RawVec::<T, Global, false>::with_capacity(len).into_box(len) }
645646
}
646647

647648
/// Constructs a new boxed slice with uninitialized contents, with the memory
@@ -666,7 +667,8 @@ impl<T> Box<[T]> {
666667
#[unstable(feature = "new_uninit", issue = "63291")]
667668
#[must_use]
668669
pub fn new_zeroed_slice(len: usize) -> Box<[mem::MaybeUninit<T>]> {
669-
unsafe { RawVec::with_capacity_zeroed(len).into_box(len) }
670+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
671+
unsafe { RawVec::<T, Global, false>::with_capacity_zeroed(len).into_box(len) }
670672
}
671673

672674
/// Constructs a new boxed slice with uninitialized contents. Returns an error if
@@ -1687,7 +1689,7 @@ impl<T, const N: usize> TryFrom<Box<[T]>> for Box<[T; N]> {
16871689
impl<T, const N: usize, const COOP_PREFERRED: bool> TryFrom<Vec<T, Global, COOP_PREFERRED>>
16881690
for Box<[T; N]>
16891691
where
1690-
[(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
1692+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
16911693
{
16921694
type Error = Vec<T, Global, COOP_PREFERRED>;
16931695

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,15 +1527,15 @@ unsafe impl<T: Ord> TrustedLen for IntoIterSorted<T> {}
15271527
#[derive(Debug)]
15281528
pub struct Drain<'a, T: 'a, const COOP_PREFERRED: bool>
15291529
where
1530-
[(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
1530+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
15311531
{
15321532
iter: vec::Drain<'a, T, Global, COOP_PREFERRED>,
15331533
}
15341534

15351535
#[stable(feature = "drain", since = "1.6.0")]
15361536
impl<T, const COOP_PREFERRED: bool> Iterator for Drain<'_, T, COOP_PREFERRED>
15371537
where
1538-
[(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
1538+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
15391539
{
15401540
type Item = T;
15411541

@@ -1551,22 +1551,31 @@ where
15511551
}
15521552

15531553
#[stable(feature = "drain", since = "1.6.0")]
1554-
impl<T, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, COOP_PREFERRED> {
1554+
impl<T, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, COOP_PREFERRED>
1555+
where
1556+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1557+
{
15551558
#[inline]
15561559
fn next_back(&mut self) -> Option<T> {
15571560
self.iter.next_back()
15581561
}
15591562
}
15601563

15611564
#[stable(feature = "drain", since = "1.6.0")]
1562-
impl<T, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, COOP_PREFERRED> {
1565+
impl<T, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, COOP_PREFERRED>
1566+
where
1567+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1568+
{
15631569
fn is_empty(&self) -> bool {
15641570
self.iter.is_empty()
15651571
}
15661572
}
15671573

15681574
#[stable(feature = "fused", since = "1.26.0")]
1569-
impl<T, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, COOP_PREFERRED> {}
1575+
impl<T, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, COOP_PREFERRED>
1576+
where
1577+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
1578+
{}
15701579

15711580
/// A draining iterator over the elements of a `BinaryHeap`.
15721581
///

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

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct IntoIter<
2626

2727
impl<T, A: Allocator, const COOP_PREFERRED: bool> IntoIter<T, A, COOP_PREFERRED>
2828
where
29-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:,
29+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
3030
{
3131
pub(super) fn new(inner: VecDeque<T, A, COOP_PREFERRED>) -> Self {
3232
IntoIter { inner }
@@ -38,19 +38,19 @@ where
3838
}
3939

4040
#[stable(feature = "collection_debug", since = "1.17.0")]
41-
impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A>
41+
impl<T: fmt::Debug, A: Allocator, const COOP_PREFERRED: bool> fmt::Debug for IntoIter<T, A, COOP_PREFERRED>
4242
where
43-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:,
43+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
4444
{
4545
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
4646
f.debug_tuple("IntoIter").field(&self.inner).finish()
4747
}
4848
}
4949

5050
#[stable(feature = "rust1", since = "1.0.0")]
51-
impl<T, A: Allocator> Iterator for IntoIter<T, A>
51+
impl<T, A: Allocator, const COOP_PREFERRED: bool> Iterator for IntoIter<T, A, COOP_PREFERRED>
5252
where
53-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:,
53+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
5454
{
5555
type Item = T;
5656

@@ -67,9 +67,9 @@ where
6767
}
6868

6969
#[stable(feature = "rust1", since = "1.0.0")]
70-
impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A>
70+
impl<T, A: Allocator, const COOP_PREFERRED: bool> DoubleEndedIterator for IntoIter<T, A, COOP_PREFERRED>
7171
where
72-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:,
72+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
7373
{
7474
#[inline]
7575
fn next_back(&mut self) -> Option<T> {
@@ -78,23 +78,25 @@ where
7878
}
7979

8080
#[stable(feature = "rust1", since = "1.0.0")]
81-
impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A>
81+
impl<T, A: Allocator, const COOP_PREFERRED: bool> ExactSizeIterator for IntoIter<T, A, COOP_PREFERRED>
8282
where
83-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:,
83+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
8484
{
8585
fn is_empty(&self) -> bool {
8686
self.inner.is_empty()
8787
}
8888
}
8989

9090
#[stable(feature = "fused", since = "1.26.0")]
91-
impl<T, A: Allocator> FusedIterator for IntoIter<T, A> where
92-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
91+
impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for IntoIter<T, A, COOP_PREFERRED>
92+
where
93+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
9394
{
9495
}
9596

9697
#[unstable(feature = "trusted_len", issue = "37572")]
97-
unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> where
98-
[(); alloc::co_alloc_metadata_num_slots::<A>()]:
98+
unsafe impl<T, A: Allocator, const COOP_PREFERRED : bool> TrustedLen for IntoIter<T, A, COOP_PREFERRED>
99+
where
100+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
99101
{
100102
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ macro_rules! __impl_slice_eq1 {
44
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_with_preference_specific::<A>(COOP_PREFERRED)]:,
7+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
88
$($constraints)*
99
{
1010
fn eq(&self, other: &$rhs) -> bool {

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

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ where
161161
#[stable(feature = "rust1", since = "1.0.0")]
162162
impl<T, const COOP_PREFERRED: bool> Default for VecDeque<T, Global, COOP_PREFERRED>
163163
where
164-
[(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
164+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
165165
{
166166
/// Creates an empty deque.
167167
#[inline]
@@ -568,7 +568,9 @@ where
568568
#[stable(feature = "rust1", since = "1.0.0")]
569569
#[rustc_const_stable(feature = "const_vec_deque_new", since = "CURRENT_RUSTC_VERSION")]
570570
#[must_use]
571-
pub const fn new() -> VecDeque<T, Global, COOP_PREFERRED> {
571+
pub const fn new() -> VecDeque<T, Global, COOP_PREFERRED>
572+
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
573+
{
572574
// FIXME: This should just be `VecDeque::new_in(Global)` once that hits stable.
573575
VecDeque { head: 0, len: 0, buf: RawVec::NEW }
574576
}
@@ -1403,6 +1405,7 @@ where
14031405
pub fn drain<R>(&mut self, range: R) -> Drain<'_, T, A>
14041406
where
14051407
R: RangeBounds<usize>,
1408+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:,
14061409
{
14071410
// Memory safety
14081411
//
@@ -2799,7 +2802,7 @@ impl<T, A: Allocator, const COOP_PREFERRED: bool> FromIterator<T> for VecDeque<T
27992802
where
28002803
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
28012804
{
2802-
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, COOP_PREFERRED> {
2805+
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, A, COOP_PREFERRED> {
28032806
SpecFromIter::spec_from_iter(iter.into_iter())
28042807
}
28052808
}
@@ -2810,11 +2813,11 @@ where
28102813
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
28112814
{
28122815
type Item = T;
2813-
type IntoIter = IntoIter<T, A>;
2816+
type IntoIter = IntoIter<T, A, COOP_PREFERRED>;
28142817

28152818
/// Consumes the deque into a front-to-back iterator yielding elements by
28162819
/// value.
2817-
fn into_iter(self) -> IntoIter<T, A> {
2820+
fn into_iter(self) -> IntoIter<T, A, COOP_PREFERRED> {
28182821
IntoIter::new(self)
28192822
}
28202823
}
@@ -2900,10 +2903,13 @@ where
29002903
}
29012904

29022905
#[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")]
2903-
impl<T, A: Allocator, const COOP_PREFERRED: bool> From<Vec<T, A, COOP_PREFERRED>>
2906+
impl<T, A: Allocator, const COOP_PREFERRED: bool, const OTHER_COOP_PREFERRED: bool> From<Vec<T, A, OTHER_COOP_PREFERRED>>
29042907
for VecDeque<T, A, COOP_PREFERRED>
29052908
where
29062909
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
2910+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(
2911+
OTHER_COOP_PREFERRED,
2912+
)]:,
29072913
{
29082914
/// Turn a [`Vec<T>`] into a [`VecDeque<T>`].
29092915
///
@@ -2914,22 +2920,19 @@ where
29142920
/// and to not re-allocate the `Vec`'s buffer or allocate
29152921
/// any additional memory.
29162922
#[inline]
2917-
fn from<const OTHER_COOP_PREFERRED: bool>(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self
2918-
where
2919-
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(
2920-
OTHER_COOP_PREFERRED,
2921-
)]:,
2923+
fn from(other: Vec<T, A, OTHER_COOP_PREFERRED>) -> Self
29222924
{
29232925
let (ptr, len, cap, alloc) = other.into_raw_parts_with_alloc();
29242926
Self { head: 0, len, buf: unsafe { RawVec::from_raw_parts_in(ptr, cap, alloc) } }
29252927
}
29262928
}
29272929

29282930
#[stable(feature = "vecdeque_vec_conversions", since = "1.10.0")]
2929-
impl<T, A: Allocator, const COOP_PREFERRED: bool> From<VecDeque<T, A, COOP_PREFERRED>>
2931+
impl<T, A: Allocator, const COOP_PREFERRED: bool, const VECDEQUE_COOP_PREFERRED: bool> From<VecDeque<T, A, VECDEQUE_COOP_PREFERRED>>
29302932
for Vec<T, A, COOP_PREFERRED>
29312933
where
29322934
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
2935+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(VECDEQUE_COOP_PREFERRED)]:,
29332936
{
29342937
/// Turn a [`VecDeque<T>`] into a [`Vec<T>`].
29352938
///
@@ -2960,9 +2963,12 @@ where
29602963
/// assert_eq!(vec, [8, 9, 1, 2, 3, 4]);
29612964
/// assert_eq!(vec.as_ptr(), ptr);
29622965
/// ```
2963-
fn from<const _VECDEQUE_COOP_PREFERRED: bool>(
2964-
mut other: VecDeque<T, A, _VECDEQUE_COOP_PREFERRED>,
2965-
) -> Self {
2966+
fn from(
2967+
mut other: VecDeque<T, A, VECDEQUE_COOP_PREFERRED>,
2968+
) -> Self
2969+
where
2970+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(VECDEQUE_COOP_PREFERRED)]:,
2971+
{
29662972
other.make_contiguous();
29672973

29682974
unsafe {
@@ -2985,7 +2991,7 @@ where
29852991
impl<T, const N: usize, const COOP_PREFERRED: bool> From<[T; N]>
29862992
for VecDeque<T, Global, COOP_PREFERRED>
29872993
where
2988-
[(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
2994+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
29892995
{
29902996
/// Converts a `[T; N]` into a `VecDeque<T>`.
29912997
///

library/alloc/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,6 @@ use crate::alloc::Global;
291291
/// See also `core::alloc::co_alloc_metadata_num_slots_with_preference`.
292292
#[unstable(feature = "global_co_alloc", issue = "none")]
293293
pub const fn co_alloc_metadata_num_slots_with_preference_global(coop_preferred: bool) -> usize {
294-
// FIXME or replace any calls with core::alloc::co_alloc_metadata_num_slots_with_preference_specific::<Global>(COOP_PREFERRED). Then rename co_alloc_metadata_num_slots_with_preference_specific to co_alloc_metadata_num_slots_with_preference. See raw_vec.rs.
294+
// FIXME or replace any calls with core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED). See raw_vec.rs.
295295
if /*Global::IS_CO_ALLOCATOR*/ true && coop_preferred { 1 } else { 0 }
296296
}

library/alloc/src/rc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,7 +1989,7 @@ impl<T: ?Sized> From<Box<T>> for Rc<T> {
19891989
#[stable(feature = "shared_from_slice", since = "1.21.0")]
19901990
impl<T, const COOP_PREFERRED: bool> From<Vec<T, Global, COOP_PREFERRED>> for Rc<[T]>
19911991
where
1992-
[(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
1992+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
19931993
{
19941994
/// Allocate a reference-counted slice and move `v`'s items into it.
19951995
///
@@ -2004,7 +2004,7 @@ where
20042004
#[inline]
20052005
fn from(mut v: Vec<T, Global, COOP_PREFERRED>) -> Rc<[T]>
20062006
where
2007-
[(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
2007+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
20082008
{
20092009
unsafe {
20102010
let rc = Rc::copy_from_slice(&v);

library/alloc/src/str.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ where
136136
T: Copy,
137137
B: AsRef<[T]> + ?Sized,
138138
S: Borrow<B>,
139-
[(); crate::co_alloc_metadata_num_slots_with_preference_global(COOP_PREFERRED)]:,
139+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<Global>(COOP_PREFERRED)]:,
140140
{
141141
let sep_len = sep.len();
142142
let mut iter = slice.iter();

0 commit comments

Comments
 (0)