Skip to content

Commit

Permalink
Vec, VecDeque, RawVec etc. with COOP_PREFERRED. WIP: probably NOT com…
Browse files Browse the repository at this point in the history
…pilable. Triggers an internal compiler error from cargo 1.68.0-nightly (f6e737b1e 2022-12-02)
  • Loading branch information
peter-lyons-kehl committed Dec 11, 2022
1 parent 7e84033 commit 27a545d
Show file tree
Hide file tree
Showing 15 changed files with 339 additions and 311 deletions.
15 changes: 9 additions & 6 deletions library/alloc/src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
#![stable(feature = "rust1", since = "1.0.0")]

use core::alloc;
use core::any::Any;
use core::async_iter::AsyncIterator;
use core::borrow;
Expand Down Expand Up @@ -699,7 +700,7 @@ impl<T> Box<[T]> {
Err(_) => return Err(AllocError),
};
let ptr = Global.allocate(layout)?;
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
Ok(RawVec::<T, Global, false>::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
}
}

Expand Down Expand Up @@ -731,12 +732,13 @@ impl<T> Box<[T]> {
Err(_) => return Err(AllocError),
};
let ptr = Global.allocate_zeroed(layout)?;
Ok(RawVec::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
Ok(RawVec::<T, Global, false>::from_raw_parts_in(ptr.as_mut_ptr() as *mut _, len, Global).into_box(len))
}
}
}

