diff --git a/src/merge.rs b/src/merge.rs index e3d4876..e174609 100644 --- a/src/merge.rs +++ b/src/merge.rs @@ -1,32 +1,38 @@ -use std::ops::Range; +use std::fmt::Debug; use diff; use diff::Result::*; -#[derive(Debug, PartialEq, Eq)] -struct Chunk(Range, Range); +#[derive(Debug, PartialEq)] +struct Chunk<'a, Item: 'a + Debug + PartialEq + Eq>(&'a [diff::Result], &'a [diff::Result]); -#[derive(Debug, PartialEq, Eq)] +#[derive(Debug, PartialEq)] enum ChunkKind { Stable, Unstable, } -#[derive(Debug, PartialEq, Eq)] -struct ChunkItem { +#[derive(Debug, PartialEq)] +struct ChunkItem<'a, Item> +where + Item: 'a + Debug + PartialEq + Eq +{ kind: ChunkKind, - chunk: Chunk, + chunk: Chunk<'a, Item>, } -impl ChunkItem { - fn stable(chunk: Chunk) -> ChunkItem { +impl<'a, Item> ChunkItem<'a, Item> +where + Item: 'a + Debug + PartialEq + Eq +{ + fn stable(chunk: Chunk<'a, Item>) -> ChunkItem<'a, Item> { ChunkItem { kind: ChunkKind::Stable, chunk: chunk, } } - fn unstable(chunk: Chunk) -> ChunkItem { + fn unstable(chunk: Chunk<'a, Item>) -> ChunkItem<'a, Item> { ChunkItem { kind: ChunkKind::Unstable, chunk: chunk, @@ -36,7 +42,7 @@ impl ChunkItem { struct MergeIterator<'a, Item> where - Item: 'a + Item: 'a + Debug + PartialEq + Eq { left: &'a [diff::Result], right: &'a [diff::Result], @@ -45,7 +51,10 @@ where ri: usize, } -impl<'a, Item> MergeIterator<'a, Item> { +impl<'a, Item> MergeIterator<'a, Item> +where + Item: 'a + Debug + PartialEq + Eq +{ fn new(left: &'a [diff::Result], right: &'a [diff::Result]) -> MergeIterator<'a, Item> { MergeIterator { left, @@ -56,8 +65,11 @@ impl<'a, Item> MergeIterator<'a, Item> { } } -impl<'a, Item> Iterator for MergeIterator<'a, Item> { - type Item = ChunkItem; +impl<'a, Item> Iterator for MergeIterator<'a, Item> +where + Item: 'a + Debug + PartialEq + Eq +{ + type Item = ChunkItem<'a, Item>; fn next(&mut self) -> Option { let mut i = 0; @@ -65,7 +77,7 @@ impl<'a, Item> Iterator for MergeIterator<'a, Item> { i += 1; } if i > 0 { - let chunk = ChunkItem::stable(Chunk(self.li..self.li+i, self.ri..self.ri+i)); + let chunk = ChunkItem::stable(Chunk(&self.left[self.li..self.li+i], &self.right[self.ri..self.ri+i])); self.li += i; self.ri += i; return Some(chunk); @@ -90,14 +102,14 @@ impl<'a, Item> Iterator for MergeIterator<'a, Item> { ri += 1; }, (Some(&Both(..)), Some(&Both(..))) => { - let chunk = ChunkItem::unstable(Chunk(self.li..li, self.ri..ri)); + let chunk = ChunkItem::unstable(Chunk(&self.left[self.li..li], &self.right[self.ri..ri])); self.li = li; self.ri = ri; return Some(chunk); } _ => { if self.li < self.left.len() || self.ri < self.right.len() { - let chunk = ChunkItem::unstable(Chunk(self.li..self.left.len(), self.ri..self.right.len())); + let chunk = ChunkItem::unstable(Chunk(&self.left[self.li..self.left.len()], &self.right[self.ri..self.right.len()])); self.li = self.left.len(); self.ri = self.right.len(); return Some(chunk); @@ -124,12 +136,13 @@ mod test { let ob = diff::chars(o, b); let merge = MergeIterator::new(&oa, &ob).collect::>(); + assert_eq!(vec![ - ChunkItem::stable(Chunk(0..3, 0..3)), - ChunkItem::unstable(Chunk(3..6, 3..3)), - ChunkItem::stable(Chunk(6..9, 3..6)), - ChunkItem::unstable(Chunk(9..9, 6..9)), - ChunkItem::stable(Chunk(9..12, 9..12)), + ChunkItem::stable (Chunk(&oa[0.. 3], &ob[0.. 3])), + ChunkItem::unstable(Chunk(&oa[3.. 6], &ob[3.. 3])), + ChunkItem::stable (Chunk(&oa[6.. 9], &ob[3.. 6])), + ChunkItem::unstable(Chunk(&oa[9.. 9], &ob[6.. 9])), + ChunkItem::stable (Chunk(&oa[9..12], &ob[9..12])), ], merge); } @@ -144,9 +157,9 @@ mod test { let merge = MergeIterator::new(&oa, &ob).collect::>(); assert_eq!(vec![ - ChunkItem::stable(Chunk(0..3, 0..3)), - ChunkItem::unstable(Chunk(3..9, 3..9)), - ChunkItem::stable(Chunk(9..12, 9..12)), + ChunkItem::stable (Chunk(&oa[0.. 3], &ob[0.. 3])), + ChunkItem::unstable(Chunk(&oa[3.. 9], &ob[3.. 9])), + ChunkItem::stable (Chunk(&oa[9..12], &ob[9..12])), ], merge); } @@ -161,8 +174,8 @@ mod test { let merge = MergeIterator::new(&oa, &ob).collect::>(); assert_eq!(vec![ - ChunkItem::stable(Chunk(0..9, 0..9)), - ChunkItem::unstable(Chunk(9..9, 9..12)), + ChunkItem::stable (Chunk(&oa[0..9], &ob[0.. 9])), + ChunkItem::unstable(Chunk(&oa[9..9], &ob[9..12])), ], merge); } @@ -177,8 +190,8 @@ mod test { let merge = MergeIterator::new(&oa, &ob).collect::>(); assert_eq!(vec![ - ChunkItem::stable(Chunk(0..6, 0..6)), - ChunkItem::unstable(Chunk(6..9, 6..12)), + ChunkItem::stable (Chunk(&oa[0..6], &ob[0.. 6])), + ChunkItem::unstable(Chunk(&oa[6..9], &ob[6..12])), ], merge); } @@ -193,7 +206,7 @@ mod test { let merge = MergeIterator::new(&oa, &ob).collect::>(); assert_eq!(vec![ - ChunkItem::unstable(Chunk(0..6, 0..6)), + ChunkItem::unstable(Chunk(&oa[0..6], &ob[0..6])), ], merge); } }