Skip to content

Commit

Permalink
rebased
Browse files Browse the repository at this point in the history
  • Loading branch information
onestacked committed Nov 11, 2022
1 parent 0634b1c commit bd189bd
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 136 deletions.
8 changes: 8 additions & 0 deletions library/core/src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,14 @@ where
///
/// To minimize indirection fields are still pub but callers should at least use
/// `push_unchecked` to signal that something unsafe is going on.
#[cfg(not(bootstrap))]
pub(crate) struct Guard<'a, T: ~const Destruct, const N: usize> {
/// The array to be initialized.
pub array_mut: &'a mut [MaybeUninit<T>; N],
/// The number of items that have been initialized so far.
pub initialized: usize,
}
#[cfg(bootstrap)]
pub(crate) struct Guard<'a, T: Destruct, const N: usize> {
/// The array to be initialized.
pub array_mut: &'a mut [MaybeUninit<T>; N],
Expand Down
4 changes: 4 additions & 0 deletions library/core/src/iter/adapters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#[cfg(not(bootstrap))]
use crate::const_closure::ConstFnMutClosure;
use crate::iter::{InPlaceIterable, Iterator};
#[cfg(not(bootstrap))]
use crate::marker::Destruct;
#[cfg(not(bootstrap))]
use crate::ops::NeverShortCircuit;
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};

