Skip to content

Commit 9c4bddf

Browse files
committed
make slice::Iter ~const Iterator.
1 parent 8826b68 commit 9c4bddf

File tree

7 files changed

+369
-22
lines changed

7 files changed

+369
-22
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,28 @@ pub trait DoubleEndedIterator: Iterator {
370370
}
371371

372372
#[stable(feature = "rust1", since = "1.0.0")]
373+
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
374+
#[cfg(not(bootstrap))]
375+
impl<'a, I: ~const DoubleEndedIterator + ?Sized> const DoubleEndedIterator for &'a mut I {
376+
fn next_back(&mut self) -> Option<I::Item> {
377+
(**self).next_back()
378+
}
379+
fn advance_back_by(&mut self, n: usize) -> Result<(), usize>
380+
where
381+
Self::Item: ~const Destruct,
382+
{
383+
(**self).advance_back_by(n)
384+
}
385+
fn nth_back(&mut self, n: usize) -> Option<I::Item>
386+
where
387+
Self::Item: ~const Destruct,
388+
{
389+
(**self).nth_back(n)
390+
}
391+
}
392+
393+
#[stable(feature = "rust1", since = "1.0.0")]
394+
#[cfg(bootstrap)]
373395
impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
374396
fn next_back(&mut self) -> Option<I::Item> {
375397
(**self).next_back()

library/core/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
#![warn(multiple_supertrait_upcastable)]
9999
//
100100
// Library features:
101+
#![feature(const_assume)]
101102
#![feature(const_align_offset)]
102103
#![feature(const_align_of_val)]
103104
#![feature(const_align_of_val_raw)]
@@ -143,6 +144,7 @@
143144
#![feature(const_ptr_sub_ptr)]
144145
#![feature(const_replace)]
145146
#![feature(const_result_drop)]
147+
#![feature(const_pointer_byte_offsets)]
146148
#![feature(const_ptr_as_ref)]
147149
#![feature(const_ptr_is_null)]
148150
#![feature(const_ptr_read)]

library/core/src/slice/iter.rs

+19-14
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
mod macros;
55

66
use crate::cmp;
7-
use crate::cmp::Ordering;
87
use crate::fmt;
98
use crate::intrinsics::assume;
109
use crate::iter::{
1110
FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, UncheckedIterator,
1211
};
12+
#[cfg(not(bootstrap))]
13+
use crate::marker::Destruct;
1314
use crate::marker::{PhantomData, Send, Sized, Sync};
1415
use crate::mem::{self, SizedTypeProperties};
1516
use crate::num::NonZeroUsize;
@@ -18,7 +19,8 @@ use crate::ptr::NonNull;
1819
use super::{from_raw_parts, from_raw_parts_mut};
1920

2021
#[stable(feature = "rust1", since = "1.0.0")]
21-
impl<'a, T> IntoIterator for &'a [T] {
22+
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
23+
impl<'a, T> const IntoIterator for &'a [T] {
2224
type Item = &'a T;
2325
type IntoIter = Iter<'a, T>;
2426

@@ -80,8 +82,9 @@ unsafe impl<T: Sync> Sync for Iter<'_, T> {}
8082
unsafe impl<T: Sync> Send for Iter<'_, T> {}
8183

8284
impl<'a, T> Iter<'a, T> {
85+
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
8386
#[inline]
84-
pub(super) fn new(slice: &'a [T]) -> Self {
87+
pub(super) const fn new(slice: &'a [T]) -> Self {
8588
let ptr = slice.as_ptr();
8689
// SAFETY: Similar to `IterMut::new`.
8790
unsafe {
@@ -121,21 +124,23 @@ impl<'a, T> Iter<'a, T> {
121124
#[must_use]
122125
#[stable(feature = "iter_to_slice", since = "1.4.0")]
123126
#[inline]
124-
pub fn as_slice(&self) -> &'a [T] {
127+
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
128+
pub const fn as_slice(&self) -> &'a [T] {
125129
self.make_slice()
126130
}
127131
}
128132

129133
iterator! {struct Iter -> *const T, &'a T, const, {/* no mut */}, {
130-
fn is_sorted_by<F>(self, mut compare: F) -> bool
131-
where
132-
Self: Sized,
133-
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
134-
{
135-
self.as_slice().windows(2).all(|w| {
136-
compare(&&w[0], &&w[1]).map(|o| o != Ordering::Greater).unwrap_or(false)
137-
})
138-
}
134+
// FIXME(const_trait_impl)
135+
// fn is_sorted_by<F>(self, mut compare: F) -> bool
136+
// where
137+
// Self: Sized,
138+
// F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
139+
// {
140+
// self.as_slice().windows(2).all(|w| {
141+
// compare(&&w[0], &&w[1]).map(|o| o != Ordering::Greater).unwrap_or(false)
142+
// })
143+
// }
139144
}}
140145

141146
#[stable(feature = "rust1", since = "1.0.0")]
@@ -202,7 +207,7 @@ unsafe impl<T: Send> Send for IterMut<'_, T> {}
202207

203208
impl<'a, T> IterMut<'a, T> {
204209
#[inline]
205-
pub(super) fn new(slice: &'a mut [T]) -> Self {
210+
pub(super) const fn new(slice: &'a mut [T]) -> Self {
206211
let ptr = slice.as_mut_ptr();
207212
// SAFETY: There are several things here:
208213
//

0 commit comments

Comments
 (0)