Skip to content

Commit 8249c6c

Browse files
committed
rebased
1 parent 7d2c911 commit 8249c6c

File tree

17 files changed

+125
-63
lines changed

17 files changed

+125
-63
lines changed

library/core/src/array/mod.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ where
916916
///
917917
/// To minimize indirection fields are still pub but callers should at least use
918918
/// `push_unchecked` to signal that something unsafe is going on.
919-
pub(crate) struct Guard<'a, T, const N: usize> {
919+
pub(crate) struct Guard<'a, T: ~const Destruct, const N: usize> {
920920
/// The array to be initialized.
921921
pub array_mut: &'a mut [MaybeUninit<T>; N],
922922
/// The number of items that have been initialized so far.
@@ -930,7 +930,7 @@ impl<T, const N: usize> Guard<'_, T, N> {
930930
///
931931
/// No more than N elements must be initialized.
932932
#[inline]
933-
pub unsafe fn push_unchecked(&mut self, item: T) {
933+
pub const unsafe fn push_unchecked(&mut self, item: T) {
934934
// SAFETY: If `initialized` was correct before and the caller does not
935935
// invoke this method more than N times then writes will be in-bounds
936936
// and slots will not be initialized more than once.
@@ -941,15 +941,33 @@ impl<T, const N: usize> Guard<'_, T, N> {
941941
}
942942
}
943943

944-
impl<T, const N: usize> Drop for Guard<'_, T, N> {
944+
impl<T: ~const Destruct, const N: usize> const Drop for Guard<'_, T, N> {
945945
fn drop(&mut self) {
946946
debug_assert!(self.initialized <= N);
947947

948+
#[inline]
949+
const fn drop_ct<T: ~const Destruct>(x: &mut [T]) {
950+
let mut i = 0;
951+
while i < x.len() {
952+
// SAFETY: dropping the value, contains initialized objects
953+
unsafe {
954+
crate::ptr::read(&mut x[i]);
955+
}
956+
i += 1;
957+
}
958+
}
959+
#[inline]
960+
fn drop_rt<T>(x: &mut [T]) {
961+
// SAFETY: slice contains initialized objects
962+
unsafe { crate::ptr::drop_in_place(x) }
963+
}
964+
948965
// SAFETY: this slice will contain only initialized objects.
949966
unsafe {
950-
crate::ptr::drop_in_place(MaybeUninit::slice_assume_init_mut(
951-
&mut self.array_mut.get_unchecked_mut(..self.initialized),
952-
));
967+
let to_drop = MaybeUninit::slice_assume_init_mut(
968+
self.array_mut.get_unchecked_mut(..self.initialized),
969+
);
970+
crate::intrinsics::const_eval_select((to_drop,), drop_ct, drop_rt);
953971
}
954972
}
955973
}

