From 2063f2f44ea90bbb62eacb2ca54f96a0711de8ba Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Wed, 23 Aug 2023 22:37:03 -0700 Subject: [PATCH] merge: implement `iter_mut()` and `into_iter()` These two are trivial to implement using `itertools::interleave()`. I don't think we even need tests. --- lib/src/merge.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/src/merge.rs b/lib/src/merge.rs index 7151ab142..54d19db13 100644 --- a/lib/src/merge.rs +++ b/lib/src/merge.rs @@ -215,13 +215,18 @@ impl Merge { trivial_merge(&self.removes, &self.adds) } - /// Returns an iterator over the terms. The items will alternate between - /// positive and negative terms, starting with positive (since there's one - /// more of those). + /// Returns an iterator over references to the terms. The items will + /// alternate between positive and negative terms, starting with + /// positive (since there's one more of those). pub fn iter(&self) -> impl Iterator { itertools::interleave(&self.adds, &self.removes) } + /// A version of `Merge::iter()` that iterates over mutable references. + pub fn iter_mut(&mut self) -> impl Iterator { + itertools::interleave(self.adds.iter_mut(), self.removes.iter_mut()) + } + /// Creates a new merge by applying `f` to each remove and add. pub fn map<'a, U>(&'a self, f: impl FnMut(&'a T) -> U) -> Merge { let builder: MergeBuilder = self.iter().map(f).collect(); @@ -268,6 +273,15 @@ impl MergeBuilder { } } +impl IntoIterator for Merge { + type Item = T; + type IntoIter = itertools::Interleave, std::vec::IntoIter>; + + fn into_iter(self) -> Self::IntoIter { + itertools::interleave(self.adds, self.removes) + } +} + impl FromIterator for MergeBuilder { fn from_iter>(iter: I) -> Self { let mut removes = vec![];