|
1 |
| -use crate::ops::{ControlFlow, Try}; |
| 1 | +use crate::{ |
| 2 | + const_closure::ConstFnMutClosure, |
| 3 | + marker::Destruct, |
| 4 | + ops::{ControlFlow, Try}, |
| 5 | +}; |
2 | 6 |
|
3 | 7 | /// An iterator able to yield elements from both ends.
|
4 | 8 | ///
|
@@ -37,7 +41,8 @@ use crate::ops::{ControlFlow, Try};
|
37 | 41 | /// ```
|
38 | 42 | #[stable(feature = "rust1", since = "1.0.0")]
|
39 | 43 | #[cfg_attr(not(test), rustc_diagnostic_item = "DoubleEndedIterator")]
|
40 |
| -pub trait DoubleEndedIterator: Iterator { |
| 44 | +#[const_trait] |
| 45 | +pub trait DoubleEndedIterator: ~const Iterator { |
41 | 46 | /// Removes and returns an element from the end of the iterator.
|
42 | 47 | ///
|
43 | 48 | /// Returns `None` when there are no more elements.
|
@@ -131,10 +136,20 @@ pub trait DoubleEndedIterator: Iterator {
|
131 | 136 | /// [`Err(k)`]: Err
|
132 | 137 | #[inline]
|
133 | 138 | #[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 { |
136 | 149 | self.next_back().ok_or(i)?;
|
| 150 | + i += 1; |
137 | 151 | }
|
| 152 | + |
138 | 153 | Ok(())
|
139 | 154 | }
|
140 | 155 |
|
@@ -181,7 +196,10 @@ pub trait DoubleEndedIterator: Iterator {
|
181 | 196 | /// ```
|
182 | 197 | #[inline]
|
183 | 198 | #[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 | + { |
185 | 203 | self.advance_back_by(n).ok()?;
|
186 | 204 | self.next_back()
|
187 | 205 | }
|
@@ -221,8 +239,9 @@ pub trait DoubleEndedIterator: Iterator {
|
221 | 239 | fn try_rfold<B, F, R>(&mut self, init: B, mut f: F) -> R
|
222 | 240 | where
|
223 | 241 | 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>, |
226 | 245 | {
|
227 | 246 | let mut accum = init;
|
228 | 247 | while let Some(x) = self.next_back() {
|
@@ -291,8 +310,9 @@ pub trait DoubleEndedIterator: Iterator {
|
291 | 310 | #[stable(feature = "iter_rfold", since = "1.27.0")]
|
292 | 311 | fn rfold<B, F>(mut self, init: B, mut f: F) -> B
|
293 | 312 | 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, |
296 | 316 | {
|
297 | 317 | let mut accum = init;
|
298 | 318 | while let Some(x) = self.next_back() {
|
@@ -344,19 +364,21 @@ pub trait DoubleEndedIterator: Iterator {
|
344 | 364 | /// ```
|
345 | 365 | #[inline]
|
346 | 366 | #[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> |
348 | 368 | where
|
349 | 369 | Self: Sized,
|
350 |
| - P: FnMut(&Self::Item) -> bool, |
| 370 | + Self::Item: ~const Destruct, |
| 371 | + P: ~const FnMut(&Self::Item) -> bool + ~const Destruct, |
351 | 372 | {
|
352 | 373 | #[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 } |
357 | 379 | }
|
358 | 380 |
|
359 |
| - self.try_rfold((), check(predicate)).break_value() |
| 381 | + self.try_rfold((), ConstFnMutClosure::new(&mut predicate, check)).break_value() |
360 | 382 | }
|
361 | 383 | }
|
362 | 384 |
|
|
0 commit comments