Skip to content

Commit bd189bd

Browse files
committed
rebased
1 parent 0634b1c commit bd189bd

17 files changed

+191
-136
lines changed

library/core/src/array/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,14 @@ where
853853
///
854854
/// To minimize indirection fields are still pub but callers should at least use
855855
/// `push_unchecked` to signal that something unsafe is going on.
856+
#[cfg(not(bootstrap))]
857+
pub(crate) struct Guard<'a, T: ~const Destruct, const N: usize> {
858+
/// The array to be initialized.
859+
pub array_mut: &'a mut [MaybeUninit<T>; N],
860+
/// The number of items that have been initialized so far.
861+
pub initialized: usize,
862+
}
863+
#[cfg(bootstrap)]
856864
pub(crate) struct Guard<'a, T: Destruct, const N: usize> {
857865
/// The array to be initialized.
858866
pub array_mut: &'a mut [MaybeUninit<T>; N],

library/core/src/iter/adapters/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
#[cfg(not(bootstrap))]
2+
use crate::const_closure::ConstFnMutClosure;
13
use crate::iter::{InPlaceIterable, Iterator};
24
#[cfg(not(bootstrap))]
35
use crate::marker::Destruct;
6+
#[cfg(not(bootstrap))]
7+
use crate::ops::NeverShortCircuit;
48
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
59

610
mod array_chunks;

library/core/src/iter/adapters/zip.rs

