Skip to content

Commit

Permalink
merge: relax input type of Merge::from_removes_adds()
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Nov 7, 2023
1 parent 328c47e commit 634e409
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
6 changes: 2 additions & 4 deletions lib/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,10 @@ pub fn merge(slices: &Merge<&[u8]>) -> MergeResult {
merge_hunks.push(Merge::from_removes_adds(
parts[..num_diffs]
.iter()
.map(|part| ContentHunk(part.to_vec()))
.collect_vec(),
.map(|part| ContentHunk(part.to_vec())),
parts[num_diffs..]
.iter()
.map(|part| ContentHunk(part.to_vec()))
.collect_vec(),
.map(|part| ContentHunk(part.to_vec())),
));
}
}
Expand Down
16 changes: 12 additions & 4 deletions lib/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,18 @@ impl<T> Merge<T> {
}

/// Creates a new merge object from the given removes and adds.
pub fn from_removes_adds(removes: Vec<T>, adds: Vec<T>) -> Self {
// TODO: removes and adds can be just IntoIterator.
assert_eq!(adds.len(), removes.len() + 1);
let values = itertools::interleave(adds, removes).collect();
pub fn from_removes_adds(
removes: impl IntoIterator<Item = T>,
adds: impl IntoIterator<Item = T>,
) -> Self {
let removes = removes.into_iter();
let mut adds = adds.into_iter();
let mut values = SmallVec::with_capacity(removes.size_hint().0 * 2 + 1);
values.push(adds.next().expect("must have at least one add"));
for diff in removes.zip_longest(adds) {
let (remove, add) = diff.both().expect("must have one more adds than removes");
values.extend([remove, add]);
}
Merge { values }
}

Expand Down
4 changes: 2 additions & 2 deletions lib/src/simple_op_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ fn ref_target_from_proto(maybe_proto: Option<crate::protos::op_store::RefTarget>
crate::protos::op_store::ref_target::Value::Conflict(conflict) => {
let term_from_proto =
|term: crate::protos::op_store::ref_conflict::Term| term.value.map(CommitId::new);
let removes = conflict.removes.into_iter().map(term_from_proto).collect();
let adds = conflict.adds.into_iter().map(term_from_proto).collect();
let removes = conflict.removes.into_iter().map(term_from_proto);
let adds = conflict.adds.into_iter().map(term_from_proto);
RefTarget::from_merge(Merge::from_removes_adds(removes, adds))
}
}
Expand Down

0 comments on commit 634e409

Please sign in to comment.