mod array_chunks;
Expand Down
27 changes: 14 additions & 13 deletions library/core/src/iter/adapters/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,16 +371,16 @@ where
#[cfg(not(bootstrap))]
impl<A, B> const ZipImpl<A, B> for Zip<A, B>
where
A: ~const TrustedRandomAccessNoCoerce + ~const Iterator,
B: ~const TrustedRandomAccessNoCoerce + ~const Iterator,
A: TrustedRandomAccessNoCoerce + ~const Iterator,
B: TrustedRandomAccessNoCoerce + ~const Iterator,
A::Item: ~const Destruct,
B::Item: ~const Destruct,
{
zip_impl_general_defaults! {}

#[inline]
default fn size_hint(&self) -> (usize, Option<usize>) {
let size = cmp::min(self.a.size(), self.b.size());
let size = cmp::min(self.a.size_hint().0, self.b.size_hint().0);
(size, Some(size))
}

Expand Down Expand Up @@ -519,14 +519,14 @@ where
#[cfg(not(bootstrap))]
impl<A, B> const ZipImpl<A, B> for Zip<A, B>
where
A: ~const TrustedRandomAccess + ~const Iterator,
B: ~const TrustedRandomAccess + ~const Iterator,
A: TrustedRandomAccess + ~const Iterator,
B: TrustedRandomAccess + ~const Iterator,
A::Item: ~const Destruct,
B::Item: ~const Destruct,
{
fn new(a: A, b: B) -> Self {
let a_len = a.size();
let len = cmp::min(a_len, b.size());
let a_len = a.size_hint().0;
let len = cmp::min(a_len, b.size_hint().0);
Zip { a, b, index: 0, len, a_len }
}

Expand Down Expand Up @@ -598,13 +598,13 @@ where
B: ~const DoubleEndedIterator + ~const ExactSizeIterator,
{
if A::MAY_HAVE_SIDE_EFFECT || B::MAY_HAVE_SIDE_EFFECT {
let sz_a = self.a.size();
let sz_b = self.b.size();
let sz_a = self.a.size_hint().0;
let sz_b = self.b.size_hint().0;
// Adjust a, b to equal length, make sure that only the first call
// of `next_back` does this, otherwise we will break the restriction
// on calls to `self.next_back()` after calling `get_unchecked()`.
if sz_a != sz_b {
let sz_a = self.a.size();
let sz_a = self.a.size_hint().0;
if A::MAY_HAVE_SIDE_EFFECT && sz_a > self.len {
// FIXME(const_trait_impl): replace with `for`
let mut i = 0;
Expand All @@ -631,7 +631,7 @@ where
const_eval_select((self.a_len, self.len), assert_ct, assert_rt);
}
}
let sz_b = self.b.size();
let sz_b = self.b.size_hint().0;
if B::MAY_HAVE_SIDE_EFFECT && sz_b > self.len {
// FIXME(const_trait_impl): replace with `for`
let mut i = 0;
Expand Down Expand Up @@ -805,7 +805,8 @@ impl<A: Debug + TrustedRandomAccessNoCoerce, B: Debug + TrustedRandomAccessNoCoe
#[doc(hidden)]
#[unstable(feature = "trusted_random_access", issue = "none")]
#[rustc_specialization_trait]
pub unsafe trait TrustedRandomAccess: ~const TrustedRandomAccessNoCoerce {}
#[const_trait]
pub unsafe trait TrustedRandomAccess: TrustedRandomAccessNoCoerce {}

/// Like [`TrustedRandomAccess`] but without any of the requirements / guarantees around
/// coercions to subtypes after `__iterator_get_unchecked` (they aren’t allowed here!), and
Expand All @@ -822,7 +823,7 @@ pub unsafe trait TrustedRandomAccessNoCoerce: Sized {
// Convenience method.
fn size(&self) -> usize
where
Self: Iterator,
Self: ~const Iterator,
{
self.size_hint().0
}
Expand Down
1 change: 1 addition & 0 deletions library/core/src/iter/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ unsafe_impl_trusted_step![char i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usi
/// The *successor* operation moves towards values that compare greater.
/// The *predecessor* operation moves towards values that compare lesser.
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
#[const_trait]
pub trait Step: Clone + PartialOrd + Sized {
/// Returns the number of *successor* steps required to get from `start` to `end`.
///
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/iter/traits/accum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::num::Wrapping;
/// [`sum()`]: Iterator::sum
/// [`FromIterator`]: iter::FromIterator
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
#[const_trait]
pub trait Sum<A = Self>: Sized {
/// Method which takes an iterator and generates `Self` from the elements by
/// "summing up" the items.
Expand All @@ -27,6 +28,7 @@ pub trait Sum<A = Self>: Sized {
/// [`product()`]: Iterator::product
/// [`FromIterator`]: iter::FromIterator
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
#[const_trait]
pub trait Product<A = Self>: Sized {
/// Method which takes an iterator and generates `Self` from the elements by
/// multiplying the items.
Expand Down
1 change: 1 addition & 0 deletions library/core/src/iter/traits/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ use crate::marker::Destruct;
label = "value of type `{Self}` cannot be built from `std::iter::Iterator<Item={A}>`"
)]
#[rustc_diagnostic_item = "FromIterator"]
#[const_trait]
pub trait FromIterator<A>: Sized {
/// Creates a value from an iterator.
///
Expand Down
54 changes: 38 additions & 16 deletions library/core/src/iter/traits/double_ended.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::ops::{ControlFlow, Try};
use crate::{
const_closure::ConstFnMutClosure,
marker::Destruct,
ops::{ControlFlow, Try},
};

/// An iterator able to yield elements from both ends.
///
Expand Down Expand Up @@ -37,7 +41,8 @@ use crate::ops::{ControlFlow, Try};
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "DoubleEndedIterator")]
pub trait DoubleEndedIterator: Iterator {
#[const_trait]
pub trait DoubleEndedIterator: ~const Iterator {
/// Removes and returns an element from the end of the iterator.
///
/// Returns `None` when there are no more elements.
Expand Down Expand Up @@ -131,10 +136,20 @@ pub trait DoubleEndedIterator: Iterator {
/// [`Err(k)`]: Err
#[inline]
#[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")]
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
for i in 0..n {
fn advance_back_by(&mut self, n: usize) -> Result<(), usize>
where
Self::Item: ~const Destruct,
{
//for i in 0..n {
// self.next_back().ok_or(i)?;
//}

let mut i = 0;
while i < n {
self.next_back().ok_or(i)?;
i += 1;
}

Ok(())
}

Expand Down Expand Up @@ -181,7 +196,10 @@ pub trait DoubleEndedIterator: Iterator {
/// ```
#[inline]
#[stable(feature = "iter_nth_back", since = "1.37.0")]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
fn nth_back(&mut self, n: usize) -> Option<Self::Item>
where
Self::Item: ~const Destruct,
{
self.advance_back_by(n).ok()?;
self.next_back()
}
Expand Down Expand Up @@ -221,8 +239,9 @@ pub trait DoubleEndedIterator: Iterator {
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
where
Self: Sized,
F: FnMut(B, Self::Item) -> R,
R: Try<Output = B>,
Self::Item: ~const Destruct,
F: ~const FnMut(B, Self::Item) -> R + ~const Destruct,
R: ~const Try<Output = B>,
{
let mut accum = init;
while let Some(x) = self.next_back() {
Expand Down Expand Up @@ -291,8 +310,9 @@ pub trait DoubleEndedIterator: Iterator {
#[stable(feature = "iter_rfold", since = "1.27.0")]
fn rfold<B, F>(mut self, init: B, mut f: F) -> B
where
Self: Sized,
F: FnMut(B, Self::Item) -> B,
Self: Sized + ~const Destruct,
Self::Item: ~const Destruct,
F: ~const FnMut(B, Self::Item) -> B + ~const Destruct,
{
let mut accum = init;
while let Some(x) = self.next_back() {
Expand Down Expand Up @@ -344,19 +364,21 @@ pub trait DoubleEndedIterator: Iterator {
/// ```
#[inline]
#[stable(feature = "iter_rfind", since = "1.27.0")]
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item>
where
Self: Sized,
P: FnMut(&Self::Item) -> bool,
Self::Item: ~const Destruct,
P: ~const FnMut(&Self::Item) -> bool + ~const Destruct,
{
#[inline]
fn check<T>(mut predicate: impl FnMut(&T) -> bool) -> impl FnMut((), T) -> ControlFlow<T> {
move |(), x| {
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::CONTINUE }
}
const fn check<T: ~const Destruct, P: ~const FnMut(&T) -> bool>(
predicate: &mut P,
((), x): ((), T),
) -> ControlFlow<T> {
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::CONTINUE }
}

self.try_rfold((), check(predicate)).break_value()
self.try_rfold((), ConstFnMutClosure::new(&mut predicate, check)).break_value()
}
}

Expand Down
Loading

0 comments on commit bd189bd

Please sign in to comment.