From a398b6b9d429cd0044c0b4cec95ec4fe2dbac295 Mon Sep 17 00:00:00 2001 From: The8472 Date: Mon, 11 Oct 2021 17:50:03 +0200 Subject: [PATCH 1/2] inline next() on &mut Iterator impl --- library/core/src/iter/traits/iterator.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/library/core/src/iter/traits/iterator.rs b/library/core/src/iter/traits/iterator.rs index 8f9b32e775056..f53d6cac7ed98 100644 --- a/library/core/src/iter/traits/iterator.rs +++ b/library/core/src/iter/traits/iterator.rs @@ -3460,6 +3460,7 @@ pub trait Iterator { #[stable(feature = "rust1", since = "1.0.0")] impl Iterator for &mut I { type Item = I::Item; + #[inline] fn next(&mut self) -> Option { (**self).next() } From f1c588f1ef391b33663f44c52b17c9cc12928a75 Mon Sep 17 00:00:00 2001 From: The8472 Date: Mon, 11 Oct 2021 23:36:04 +0200 Subject: [PATCH 2/2] use fold instead of try_fold now that .by_ref().next() has been inlined --- library/core/src/array/iter.rs | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/library/core/src/array/iter.rs b/library/core/src/array/iter.rs index 822747dd0e824..5d63cf03fcbc4 100644 --- a/library/core/src/array/iter.rs +++ b/library/core/src/array/iter.rs @@ -135,19 +135,12 @@ impl Iterator for IntoIter { Fold: FnMut(Acc, Self::Item) -> Acc, { let data = &mut self.data; - // FIXME: This uses try_fold(&mut iter) instead of fold(iter) because the latter - // would go through the blanket `impl Iterator for &mut I` implementation - // which lacks inline annotations on its methods and adding those would be a larger - // perturbation than using try_fold here. - // Whether it would be beneficial to add those annotations should be investigated separately. - (&mut self.alive) - .try_fold::<_, _, Result<_, !>>(init, |acc, idx| { - // SAFETY: idx is obtained by folding over the `alive` range, which implies the - // value is currently considered alive but as the range is being consumed each value - // we read here will only be read once and then considered dead. - Ok(fold(acc, unsafe { data.get_unchecked(idx).assume_init_read() })) - }) - .unwrap() + self.alive.by_ref().fold(init, |acc, idx| { + // SAFETY: idx is obtained by folding over the `alive` range, which implies the + // value is currently considered alive but as the range is being consumed each value + // we read here will only be read once and then considered dead. + fold(acc, unsafe { data.get_unchecked(idx).assume_init_read() }) + }) } fn count(self) -> usize {