From d882491158a29f58131c86d12272b350037b78c1 Mon Sep 17 00:00:00 2001 From: Xenira <1288524+Xenira@users.noreply.github.com> Date: Mon, 27 May 2024 20:00:38 +0200 Subject: [PATCH] feat(`FilterOk`): implement `DoubleEndedIterator` Refs: #947 --- benches/specializations.rs | 1 + src/adaptors/mod.rs | 24 ++++++++++++++++++++++++ tests/specializations.rs | 4 +++- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/benches/specializations.rs b/benches/specializations.rs index 18039fc4e..958bcc124 100644 --- a/benches/specializations.rs +++ b/benches/specializations.rs @@ -639,6 +639,7 @@ bench_specializations! { v.iter().copied().map_ok(|x| x + 1) } filter_ok { + DoubleEndedIterator { let v = black_box((0_u32..1024) .map(|x| if x % 2 == 1 { Err(x) } else { Ok(x) }) diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index 6f5e0260e..e757837b7 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -936,6 +936,30 @@ where } } +impl DoubleEndedIterator for FilterOk +where + I: DoubleEndedIterator>, + F: FnMut(&T) -> bool, +{ + fn next_back(&mut self) -> Option { + let f = &mut self.f; + self.iter.rfind(|res| match res { + Ok(t) => f(t), + _ => true, + }) + } + + fn rfold(self, init: Acc, fold_f: Fold) -> Acc + where + Fold: FnMut(Acc, Self::Item) -> Acc, + { + let mut f = self.f; + self.iter + .filter(|v| v.as_ref().map(&mut f).unwrap_or(true)) + .rfold(init, fold_f) + } +} + impl FusedIterator for FilterOk where I: FusedIterator>, diff --git a/tests/specializations.rs b/tests/specializations.rs index 712311472..cdc51fb23 100644 --- a/tests/specializations.rs +++ b/tests/specializations.rs @@ -447,7 +447,9 @@ quickcheck! { } fn filter_ok(v: Vec>) -> () { - test_specializations(&v.into_iter().filter_ok(|&i| i < 20)); + let it = v.into_iter().filter_ok(|&i| i < 20); + test_specializations(&it); + test_double_ended_specializations(&it); } fn filter_map_ok(v: Vec>) -> () {