Skip to content

Commit 18d9361

Browse files
CoAlloc: Removed/split CO_ALLOC_PREF. Fixed tests
1 parent 9fc9cd9 commit 18d9361

File tree

8 files changed

+49
-10
lines changed

8 files changed

+49
-10
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::*;
22
use crate::boxed::Box;
3+
use crate::{CO_ALLOC_PREF_META_YES, CO_ALLOC_PREF_META_NO};
34
use crate::testing::crash_test::{CrashTestDummy, Panic};
45
use core::mem;
56
use std::iter::TrustedLen;
@@ -448,7 +449,10 @@ fn test_extend_specialization() {
448449

449450
#[allow(dead_code)]
450451
fn assert_covariance() {
451-
fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> {
452+
fn drain<'new>(d: Drain<'static, &'static str, {CO_ALLOC_PREF_META_NO!()}>) -> Drain<'new, &'new str, {CO_ALLOC_PREF_META_NO!()}> {
453+
d
454+
}
455+
fn drain_co<'new>(d: Drain<'static, &'static str, {CO_ALLOC_PREF_META_YES!()}>) -> Drain<'new, &'new str, {CO_ALLOC_PREF_META_YES!()}> {
452456
d
453457
}
454458
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,11 +2844,21 @@ where
28442844

28452845
#[stable(feature = "rust1", since = "1.0.0")]
28462846
#[allow(unused_braces)]
2847-
impl<T, const CO_ALLOC_PREF: CoAllocPref> FromIterator<T> for VecDeque<T, Global, CO_ALLOC_PREF>
2847+
impl<T> FromIterator<T> for VecDeque<T>
2848+
{
2849+
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T> {
2850+
SpecFromIterCo::spec_from_iter_co(iter.into_iter())
2851+
}
2852+
}
2853+
2854+
#[unstable(feature = "global_co_alloc", issue = "none")]
2855+
#[allow(unused_braces)]
2856+
impl<T, const CO_ALLOC_PREF: CoAllocPref> VecDeque<T, Global, CO_ALLOC_PREF>
28482857
where
28492858
[(); { crate::meta_num_slots_global!(CO_ALLOC_PREF) }]:,
28502859
{
2851-
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, CO_ALLOC_PREF> {
2860+
/// Like [from_iter], but coallocation-aware.
2861+
pub fn from_iter_co<I: IntoIterator<Item = T>>(iter: I) -> VecDeque<T, Global, CO_ALLOC_PREF> {
28522862
SpecFromIterCo::spec_from_iter_co(iter.into_iter())
28532863
}
28542864
}

library/alloc/src/raw_vec.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ where
281281
&self.alloc
282282
}
283283

284+
#[inline]
285+
const fn assert_alignment() {
286+
assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0);
287+
}
288+
284289
fn current_memory(&self) -> Option<(NonNull<u8>, Layout)> {
285290
if T::IS_ZST || self.cap == 0 {
286291
None
@@ -289,7 +294,8 @@ where
289294
// and could hypothetically handle differences between stride and size, but this memory
290295
// has already been allocated so we know it can't overflow and currently rust does not
291296
// support such types. So we can do better by skipping some checks and avoid an unwrap.
292-
let _: () = const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
297+
let _: () = Self::assert_alignment();
298+
//let _: () = const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
293299
unsafe {
294300
let align = mem::align_of::<T>();
295301
let size = mem::size_of::<T>().unchecked_mul(self.cap);
@@ -483,7 +489,8 @@ where
483489

484490
let (ptr, layout) = if let Some(mem) = self.current_memory() { mem } else { return Ok(()) };
485491
// See current_memory() why this assert is here
486-
let _: () = const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
492+
let _: () = Self::assert_alignment();
493+
//let _: () = const { assert!(mem::size_of::<T>() % mem::align_of::<T>() == 0) };
487494
let ptr = unsafe {
488495
// `Layout::array` cannot overflow here because it would have
489496
// overflowed earlier when capacity was larger.

library/alloc/src/slice.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,17 @@ pub(crate) mod hack {
103103
// `vec!` macro mostly and causes perf regression. See #71204 for
104104
// discussion and perf results.
105105
#[allow(unused_braces)]
106-
pub fn into_vec<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref>(
106+
pub fn into_vec<T, A: Allocator>(
107+
b: Box<[T], A>,
108+
) -> Vec<T, A>
109+
where
110+
[(); { crate::meta_num_slots_default!(A) }]:,
111+
{
112+
into_vec_co::<T, A, {crate::CO_ALLOC_PREF_META_DEFAULT!()}>(b)
113+
}
114+
115+
#[allow(unused_braces)]
116+
pub fn into_vec_co<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref>(
107117
b: Box<[T], A>,
108118
) -> Vec<T, A, CO_ALLOC_PREF>
109119
where
@@ -648,7 +658,7 @@ impl<T> [T] {
648658
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
649659
{
650660
// N.B., see the `hack` module in this file for more details.
651-
hack::into_vec(self)
661+
hack::into_vec_co(self)
652662
}
653663

654664
/// Creates a vector by copying a slice `n` times.

library/alloc/src/vec/into_iter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ where
465465
}
466466
#[cfg(test)]
467467
fn clone(&self) -> Self {
468-
crate::slice::to_vec(self.as_slice(), self.alloc.deref().clone()).into_iter()
468+
crate::slice::to_vec_co(self.as_slice(), self.alloc.deref().clone()).into_iter()
469469
}
470470
}
471471

library/alloc/src/vec/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2864,7 +2864,7 @@ where
28642864
#[cfg(test)]
28652865
fn clone(&self) -> Self {
28662866
let alloc = self.allocator().clone();
2867-
crate::slice::to_vec(&**self, alloc)
2867+
crate::slice::to_vec_co(&**self, alloc)
28682868
}
28692869

28702870
fn clone_from(&mut self, other: &Self) {

library/alloc/tests/autotraits.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use alloc::{CO_ALLOC_PREF_META_YES, CO_ALLOC_PREF_META_NO};
2+
13
fn require_sync<T: Sync>(_: T) {}
24
fn require_send_sync<T: Send + Sync>(_: T) {}
35

@@ -192,7 +194,12 @@ fn test_binary_heap() {
192194
});
193195

194196
require_send_sync(async {
195-
let _v = None::<alloc::collections::binary_heap::Drain<'_, &u32>>;
197+
let _v = None::<alloc::collections::binary_heap::Drain<'_, &u32, {CO_ALLOC_PREF_META_NO!()}>>;
198+
async {}.await;
199+
});
200+
201+
require_send_sync(async {
202+
let _v = None::<alloc::collections::binary_heap::Drain<'_, &u32, {CO_ALLOC_PREF_META_YES!()}>>;
196203
async {}.await;
197204
});
198205

library/alloc/tests/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![feature(alloc_layout_extra)]
33
#![feature(assert_matches)]
44
#![feature(btree_drain_filter)]
5+
#![feature(global_co_alloc_meta)]
56
#![feature(cow_is_borrowed)]
67
#![feature(const_box)]
78
#![feature(const_convert)]

0 commit comments

Comments
 (0)