From 3377c3c43faba7bfc446402d1c6d31b119bca50f Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Sun, 26 Nov 2023 12:21:47 +0100 Subject: [PATCH 1/2] `ExactlyOneError::fold` --- src/exactly_one_err.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/exactly_one_err.rs b/src/exactly_one_err.rs index e24d33fc5..47c734c1a 100644 --- a/src/exactly_one_err.rs +++ b/src/exactly_one_err.rs @@ -63,6 +63,21 @@ where fn size_hint(&self) -> (usize, Option) { size_hint::add_scalar(self.inner.size_hint(), self.additional_len()) } + + fn fold(self, mut init: B, mut f: F) -> B + where + F: FnMut(B, Self::Item) -> B, + { + match self.first_two { + Some(Either::Left([first, second])) => { + init = f(init, first); + init = f(init, second); + } + Some(Either::Right(second)) => init = f(init, second), + None => {} + } + self.inner.fold(init, f) + } } impl ExactSizeIterator for ExactlyOneError where I: ExactSizeIterator {} From 5835a90f726e4e9a42a82f7d6b20aef8ac85f740 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Fri, 15 Dec 2023 19:06:04 +0100 Subject: [PATCH 2/2] Test `ExactlyOneError` specializations --- tests/specializations.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/specializations.rs b/tests/specializations.rs index abe41fe92..e14b1b669 100644 --- a/tests/specializations.rs +++ b/tests/specializations.rs @@ -330,6 +330,17 @@ quickcheck! { test_specializations(&it); test_double_ended_specializations(&it); } + + fn exactly_one_error(v: Vec) -> TestResult { + // Use `at_most_one` would be similar. + match v.iter().exactly_one() { + Ok(_) => TestResult::discard(), + Err(it) => { + test_specializations(&it); + TestResult::passed() + } + } + } } quickcheck! {