Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using Slice Iterators in const Contexts #104491

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions library/core/src/iter/traits/double_ended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,28 @@ pub trait DoubleEndedIterator: Iterator {
}

#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
#[cfg(not(bootstrap))]
impl<'a, I: ~const DoubleEndedIterator + ?Sized> const DoubleEndedIterator for &'a mut I {
fn next_back(&mut self) -> Option<I::Item> {
(**self).next_back()
}
fn advance_back_by(&mut self, n: usize) -> Result<(), usize>
where
Self::Item: ~const Destruct,
{
(**self).advance_back_by(n)
}
fn nth_back(&mut self, n: usize) -> Option<I::Item>
where
Self::Item: ~const Destruct,
{
(**self).nth_back(n)
}
}

#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(bootstrap)]
impl<'a, I: DoubleEndedIterator + ?Sized> DoubleEndedIterator for &'a mut I {
fn next_back(&mut self) -> Option<I::Item> {
(**self).next_back()
Expand Down
2 changes: 2 additions & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
#![warn(multiple_supertrait_upcastable)]
//
// Library features:
#![feature(const_assume)]
#![feature(const_align_offset)]
#![feature(const_align_of_val)]
#![feature(const_align_of_val_raw)]
Expand Down Expand Up @@ -143,6 +144,7 @@
#![feature(const_ptr_sub_ptr)]
#![feature(const_replace)]
#![feature(const_result_drop)]
#![feature(const_pointer_byte_offsets)]
#![feature(const_ptr_as_ref)]
#![feature(const_ptr_is_null)]
#![feature(const_ptr_read)]
Expand Down
33 changes: 19 additions & 14 deletions library/core/src/slice/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
mod macros;

use crate::cmp;
use crate::cmp::Ordering;
use crate::fmt;
use crate::intrinsics::assume;
use crate::iter::{
FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce, UncheckedIterator,
};
#[cfg(not(bootstrap))]
use crate::marker::Destruct;
use crate::marker::{PhantomData, Send, Sized, Sync};
use crate::mem::{self, SizedTypeProperties};
use crate::num::NonZeroUsize;
Expand All @@ -18,7 +19,8 @@ use crate::ptr::NonNull;
use super::{from_raw_parts, from_raw_parts_mut};

#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> IntoIterator for &'a [T] {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
impl<'a, T> const IntoIterator for &'a [T] {
type Item = &'a T;
type IntoIter = Iter<'a, T>;

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

impl<'a, T> Iter<'a, T> {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
#[inline]
pub(super) fn new(slice: &'a [T]) -> Self {
pub(super) const fn new(slice: &'a [T]) -> Self {
let ptr = slice.as_ptr();
// SAFETY: Similar to `IterMut::new`.
unsafe {
Expand Down Expand Up @@ -121,21 +124,23 @@ impl<'a, T> Iter<'a, T> {
#[must_use]
#[stable(feature = "iter_to_slice", since = "1.4.0")]
#[inline]
pub fn as_slice(&self) -> &'a [T] {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
pub const fn as_slice(&self) -> &'a [T] {
self.make_slice()
}
}

iterator! {struct Iter -> *const T, &'a T, const, {/* no mut */}, {
fn is_sorted_by<F>(self, mut compare: F) -> bool
where
Self: Sized,
F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
{
self.as_slice().windows(2).all(|w| {
compare(&&w[0], &&w[1]).map(|o| o != Ordering::Greater).unwrap_or(false)
})
}
// FIXME(const_trait_impl)
// fn is_sorted_by<F>(self, mut compare: F) -> bool
// where
// Self: Sized,
// F: FnMut(&Self::Item, &Self::Item) -> Option<Ordering>,
// {
// self.as_slice().windows(2).all(|w| {
// compare(&&w[0], &&w[1]).map(|o| o != Ordering::Greater).unwrap_or(false)
// })
// }
}}

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

impl<'a, T> IterMut<'a, T> {
#[inline]
pub(super) fn new(slice: &'a mut [T]) -> Self {
pub(super) const fn new(slice: &'a mut [T]) -> Self {
let ptr = slice.as_mut_ptr();
// SAFETY: There are several things here:
//
Expand Down
Loading