From 6965117dd8617174ed1dd12ac0662ab61a739fc2 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 6 Dec 2021 13:00:51 -0700 Subject: [PATCH] Allow patches to be composed with edit iterators in addition to other Patches This can avoid an extra allocation in some cases. Co-Authored-By: Max Brunsfeld --- crates/text/src/patch.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/crates/text/src/patch.rs b/crates/text/src/patch.rs index 91a0761017..a21f1125ec 100644 --- a/crates/text/src/patch.rs +++ b/crates/text/src/patch.rs @@ -9,7 +9,8 @@ pub struct Patch(Vec>); impl Patch where - T: Clone + T: 'static + + Clone + Copy + Ord + Sub @@ -41,9 +42,9 @@ where self.0 } - pub fn compose(&self, other: &Self) -> Self { + pub fn compose(&self, new_edits_iter: impl IntoIterator>) -> Self { let mut old_edits_iter = self.0.iter().cloned().peekable(); - let mut new_edits_iter = other.0.iter().cloned().peekable(); + let mut new_edits_iter = new_edits_iter.into_iter().peekable(); let mut composed = Patch(Vec::new()); let mut old_start = T::default(); @@ -200,6 +201,15 @@ where } } +impl<'a, T: Clone> IntoIterator for &'a Patch { + type Item = Edit; + type IntoIter = std::iter::Cloned>>; + + fn into_iter(self) -> Self::IntoIter { + self.0.iter().cloned() + } +} + #[cfg(test)] mod tests { use super::*;