+14-13
Original file line numberDiff line numberDiff line change
@@ -371,16 +371,16 @@ where
371371
#[cfg(not(bootstrap))]
372372
impl<A, B> const ZipImpl<A, B> for Zip<A, B>
373373
where
374-
A: ~const TrustedRandomAccessNoCoerce + ~const Iterator,
375-
B: ~const TrustedRandomAccessNoCoerce + ~const Iterator,
374+
A: TrustedRandomAccessNoCoerce + ~const Iterator,
375+
B: TrustedRandomAccessNoCoerce + ~const Iterator,
376376
A::Item: ~const Destruct,
377377
B::Item: ~const Destruct,
378378
{
379379
zip_impl_general_defaults! {}
380380

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

@@ -519,14 +519,14 @@ where
519519
#[cfg(not(bootstrap))]
520520
impl<A, B> const ZipImpl<A, B> for Zip<A, B>
521521
where
522-
A: ~const TrustedRandomAccess + ~const Iterator,
523-
B: ~const TrustedRandomAccess + ~const Iterator,
522+
A: TrustedRandomAccess + ~const Iterator,
523+
B: TrustedRandomAccess + ~const Iterator,
524524
A::Item: ~const Destruct,
525525
B::Item: ~const Destruct,
526526
{
527527
fn new(a: A, b: B) -> Self {
528-
let a_len = a.size();
529-
let len = cmp::min(a_len, b.size());
528+
let a_len = a.size_hint().0;
529+
let len = cmp::min(a_len, b.size_hint().0);
530530
Zip { a, b, index: 0, len, a_len }
531531
}
532532

@@ -598,13 +598,13 @@ where
598598
B: ~const DoubleEndedIterator + ~const ExactSizeIterator,
599599
{
600600
if A::MAY_HAVE_SIDE_EFFECT || B::MAY_HAVE_SIDE_EFFECT {
601-
let sz_a = self.a.size();
602-
let sz_b = self.b.size();
601+
let sz_a = self.a.size_hint().0;
602+
let sz_b = self.b.size_hint().0;
603603
// Adjust a, b to equal length, make sure that only the first call
604604
// of `next_back` does this, otherwise we will break the restriction
605605
// on calls to `self.next_back()` after calling `get_unchecked()`.
606606
if sz_a != sz_b {
607-
let sz_a = self.a.size();
607+
let sz_a = self.a.size_hint().0;
608608
if A::MAY_HAVE_SIDE_EFFECT && sz_a > self.len {
609609
// FIXME(const_trait_impl): replace with `for`
610610
let mut i = 0;
@@ -631,7 +631,7 @@ where
631631
const_eval_select((self.a_len, self.len), assert_ct, assert_rt);
632632
}
633633
}
634-
let sz_b = self.b.size();
634+
let sz_b = self.b.size_hint().0;
635635
if B::MAY_HAVE_SIDE_EFFECT && sz_b > self.len {
636636
// FIXME(const_trait_impl): replace with `for`
637637
let mut i = 0;
@@ -805,7 +805,8 @@ impl<A: Debug + TrustedRandomAccessNoCoerce, B: Debug + TrustedRandomAccessNoCoe
805805
#[doc(hidden)]
806806
#[unstable(feature = "trusted_random_access", issue = "none")]
807807
#[rustc_specialization_trait]
808-
pub unsafe trait TrustedRandomAccess: ~const TrustedRandomAccessNoCoerce {}
808+
#[const_trait]
809+
pub unsafe trait TrustedRandomAccess: TrustedRandomAccessNoCoerce {}
809810

810811
/// Like [`TrustedRandomAccess`] but without any of the requirements / guarantees around
811812
/// coercions to subtypes after `__iterator_get_unchecked` (they aren’t allowed here!), and
@@ -822,7 +823,7 @@ pub unsafe trait TrustedRandomAccessNoCoerce: Sized {
822823
// Convenience method.
823824
fn size(&self) -> usize
824825
where
825-
Self: Iterator,
826+
Self: ~const Iterator,
826827
{
827828
self.size_hint().0
828829
}

library/core/src/iter/range.rs

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ unsafe_impl_trusted_step![char i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usi
2121
/// The *successor* operation moves towards values that compare greater.
2222
/// The *predecessor* operation moves towards values that compare lesser.
2323
#[unstable(feature = "step_trait", reason = "recently redesigned", issue = "42168")]
24+
#[const_trait]
2425
pub trait Step: Clone + PartialOrd + Sized {
2526
/// Returns the number of *successor* steps required to get from `start` to `end`.
2627
///

library/core/src/iter/traits/accum.rs

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use crate::num::Wrapping;
1010
/// [`sum()`]: Iterator::sum
1111
/// [`FromIterator`]: iter::FromIterator
1212
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
13+
#[const_trait]
1314
pub trait Sum<A = Self>: Sized {
1415
/// Method which takes an iterator and generates `Self` from the elements by
1516
/// "summing up" the items.
@@ -27,6 +28,7 @@ pub trait Sum<A = Self>: Sized {
2728
/// [`product()`]: Iterator::product
2829
/// [`FromIterator`]: iter::FromIterator
2930
#[stable(feature = "iter_arith_traits", since = "1.12.0")]
31+
#[const_trait]
3032
pub trait Product<A = Self>: Sized {
3133
/// Method which takes an iterator and generates `Self` from the elements by
3234
/// multiplying the items.

library/core/src/iter/traits/collect.rs

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ use crate::marker::Destruct;
122122
label = "value of type `{Self}` cannot be built from `std::iter::Iterator<Item={A}>`"
123123
)]
124124
#[rustc_diagnostic_item = "FromIterator"]
125+
#[const_trait]
125126
pub trait FromIterator<A>: Sized {
126127
/// Creates a value from an iterator.
127128
///

library/core/src/iter/traits/double_ended.rs

+38-16
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use crate::ops::{ControlFlow, Try};
1+
use crate::{
2+
const_closure::ConstFnMutClosure,
3+
marker::Destruct,
4+
ops::{ControlFlow, Try},
5+
};
26

37
/// An iterator able to yield elements from both ends.
48
///
@@ -37,7 +41,8 @@ use crate::ops::{ControlFlow, Try};
3741
/// ```
3842
#[stable(feature = "rust1", since = "1.0.0")]
3943
#[cfg_attr(not(test), rustc_diagnostic_item = "DoubleEndedIterator")]
40-
pub trait DoubleEndedIterator: Iterator {
44+
#[const_trait]
45+
pub trait DoubleEndedIterator: ~const Iterator {
4146
/// Removes and returns an element from the end of the iterator.
4247
///
4348
/// Returns `None` when there are no more elements.
@@ -131,10 +136,20 @@ pub trait DoubleEndedIterator: Iterator {
131136
/// [`Err(k)`]: Err
132137
#[inline]
133138
#[unstable(feature = "iter_advance_by", reason = "recently added", issue = "77404")]
134-
fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
135-
for i in 0..n {
139+
fn advance_back_by(&mut self, n: usize) -> Result<(), usize>
140+
where
141+
Self::Item: ~const Destruct,
142+
{
143+
//for i in 0..n {
144+
// self.next_back().ok_or(i)?;
145+
//}
146+
147+
let mut i = 0;
148+
while i < n {
136149
self.next_back().ok_or(i)?;
150+
i += 1;
137151
}
152+
138153
Ok(())
139154
}
140155

@@ -181,7 +196,10 @@ pub trait DoubleEndedIterator: Iterator {
181196
/// ```
182197
#[inline]
183198
#[stable(feature = "iter_nth_back", since = "1.37.0")]
184-
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
199+
fn nth_back(&mut self, n: usize) -> Option<Self::Item>
200+
where
201+
Self::Item: ~const Destruct,
202+
{
185203
self.advance_back_by(n).ok()?;
186204
self.next_back()
187205
}
@@ -221,8 +239,9 @@ pub trait DoubleEndedIterator: Iterator {
221239
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
222240
where
223241
Self: Sized,
224-
F: FnMut(B, Self::Item) -> R,
225-
R: Try<Output = B>,
242+
Self::Item: ~const Destruct,
243+
F: ~const FnMut(B, Self::Item) -> R + ~const Destruct,
244+
R: ~const Try<Output = B>,
226245
{
227246
let mut accum = init;
228247
while let Some(x) = self.next_back() {
@@ -291,8 +310,9 @@ pub trait DoubleEndedIterator: Iterator {
291310
#[stable(feature = "iter_rfold", since = "1.27.0")]
292311
fn rfold<B, F>(mut self, init: B, mut f: F) -> B
293312
where
294-
Self: Sized,
295-
F: FnMut(B, Self::Item) -> B,
313+
Self: Sized + ~const Destruct,
314+
Self::Item: ~const Destruct,
315+
F: ~const FnMut(B, Self::Item) -> B + ~const Destruct,
296316
{
297317
let mut accum = init;
298318
while let Some(x) = self.next_back() {
@@ -344,19 +364,21 @@ pub trait DoubleEndedIterator: Iterator {
344364
/// ```
345365
#[inline]
346366
#[stable(feature = "iter_rfind", since = "1.27.0")]
347-
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
367+
fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item>
348368
where
349369
Self: Sized,
350-
P: FnMut(&Self::Item) -> bool,
370+
Self::Item: ~const Destruct,
371+
P: ~const FnMut(&Self::Item) -> bool + ~const Destruct,
351372
{
352373
#[inline]
353-
fn check<T>(mut predicate: impl FnMut(&T) -> bool) -> impl FnMut((), T) -> ControlFlow<T> {
354-
move |(), x| {
355-
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::CONTINUE }
356-
}
374+
const fn check<T: ~const Destruct, P: ~const FnMut(&T) -> bool>(
375+
predicate: &mut P,
376+
((), x): ((), T),
377+
) -> ControlFlow<T> {
378+
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::CONTINUE }
357379
}
358380

359-
self.try_rfold((), check(predicate)).break_value()
381+
self.try_rfold((), ConstFnMutClosure::new(&mut predicate, check)).break_value()
360382
}
361383
}
362384

0 commit comments

Comments
 (0)