library/core/src/cmp.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,7 @@ pub const fn min<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
12231223
#[stable(feature = "cmp_min_max_by", since = "1.53.0")]
12241224
#[rustc_const_unstable(feature = "const_cmp", issue = "92391")]
12251225
pub const fn min_by<T, F: ~const FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T
1226-
where
1226+
where
12271227
T: ~const Destruct,
12281228
{
12291229
match compare(&v1, &v2) {
@@ -1309,6 +1309,7 @@ pub const fn max<T: ~const Ord + ~const Destruct>(v1: T, v2: T) -> T {
13091309
pub const fn max_by<T, F: ~const FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T
13101310
where
13111311
T: ~const Destruct,
1312+
{
13121313
match compare(&v1, &v2) {
13131314
Ordering::Less | Ordering::Equal => v2,
13141315
Ordering::Greater => v1,

library/core/src/const_closure.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub(crate) struct ConstFnMutClosure<CapturedData, Function> {
2525
/// The Function of the Closure, must be: Fn(CapturedData, ClosureArgs) -> ClosureReturn
2626
pub func: Function,
2727
}
28+
2829
impl<'a, CapturedData: ?Sized, Function> ConstFnMutClosure<&'a mut CapturedData, Function> {
2930
/// Function for creating a new closure.
3031
///

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::const_closure::ConstFnMutClosure;
12
use crate::fmt;
23
use crate::iter::adapters::{
34
zip::try_get_unchecked, SourceIter, TrustedRandomAccess, TrustedRandomAccessNoCoerce,

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::const_closure::ConstFnMutClosure;
12
use crate::iter::{InPlaceIterable, Iterator};
23
use crate::marker::Destruct;
34
use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ macro_rules! zip_impl_general_defaults {
171171
#[inline]
172172
#[cfg_attr(not(bootstrap), rustc_allow_const_fn_unstable(const_try, const_for))]
173173
default fn next(&mut self) -> Option<(A::Item, B::Item)> {
174-
let x = self.a.next()?;
175-
let y = self.b.next()?;
174+
// FIXME(const_trait_impl): revert to `?` when we can
175+
let Some(x) = self.a.next() else { return None };
176+
let Some(y) = self.b.next() else { return None };
176177
Some((x, y))
177178
}
178179

@@ -586,11 +587,12 @@ pub unsafe trait TrustedRandomAccess: ~const TrustedRandomAccessNoCoerce {}
586587
#[doc(hidden)]
587588
#[unstable(feature = "trusted_random_access", issue = "none")]
588589
#[rustc_specialization_trait]
590+
#[const_trait]
589591
pub unsafe trait TrustedRandomAccessNoCoerce: Sized {
590592
// Convenience method.
591593
fn size(&self) -> usize
592594
where
593-
Self: Iterator,
595+
Self: ~const Iterator,
594596
{
595597
self.size_hint().0
596598
}

library/core/src/iter/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,8 @@ macro_rules! impl_fold_via_try_fold {
364364
#[inline]
365365
fn $fold<AAA, FFF>(mut self, init: AAA, mut fold: FFF) -> AAA
366366
where
367-
FFF: FnMut(AAA, Self::Item) -> AAA,
367+
FFF: ~const FnMut(AAA, Self::Item) -> AAA + ~const crate::marker::Destruct,
368+
Self: ~const crate::marker::Destruct,
368369
{
369370
use crate::const_closure::ConstFnMutClosure;
370371
use crate::ops::NeverShortCircuit;

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

+2-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
///
@@ -267,7 +268,7 @@ pub trait IntoIterator {
267268

268269
#[rustc_const_unstable(feature = "const_intoiterator_identity", issue = "90603")]
269270
#[stable(feature = "rust1", since = "1.0.0")]
270-
impl<I: Iterator> const IntoIterator for I {
271+
impl<I: ~const Iterator> const IntoIterator for I {
271272
type Item = I::Item;
272273
type IntoIter = I;
273274

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

+30-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::const_closure::ConstFnMutClosure;
2+
use crate::marker::Destruct;
13
use crate::ops::{ControlFlow, Try};
24

35
/// An iterator able to yield elements from both ends.
@@ -37,6 +39,7 @@ use crate::ops::{ControlFlow, Try};
3739
/// ```
3840
#[stable(feature = "rust1", since = "1.0.0")]
3941
#[cfg_attr(not(test), rustc_diagnostic_item = "DoubleEndedIterator")]
42+
#[const_trait]
4043
pub trait DoubleEndedIterator: Iterator {
4144
/// Removes and returns an element from the end of the iterator.
4245
///
@@ -131,9 +134,15 @@ pub trait DoubleEndedIterator: Iterator {
131134
/// [`Err(k)`]: Err
132135
#[inline]
133136
#[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 {
137+
fn advance_back_by(&mut self, n: usize) -> Result<(), usize>
138+
where
139+
Self::Item: ~const Destruct,
140+
{
141+
// FIXME(const_trait_impl): revert back to for loop
142+
let mut i = 0;
143+
while i < n {
136144
self.next_back().ok_or(i)?;
145+
i += 1;
137146
}
138147
Ok(())
139148
}
@@ -181,7 +190,10 @@ pub trait DoubleEndedIterator: Iterator {
181190
/// ```
182191
#[inline]
183192
#[stable(feature = "iter_nth_back", since = "1.37.0")]
184-
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
193+
fn nth_back(&mut self, n: usize) -> Option<Self::Item>
194+
where
195+
Self::Item: ~const Destruct,
196+
{
185197
self.advance_back_by(n).ok()?;
186198
self.next_back()
187199
}
@@ -221,8 +233,9 @@ pub trait DoubleEndedIterator: Iterator {
221233
fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
222234
where
223235
Self: Sized,
224-
F: FnMut(B, Self::Item) -> R,
225-
R: Try<Output = B>,
236+
F: ~const FnMut(B, Self::Item) -> R + ~const Destruct,
237+
R: ~const Try<Output = B>,
238+
Self::Item: ~const Destruct,
226239
{
227240
let mut accum = init;
228241
while let Some(x) = self.next_back() {
@@ -291,8 +304,9 @@ pub trait DoubleEndedIterator: Iterator {
291304
#[stable(feature = "iter_rfold", since = "1.27.0")]
292305
fn rfold<B, F>(mut self, init: B, mut f: F) -> B
293306
where
294-
Self: Sized,
295-
F: FnMut(B, Self::Item) -> B,
307+
Self: Sized + ~const Destruct,
308+
F: ~const FnMut(B, Self::Item) -> B + ~const Destruct,
309+
Self::Item: ~const Destruct,
296310
{
297311
let mut accum = init;
298312
while let Some(x) = self.next_back() {
@@ -344,19 +358,21 @@ pub trait DoubleEndedIterator: Iterator {
344358
/// ```
345359
#[inline]
346360
#[stable(feature = "iter_rfind", since = "1.27.0")]
347-
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
361+
fn rfind<P>(&mut self, mut predicate: P) -> Option<Self::Item>
348362
where
349363
Self: Sized,
350-
P: FnMut(&Self::Item) -> bool,
364+
P: ~const FnMut(&Self::Item) -> bool + ~const Destruct,
365+
Self::Item: ~const Destruct,
351366
{
352367
#[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-
}
368+
const fn check<T: ~const Destruct, F: ~const FnMut(&T) -> bool>(
369+
predicate: &mut F,
370+
((), x): ((), T),
371+
) -> ControlFlow<T> {
372+
if predicate(&x) { ControlFlow::Break(x) } else { ControlFlow::CONTINUE }
357373
}
358374

359-
self.try_rfold((), check(predicate)).break_value()
375+
self.try_rfold((), ConstFnMutClosure::new(&mut predicate, check)).break_value()
360376
}
361377
}
362378

0 commit comments

Comments
 (0)