Auto merge of #138835 - joboet:fold-via-try_fold, r= · rust-lang/rust@6d0f160 (original) (raw)

3 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -114,16 +114,6 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
114 114 .try_fold(init, &mut f)
115 115 }
116 116
117 -#[inline]
118 -fn fold<B, F>(mut self, init: B, mut f: F) -> B
119 -where
120 -F: FnMut(B, Self::Item) -> B,
121 -{
122 -match self.try_fold(init, |b, item
123 -Ok(b) => b,
124 -}
125 -}
126 -
127 117 #[inline]
128 118 fn last(mut self) -> OptionSelf::Item\ {
129 119 self.inner.pop_back()
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
1 1 use crate::num::NonZero;
2 -use crate::ops::{ControlFlow, Try};
2 +use crate::ops::{ControlFlow, NeverShortCircuit, Try};
3 3
4 4 /// An iterator able to yield elements from both ends.
5 5 ///
@@ -298,16 +298,12 @@ pub trait DoubleEndedIterator: Iterator {
298 298 #[doc(alias = "foldr")]
299 299 #[inline]
300 300 #[stable(feature = "iter_rfold", since = "1.27.0")]
301 -fn rfold<B, F>(mut self, init: B, mut f: F) -> B
301 +fn rfold<B, F>(mut self, init: B, f: F) -> B
302 302 where
303 303 Self: Sized,
304 304 F: FnMut(B, Self::Item) -> B,
305 305 {
306 -let mut accum = init;
307 -while let Some(x) = self.next_back() {
308 - accum = f(accum, x);
309 -}
310 - accum
306 +self.try_rfold(init, NeverShortCircuit::wrap_mut_2(f)).0
311 307 }
312 308
313 309 /// Searches for an element of an iterator from the back that satisfies a predicate.
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@ use super::super::{
7 7 use crate::array;
8 8 use crate::cmp::{self, Ordering};
9 9 use crate::num::NonZero;
10 -use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, Residual, Try};
10 +use crate::ops::{ChangeOutputType, ControlFlow, FromResidual, NeverShortCircuit, Residual, Try};
11 11
12 12 fn _assert_is_dyn_compatible(_: &dyn Iterator<Item = ()>) {}
13 13
@@ -2550,16 +2550,12 @@ pub trait Iterator {
2550 2550 #[doc(alias = "inject", alias = "foldl")]
2551 2551 #[inline]
2552 2552 #[stable(feature = "rust1", since = "1.0.0")]
2553 -fn fold<B, F>(mut self, init: B, mut f: F) -> B
2553 +fn fold<B, F>(mut self, init: B, f: F) -> B
2554 2554 where
2555 2555 Self: Sized,
2556 2556 F: FnMut(B, Self::Item) -> B,
2557 2557 {
2558 -let mut accum = init;
2559 -while let Some(x) = self.next() {
2560 - accum = f(accum, x);
2561 -}
2562 - accum
2558 +self.try_fold(init, NeverShortCircuit::wrap_mut_2(f)).0
2563 2559 }
2564 2560
2565 2561 /// Reduces the elements to a single one, by repeatedly applying a reducing