Skip to content

Commit e3a5fb6

Browse files
Fixing A and COOP_PREFERRED generics. WIP.
1 parent 20b1238 commit e3a5fb6

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

library/alloc/src/boxed.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -778,7 +778,10 @@ where
778778
// #[unstable(feature = "new_uninit", issue = "63291")]
779779
#[must_use]
780780
#[allow(unused_braces)]
781-
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
781+
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A>
782+
where
783+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:,
784+
{
782785
unsafe {
783786
RawVec::<T, A, { SHORT_TERM_VEC_PREFERS_COOP!() }>::with_capacity_in(len, alloc)
784787
.into_box(len)
@@ -810,7 +813,10 @@ where
810813
// #[unstable(feature = "new_uninit", issue = "63291")]
811814
#[must_use]
812815
#[allow(unused_braces)]
813-
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
816+
pub fn new_zeroed_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A>
817+
where
818+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(SHORT_TERM_VEC_PREFERS_COOP!())]:,
819+
{
814820
unsafe {
815821
RawVec::<T, A, { SHORT_TERM_VEC_PREFERS_COOP!() }>::with_capacity_zeroed_in(
816822
len, alloc,
@@ -1521,7 +1527,8 @@ impl<T: Copy> From<&[T]> for Box<[T]> {
15211527
/// ```
15221528
fn from(slice: &[T]) -> Box<[T]> {
15231529
let len = slice.len();
1524-
let buf = RawVec::with_capacity(len);
1530+
// false = no need for co-alloc metadata, since it would get lost once converted to Box.
1531+
let buf = RawVec::<T, Global, false>::with_capacity(len);
15251532
unsafe {
15261533
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
15271534
buf.into_box(slice.len()).assume_init()
@@ -2050,11 +2057,12 @@ impl<I> FromIterator<I> for Box<[I]> {
20502057
#[stable(feature = "box_slice_clone", since = "1.3.0")]
20512058
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A>
20522059
where
2053-
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:,
2060+
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(false)]:,
20542061
{
20552062
fn clone(&self) -> Self {
20562063
let alloc = Box::allocator(self).clone();
2057-
self.to_vec_in(alloc).into_boxed_slice()
2064+
// false = no need for co-alloc metadata, since it would get lost once converted to the boxed slice.
2065+
self.to_vec_in::<A, false>(alloc).into_boxed_slice()
20582066
}
20592067

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ where
1818
// anything where we can't do something extra-special for `VecDeque`,
1919
// especially as that could save us some monomorphiziation work
2020
// if one uses the same iterators (like slice ones) with both.
21-
crate::vec::Vec::from_iter(iterator).into()
21+
crate::vec::Vec::<T, A, COOP_PREFERRED>::from_iter(iterator).into()
2222
}
2323
}
2424

library/alloc/src/vec/in_place_drop.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use core::ptr::{self};
22
use core::slice::{self};
3+
use crate::alloc::Global;
34

45
// A helper struct for in-place iteration that drops the destination slice of iteration,
56
// i.e. the head. The source slice (the tail) is dropped by IntoIter.

library/alloc/src/vec/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#![stable(feature = "rust1", since = "1.0.0")]
5555

5656
#[cfg(not(no_global_oom_handling))]
57+
use core::alloc;
5758
use core::cmp;
5859
use core::cmp::Ordering;
5960
use core::convert::TryFrom;
@@ -2877,9 +2878,12 @@ where
28772878
#[cfg(not(no_global_oom_handling))]
28782879
#[stable(feature = "rust1", since = "1.0.0")]
28792880
#[allow(unused_braces)]
2880-
impl<T> FromIterator<T> for Vec<T, Global, {DEFAULT_COOP_PREFERRED!()}> {
2881+
impl<T, A: Allocator, const COOP_PREFERRED: bool> FromIterator<T> for Vec<T, A, COOP_PREFERRED>
2882+
where
2883+
[(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
2884+
{
28812885
#[inline]
2882-
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T> {
2886+
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Vec<T, A, COOP_PREFERRED> {
28832887
<Self as SpecFromIter<T, I::IntoIter>>::from_iter(iter.into_iter())
28842888
}
28852889
}

0 commit comments

Comments
 (0)