From db0c386b69a5574e4731805254dbaa14229dde58 Mon Sep 17 00:00:00 2001 From: kinto-b Date: Thu, 18 Apr 2024 11:18:31 +1000 Subject: [PATCH] Implement `Unique::fold` --- src/unique_impl.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/unique_impl.rs b/src/unique_impl.rs index 0f6397e48..6ecc6e0ce 100644 --- a/src/unique_impl.rs +++ b/src/unique_impl.rs @@ -116,6 +116,23 @@ where }) } + fn fold(mut self, init: B, mut f: F) -> B + where + Self: Sized, + F: FnMut(B, Self::Item) -> B, + { + let UniqueBy { iter, used, .. } = &mut self.iter; + iter.fold(init, |mut acc, v| { + if let Entry::Vacant(e) = used.entry(v) { + let elt = e.key().clone(); + e.insert(()); + acc = f(acc, elt); + } + + acc + }) + } + #[inline] fn size_hint(&self) -> (usize, Option) { let (low, hi) = self.iter.iter.size_hint();