impl<T, A: Allocator> Box<[T], A> {
impl<T, A: Allocator> Box<[T], A>
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
/// Constructs a new boxed slice with uninitialized contents in the provided allocator.
///
/// # Examples
Expand Down Expand Up @@ -764,7 +766,7 @@ impl<T, A: Allocator> Box<[T], A> {
// #[unstable(feature = "new_uninit", issue = "63291")]
#[must_use]
pub fn new_uninit_slice_in(len: usize, alloc: A) -> Box<[mem::MaybeUninit<T>], A> {
unsafe { RawVec::with_capacity_in(len, alloc).into_box(len) }
unsafe { RawVec::<T, A, {alloc::SHORT_TERM_VEC_PREFERS_COOP}>::with_capacity_in(len, alloc).into_box(len) }
}

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

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

#[cfg(not(no_global_oom_handling))]
#[stable(feature = "box_slice_clone", since = "1.3.0")]
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A> {
impl<T: Clone, A: Allocator + Clone> Clone for Box<[T], A>
where [(); core::alloc::co_alloc_metadata_num_slots::<A>()]: {
fn clone(&self) -> Self {
let alloc = Box::allocator(self).clone();
self.to_vec_in(alloc).into_boxed_slice()
Expand Down
20 changes: 12 additions & 8 deletions library/alloc/src/collections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,14 @@
#![allow(missing_docs)]
#![stable(feature = "rust1", since = "1.0.0")]

use core::fmt;
use core::{alloc, fmt};
use core::iter::{FromIterator, FusedIterator, InPlaceIterable, SourceIter, TrustedLen};
use core::mem::{self, swap, ManuallyDrop};
use core::ops::{Deref, DerefMut};
use core::ptr;

use crate::alloc::Global;

use crate::collections::TryReserveError;
use crate::slice;
use crate::vec::{self, AsVecIntoIter, Vec};
Expand Down Expand Up @@ -1196,7 +1198,7 @@ impl<T> BinaryHeap<T> {
/// ```
#[inline]
#[stable(feature = "drain", since = "1.6.0")]
pub fn drain(&mut self) -> Drain<'_, T> {
pub fn drain(&mut self) -> Drain<'_, T, {alloc::SHORT_TERM_VEC_PREFERS_COOP}> {
Drain { iter: self.data.drain(..) }
}

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

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

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

#[stable(feature = "drain", since = "1.6.0")]
impl<T> DoubleEndedIterator for Drain<'_, T> {
impl<T, const COOP_PREFERRED: bool> DoubleEndedIterator for Drain<'_, T, COOP_PREFERRED> {
#[inline]
fn next_back(&mut self) -> Option<T> {
self.iter.next_back()
}
}

#[stable(feature = "drain", since = "1.6.0")]
impl<T> ExactSizeIterator for Drain<'_, T> {
impl<T, const COOP_PREFERRED: bool> ExactSizeIterator for Drain<'_, T, COOP_PREFERRED> {
fn is_empty(&self) -> bool {
self.iter.is_empty()
}
}

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

/// A draining iterator over the elements of a `BinaryHeap`.
///
Expand Down
51 changes: 26 additions & 25 deletions library/alloc/src/collections/vec_deque/drain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ pub struct Drain<
'a,
T: 'a,
#[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
const COOP_PREFERRED: bool = {alloc::SHORT_TERM_VEC_PREFERS_COOP}
>
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
// We can't just use a &mut VecDeque<T, A>, as that would make Drain invariant over T
// and we want it to be covariant instead
deque: NonNull<VecDeque<T, A>>,
deque: NonNull<VecDeque<T, A, COOP_PREFERRED>>,
// drain_start is stored in deque.len
drain_len: usize,
// index into the logical array, not the physical one (always lies in [0..deque.len))
Expand All @@ -35,10 +36,10 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
_marker: PhantomData<&'a T>,
}

impl<'a, T, A: Allocator> Drain<'a, T, A>
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
impl<'a, T, A: Allocator, const COOP_PREFERRED: bool> Drain<'a, T, A, COOP_PREFERRED>
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
pub(super) unsafe fn new(
deque: &'a mut VecDeque<T, A>,
deque: &'a mut VecDeque<T, A, COOP_PREFERRED>,
drain_start: usize,
drain_len: usize,
) -> Self {
Expand Down Expand Up @@ -90,8 +91,8 @@ where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
}

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

#[stable(feature = "drain", since = "1.6.0")]
unsafe impl<T: Sync, A: Allocator + Sync> Sync for Drain<'_, T, A>
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
unsafe impl<T: Sync, A: Allocator + Sync, const COOP_PREFERRED: bool> Sync for Drain<'_, T, A, COOP_PREFERRED>
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {}
#[stable(feature = "drain", since = "1.6.0")]
unsafe impl<T: Send, A: Allocator + Send> Send for Drain<'_, T, A>
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
unsafe impl<T: Send, A: Allocator + Send, const COOP_PREFERRED: bool> Send for Drain<'_, T, A, COOP_PREFERRED>
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {}

#[stable(feature = "drain", since = "1.6.0")]
impl<T, A: Allocator> Drop for Drain<'_, T, A>
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {
impl<T, A: Allocator, const COOP_PREFERRED: bool> Drop for Drain<'_, T, A, COOP_PREFERRED>
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {
fn drop(&mut self) {
struct DropGuard<'r, 'a, T, A: Allocator> (&'r mut Drain<'a, T, A>)
where [(); alloc::co_alloc_metadata_num_slots::<A>()]:;
struct DropGuard<'r, 'a, T, A: Allocator, const COOP_PREFERRED: bool> (&'r mut Drain<'a, T, A, COOP_PREFERRED>)
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:;

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

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

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

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

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

#[stable(feature = "fused", since = "1.26.0")]
impl<T, A: Allocator> FusedIterator for Drain<'_, T, A>
where [(); alloc::co_alloc_metadata_num_slots::<A>()]: {}
impl<T, A: Allocator, const COOP_PREFERRED: bool> FusedIterator for Drain<'_, T, A, COOP_PREFERRED>
where [(); alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]: {}
4 changes: 2 additions & 2 deletions library/alloc/src/collections/vec_deque/macros.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
macro_rules! __impl_slice_eq1 {
([$($vars:tt)*] $lhs:ty, $rhs:ty, $($constraints:tt)*) => {
#[stable(feature = "vec_deque_partial_eq_slice", since = "1.17.0")]
impl<T, U, A: Allocator, $($vars)*> PartialEq<$rhs> for $lhs
impl<T, U, A: Allocator, const COOP_PREFERRED: bool, $($vars)*> PartialEq<$rhs> for $lhs
where
T: PartialEq<U>,
[(); core::alloc::co_alloc_metadata_num_slots::<A>()]:,
[(); core::alloc::co_alloc_metadata_num_slots_with_preference::<A>(COOP_PREFERRED)]:,
$($constraints)*
{
fn eq(&self, other: &$rhs) -> bool {
Expand Down
Loading

0 comments on commit 27a545d

Please # to comment.