Rollup merge of #151898 - Randl:const-double-iter, r=oli-obk

constify DoubleEndedIterator

The only functions that can't be constified are `advance_back_by` (requires const range or const-hack), `nth_back` (requires `advance_back_by`), and `rfind` (requires const closures).

I've put it under `const_iter`, but I can open a separate tracking issue, though I think tracking all `Iterator` traits separately would be quite annoying, and we probably would prefer to constify them together anyway.
This commit is contained in:
Jonathan Brouwer
2026-04-08 14:21:59 +02:00
committed by GitHub
+10 -5
View File
@@ -1,3 +1,4 @@
use crate::marker::Destruct;
use crate::num::NonZero;
use crate::ops::{ControlFlow, Try};
@@ -38,7 +39,8 @@
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_diagnostic_item = "DoubleEndedIterator"]
pub trait DoubleEndedIterator: Iterator {
#[rustc_const_unstable(feature = "const_iter", issue = "92476")]
pub const trait DoubleEndedIterator: [const] Iterator {
/// Removes and returns an element from the end of the iterator.
///
/// Returns `None` when there are no more elements.
@@ -135,6 +137,7 @@ pub trait DoubleEndedIterator: Iterator {
/// [`Err(k)`]: Err
#[inline]
#[unstable(feature = "iter_advance_by", issue = "77404")]
#[rustc_non_const_trait_method]
fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
for i in 0..n {
if self.next_back().is_none() {
@@ -188,6 +191,7 @@ fn advance_back_by(&mut self, n: usize) -> Result<(), NonZero<usize>> {
/// ```
#[inline]
#[stable(feature = "iter_nth_back", since = "1.37.0")]
#[rustc_non_const_trait_method]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
if self.advance_back_by(n).is_err() {
return None;
@@ -230,8 +234,8 @@ fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
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>,
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() {
@@ -300,8 +304,8 @@ fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
#[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,
F: [const] FnMut(B, Self::Item) -> B + [const] Destruct,
{
let mut accum = init;
while let Some(x) = self.next_back() {
@@ -363,6 +367,7 @@ fn rfold<B, F>(mut self, init: B, mut f: F) -> B
/// ```
#[inline]
#[stable(feature = "iter_rfind", since = "1.27.0")]
#[rustc_non_const_trait_method]
fn rfind<P>(&mut self, predicate: P) -> Option<Self::Item>
where
Self: Sized,